TypeError: cannot concatenate 'str' and 'instance' objects (python urllib)

家住魔仙堡 提交于 2019-12-06 08:27:43

urlopen(url) returns a file-like object. To obtain the string contents, try

CPs = CPs + urllib.urlopen(url).read()

urllib.urllopen doesn't return a string, it returns an object
doc

If all went well, a file-like object is returned.

The problem is in this line: CPs = CPs + urllib.urlopen(url) I assume CPs is a string however urllib.urlopen(url) returns a file like object.

If you want to join the contents of the file at url with CPs then you need to do something like this: CPs = CPs + urllib.urlopen(url).read().

What is CPs? It looks like it is a string. urlopen will return an instance of a file-like object, not a string. See - http://docs.python.org/library/urllib.html.

The error is not thrown from the urlopen, but because you are trying to concatenate a string with an object instance.

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