打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
urllib-从Python2更新到Python3

我已经尝试适应the following script.我已经获得了后续的内容.

#!/usr/bin/python3import reimport csvimport urllib.request, urllib.parseclass Spreadsheet(object):    def __init__(self, key):        super(Spreadsheet, self).__init__()        self.key = keyclass 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.

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

解决方法:

是的,在发布之前将数据编码为字节:

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

我在这里假设您的数据仅是ASCII(电子邮件地址通常也是,大概您的密码也是).

来源:https://www.icode9.com/content-1-575651.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
python
DjangoRestFramework,认证组件、权限组件、频率组件、url注册器、响应器、分页组件
python实现服务器监控报警消息用微信发送(附代码)
requests.get()参数
Py之urllib2:Python库之urllib、urllib2、urllib3系列简介、安装、使用方法之详细攻略
python的urllib/2用法典型的例子
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服