urllib - Обновление с Python2 до Python3

Я попытался адаптировать следующий сценарий. Я получил то, что следовало.

#!/usr/bin/python3

import re
import csv

import urllib.request, urllib.parse

class Spreadsheet(object):
    def __init__(self, key):
        super(Spreadsheet, self).__init__()
        self.key = key

class Client(object):
    def __init__(self, email, password):
        super(Client, self).__init__()
        self.email = email
        self.password = password

    def _get_auth_token(self, email, password, source, service):
        url = "https://www.google.com/accounts/ClientLogin"
        params = {
            "Email": email, "Passwd": password,
            "service": service,
            "accountType": "HOSTED_OR_GOOGLE",
            "source": source
        }
        req = urllib.request.Request(url, urllib.parse.urlencode(params))
        return re.findall(r"Auth=(.*)", urllib.request.urlopen(req).read())[0]

    def get_auth_token(self):
        source = type(self).__name__
        return self._get_auth_token(self.email, self.password, source, service="wise")

    def download(self, spreadsheet, gid=0, format="csv"):
        url_format = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=%s&exportFormat=%s&gid=%i"
        headers = {
            "Authorization": "GoogleLogin auth=" + self.get_auth_token(),
            "GData-Version": "3.0"
        }
        req = urllib.request.Request(url_format % (spreadsheet.key, format, gid), headers=headers)
        return urllib.request.urlopen(req)

if __name__ == "__main__":
    email = "xxx" # (your email here)
    password = "yyyy"
    spreadsheet_id = "zzz" # (spreadsheet id here)

    # Create client and spreadsheet objects
    gs = Client(email, password)
    ss = Spreadsheet(spreadsheet_id)

    # Request a file-like object containing the spreadsheet's contents
    print(gs.download(ss).read())

Моя проблема в том, что у меня следующая ошибка.

Traceback (most recent call last):
  File "/Users/test.py", line 54, in <module>
    print(gs.download(ss).read())
  File "/Users/test.py", line 38, in download
    "Authorization": "GoogleLogin auth=" + self.get_auth_token(),
  File "/Users/test.py", line 33, in get_auth_token
    return self._get_auth_token(self.email, self.password, source, service="wise")
  File "/Users/test.py", line 29, in _get_auth_token
    return re.findall(r"Auth=(.*)", urllib.request.urlopen(req).read())[0]
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/urllib/request.py", line 138, in urlopen
    return opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/urllib/request.py", line 364, in open
    req = meth(req)
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/urllib/request.py", 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.

Проблемы исходят от urllib.request.urlopen(req) в методе _get_auth_token. Есть ли способ исправить это?


person Community    schedule 15.07.2013    source источник


Ответы (1)


Да, закодируйте свои данные в байты перед публикацией:

req = urllib.request.Request(url, urllib.parse.urlencode(params).encode('ASCII'))

Я предполагаю, что ваши данные - только ASCII (обычно адреса электронной почты, предположительно, ваш пароль тоже).

person Martijn Pieters    schedule 15.07.2013
comment
Это делает свою работу. Спасибо ! Я приму твой ответ как можно скорее. - person ; 15.07.2013