简体   繁体   English

python urllib2 urlopen响应

[英]python urllib2 urlopen response

python urllib2 urlopen response: python urllib2 urlopen响应:

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

expected: 预期:

{"token":"mYWmzpunvasAT795niiR"} { “令牌”: “mYWmzpunvasAT795niiR”}

You need to bind the resultant file-like object to a variable, otherwise the interpreter just dumps it via repr : 您需要将生成的类文件对象绑定到变​​量,否则解释器只需通过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() . 要获取执行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 ...

在调用urlopen后添加以下内容

print feed.read()

也许你会发现使用requestsurllib2更直观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM