类型错误:POST数据应为字节、字节的iterable或文件对象

2024-04-19 23:00:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在chp.3中学习python,但是我有个问题。 有人能帮我吗?在

import urllib.request
import time

def send_to_twitter(msg):

password_manager = urllib.request.HTTPPasswordMgr()
password_manager.add_password("Twitter API",
                              "http://twitter.com/statuses","xxx","xxx")
http_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
page_opener = urllib.request.build_opener(http_handler)
urllib.request.install_opener(page_opener)
params = urllib.parse.urlencode({'status': msg})
resp = urllib.request.urlopen("http://twitter.com/statuses/update.json", params)
resp.read()

def get_price():

page = urllib.request.urlopen("http://beans-r-us.appspot.com/prices.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("Do you want to see the price now(Y/N)? ")

if price_now =="Y":

        send_to_twitter(get_price())
else:
            price=99.99

        while price > 4.74:

          time.sleep(900)

          price = get_price()

        send_to_twitter("Buy")            

但不能跑。在

TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.


Tags: oftotextcomsendhttpgetrequest