Python urllib.request.urlopen: AttributeError: 'bytes' object has no attribute 'data'

大憨熊 提交于 2021-01-27 22:13:58

问题


I am using Python 3 and trying to connect to dstk. I am getting an error with urllib package.

I researched a lot on SO and could not find anything similar to this problem.

api_url = self.api_base+'/street2coordinates'
api_body = json.dumps(addresses)
#api_url=api_url.encode("utf-8")
#api_body=api_body.encode("utf-8")
print(type(api_url))
response_string = six.moves.urllib.request.urlopen(api_url, api_body).read()
response = json.loads(response_string)

If I do not encode the api_url and api_body I get the below:

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1247, in do_request_
    raise TypeError(msg)
TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.

However if I try and encode them to utf-8 (uncommenting the lines) then I get the below error:

 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 514, in open
    req.data = data
AttributeError: 'bytes' object has no attribute 'data'

This seems like a circular error for me and I am not able to resolve it. I did try make to solutions from SO regards to change it to json.load etc but nothing seems to work.


回答1:


You are encoding both the url and the request body, but only the body should be encoded.

This ought to work:

api_url = self.api_base+'/street2coordinates'
api_body = json.dumps(addresses)
api_body=api_body.encode("utf-8")
response_string = six.moves.urllib.request.urlopen(api_url, api_body).read()
response = json.loads(response_string)

urlopen's arguments are passed to another class to create an opener, and this class does not know whether it has been passed a url or a Request instance. So it checks whether the "url" is a string - if the "url" is a string, it creates a Request, if not it assumes that "url" is a Request instance and tries to set its data attribute, causing the exception that you are seeing.

The code in question is here.



来源:https://stackoverflow.com/questions/58086723/python-urllib-request-urlopen-attributeerror-bytes-object-has-no-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!