zoukankan      html  css  js  c++  java
  • Python函数

    import urllib.request
    def get_price():
        page=urllib.request.urlopen("http://www.baidu.com")
        text=page.read().decode("utf8")
        where=text.find('="') 
        start_of_price=where+2
        end_of_price=start_of_price+4
        print(text[start_of_price:end_of_price])
    
    get_price()
    

      加入函数的取价格程序:

    import urllib.request
    import time
    
    def get_price():
        page=urllib.request.urlopen("http://www.beans-r-us.biz/price.html")
        text=page.read().decode("utf8")
        where=text.find('="')
        start_of_price=where+2
        end_of_price=start_of_price+4
        return float(text[start_of_price:end_of_price])
    
    price_now=input("现在需要价格吗(Y/N)?")
    if price_now=="Y":
        print(get_price())
    else:
        price=99.99
        while peice>4.47:
            time.sleep(900)
            price=get_price()
        print("Buy!")
    

      向Twitter发送消息!    send_to_twitter()函数?

    import urllib.request
    import time
    
    def get_price():
        page=urllib.request.urlopen("http://www.beans-r-us.biz/price.html")
        text=page.read().decode("utf8")
        where=text.find('="')
        start_of_price=where+2
        end_of_price=start_of_price+4
        return float(text[start_of_price:end_of_price])
    
    def send_to_twitter():
        msg="I am a message that will be sent to Twitter"
        password_manager=ullib.request.HTTPPasswordMgr()
        password_manager.add_password("Twitter API","http://twitter.com/statuses","...","...")
        http_handler=urllib.request.HTTPBasicAuthHandler(password_manager)
        page_opener=urllib.request.build_opener(http_handler)
        urllib.reques.install_opener(page_opener)
        params=urllib.parse.urlencode
        ({'status':msg})
        resp=urllib.request.urlop
        ("http://twitter.com/statueses/update.json",params)
        resp.read()
    
    price_now=input("现在需要价格吗(Y/N)?")
    if price_now=="Y":
        send_to_twitter()
    else:
        price=99.99
        while peice>4.47:
            time.sleep(900)
            price=get_price()
        send_to_twitter()
    

      修改为可以传参的函数

    import urllib.request
    import time
    
    def get_price():
        page=urllib.request.urlopen("http://www.beans-r-us.biz/price.html")
        text=page.read().decode("utf8")
        where=text.find('="')
        start_of_price=where+2
        end_of_price=start_of_price+4
        return float(text[start_of_price:end_of_price])
    
    def send_to_twitter( msg):
        password_manager=ullib.request.HTTPPasswordMgr()
        password_manager.add_password("Twitter API","http://twitter.com/statuses","...","...")
        http_handler=urllib.request.HTTPBasicAuthHandler(password_manager)
        page_opener=urllib.request.build_opener(http_handler)
        urllib.reques.install_opener(page_opener)
        params=urllib.parse.urlencode
        ({'status':msg})
        resp=urllib.request.urlop
        ("http://twitter.com/statueses/update.json",params)
        resp.read()
    
    price_now=input("现在需要价格吗(Y/N)?")
    if price_now=="Y":
        send_to_twitter(get_price())
    else:
        price=99.99
        while peice>4.47:
            time.sleep(900)
            price=get_price()
        send_to_twitter("Buy!")
    

    总结:

    由于自己没有Twitter账户自己无法验证程序的正误。也想注册一个Twitter账户试一下。但是始终无法进入Twitter官网!

  • 相关阅读:
    使用SetTimer函数为Delphi的Win控件设置时钟
    关于QT版本的安装配置的一些困惑
    Linux设备驱动开发详解-Note(11)--- Linux 文件系统与设备文件系统(3)
    邪恶的C++
    TControl的主要功能研究(属性,函数,事件)
    Delphi研究,对全局变量函数与OOP编程关系的一点体会 good
    QT 相关资源(书籍、论坛、博客等。。。)整理
    VC UI界面库大集合
    .net程序员求职简历
    C++著名程序库的比较
  • 原文地址:https://www.cnblogs.com/lizanqirxx/p/5147930.html
Copyright © 2011-2022 走看看