Nathan Lawrence
Nathan Lawrence

Reputation: 73

urllib2 to string

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

Answers (4)

jonesy
jonesy

Reputation: 3542

>>> import urllib2
>>> s = urllib2.urlopen('http://www.google.com').read()
>>> s
<big long string here> 

Upvotes: 1

Joe Wang
Joe Wang

Reputation: 371

In python3, it should be changed to urllib.request.openurl('http://www.example.com/').read().decode('utf-8').

Upvotes: 16

wilson
wilson

Reputation: 71

i think in python3 the urllib.request.openurl('http://www.example.com/').read() method return in binary mode

Upvotes: 4

Nick Presta
Nick Presta

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

Related Questions