python urllib2 urlopen response

大城市里の小女人 提交于 2019-12-03 04:51:04

You need to bind the resultant file-like object to a variable, otherwise the interpreter just dumps it via repr:

>>> import urllib2
>>> urllib2.urlopen('http://www.google.com')
<addinfourl at 18362520 whose fp = <socket._fileobject object at 0x106b250>>
>>> 
>>> f = urllib2.urlopen('http://www.google.com')
>>> f
<addinfourl at 18635448 whose fp = <socket._fileobject object at 0x106b950>>

To get the actual data you need to perform a read().

>>> data = f.read()
>>> data[:50]
'<!doctype html><html itemscope="itemscope" itemtyp'

To see the headers returned:

>>> print f.headers
Date: Thu, 23 Aug 2012 00:46:22 GMT
Expires: -1
Cache-Control: private, max-age=0
... etc ...

Add the following after your call to urlopen

print feed.read()

Perhaps you will find using the requests library more intuitive to use than urllib2.

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