19

python urllib2 urlopen response:

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

expected:

{"token":"mYWmzpunvasAT795niiR"}

Shown
  • 201
  • 1
  • 2
  • 7

3 Answers3

26

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 ...
mhawke
  • 84,695
  • 9
  • 117
  • 138
  • I have one question here. If I don't store the contents of `f` to `data`, and simply perform `f.read()`, I get the contents only once. If I do `f.read()` again, it prints an empty string. Why is that? – Sidharth Samant May 21 '16 at 11:16
  • @SidharthSamant: because you have consumed all of the data from the stream - it is not stored internally by `urllib2`. – mhawke May 21 '16 at 11:53
4

Add the following after your call to urlopen

print feed.read()
David
  • 6,462
  • 2
  • 25
  • 22
1

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

joeln
  • 3,563
  • 25
  • 31