python urllib2 urlopen响应:
<addinfourl at 1081306700 whose fp = <socket._fileobject object at 0x4073192c>>
预期:
{ “标记”: “mYWmzpunvasAT795niiR”}
答案 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
library比urllib2
更直观易用。