Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python urllib2 urlopen response

python urllib2 urlopen response:

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

expected:

{"token":"mYWmzpunvasAT795niiR"}

like image 818
Shown Avatar asked Aug 23 '12 00:08

Shown


People also ask

What does Urlopen return Python?

The data returned by urlopen() or urlretrieve() is the raw data returned by the server. This may be binary data (such as an image), plain text or (for example) HTML. The HTTP protocol provides type information in the reply header, which can be inspected by looking at the Content-Type header.

What does urllib2 Urlopen do?

The urllib2 module defines functions and classes which help in opening URLs (mostly HTTP) in a complex world — basic and digest authentication, redirections, cookies and more. Open the URL url, which can be either a string or a Request object. HTTPS requests do not do any verification of the server's certificate.

How do I fix No module named urllib2?

The Python "ModuleNotFoundError: No module named 'urllib2'" occurs because the urllib2 module has been split into urllib. request and urllib. response in Python 3. To solve the error, import the module as from urllib.

Is urllib2 deprecated?

urllib2 is deprecated in python 3. x. use urllib instaed.


2 Answers

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 ...
like image 77
mhawke Avatar answered Oct 01 '22 17:10

mhawke


Add the following after your call to urlopen

print feed.read()
like image 28
David Avatar answered Oct 01 '22 16:10

David