Reputation: 73
I'm using urllib2 to open a url. Now I need the html file as a string. How do I do this?
Upvotes: 7
Views: 18336
Reputation: 3542
>>> import urllib2
>>> s = urllib2.urlopen('http://www.google.com').read()
>>> s
<big long string here>
Upvotes: 1
Reputation: 371
In python3, it should be changed to urllib.request.openurl('http://www.example.com/').read().decode('utf-8')
.
Upvotes: 16
Reputation: 71
i think in python3 the urllib.request.openurl('http://www.example.com/').read() method return in binary mode
Upvotes: 4
Reputation: 28695
The easiest way would be:
f = urllib2.urlopen("http://example.com/foo/bar")
s = f.read()
# s now holds the contents of the site
There is more information in the urllib2 docs.
urlopen()
returns a file-like object, so Python's file object methods work.
Upvotes: 11