Erro na função send_to_twitter em liguagem python

0 respostas
jaymeproni

Olá gente, eu comprei o livro programando da série Use a Cabeça que ensina programação com python e tentei fazer o código de envio do twitter … aqui vai o código e os erros na sequencia.

PS.: Não estou entendendo o erro, eu apenas copiei o código e coloquei meu login e senha nos locais específicos. Testei e nada.

import urllib.request
import time

def get_price():
    page = urllib.request.urlopen("http://www.beans-r-us.biz/prices-loyalty.html")
    text = page.read().decode("utf-8")
    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 = "Mensagem enviada para o twitter via python!"
    password_manager = urllib.request.HTTPPasswordMgr()
    password_manager.add_password("Twitter API", "http://twitter.com/statuses", "MEU_LOGIN", "MINHA_SENHA")
    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()


price_now = input("Do you want to see the price now (y/n)? ")

if price_now == 'y':
        send_to_twitter()
else:
        price = 99.99
        while price > 4.74:
                time.sleep(180)
                price = get_price()
        send_to_twitter()

Os erros são:

Traceback (most recent call last):

File “C:\cursopython\cap1\<a href="http://getprice.py">getprice.py</a>”, line 28, in 

send_to_twitter()

File “C:\cursopython\cap1\<a href="http://getprice.py">getprice.py</a>”, line 21, in send_to_twitter

resp = urllib.request.urlopen(“<a href="http://twitter.com/statuses/update.json">http://twitter.com/statuses/update.json</a>”, params)

File “C:\Python32\lib\urllib\<a href="http://request.py">request.py</a>”, line 138, in urlopen

return opener.open(url, data, timeout)

File “C:\Python32\lib\urllib\<a href="http://request.py">request.py</a>”, line 364, in open

req = meth(req)

File “C:\Python32\lib\urllib\<a href="http://request.py">request.py</a>”, line 1052, in do_request_

raise TypeError("POST data should be bytes"

TypeError: POST data should be bytes or an iterable of bytes. It cannot be str.

Volto a dizer, deveria funcionar. Estou usando python 3.2 … o livro pede 3 no mínimo e eu apenas copei o código.

Criado 31 de março de 2011
Respostas 0
Participantes 1