python urllib2 urlopen响应

时间:2012-08-23 00:32:03

标签: python urllib2 urlopen

python urllib2 urlopen响应:

<addinfourl at 1081306700 whose fp = <socket._fileobject object at 0x4073192c>>

预期:

{ “标记”: “mYWmzpunvasAT795niiR”}

3 个答案:

答案 0 :(得分:26)

您需要将生成的类文件对象绑定到变​​量,否则解释器只会通过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>>

要获取执行read()

所需的实际数据
>>> data = f.read()
>>> data[:50]
'<!doctype html><html itemscope="itemscope" itemtyp'

要查看返回的标头:

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

答案 1 :(得分:4)

在致电urlopen

后添加以下内容
print feed.read()

答案 2 :(得分:2)

也许您会发现使用requests libraryurllib2更直观易用。