首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python3库-从Python2更新到urllib

Python3库-从Python2更新到urllib
EN

Stack Overflow用户
提问于 2013-07-16 00:11:00
回答 1查看 418关注 0票数 3

我试着去适应the following script。我已经得到了接下来要做的。

代码语言:javascript
复制
#!/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())

我的问题是我有以下错误。

代码语言:javascript
复制
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.

问题来自_get_auth_token方法中的urllib.request.urlopen(req)。有没有办法解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-16 00:14:11

可以,在发布前将数据编码为字节:

代码语言:javascript
复制
req = urllib.request.Request(url, urllib.parse.urlencode(params).encode('ASCII'))

我在这里假设你的数据是ASCII格式的(电子邮件地址通常是ASCII格式的,大概你的密码也是)。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17658804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档