python函数调整价格_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官网!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值