urllib2 to string

0 votes
asked Apr 15, 2010 by nathan-lawrence

I'm using urllib2 to open a url. Now I need the html file as a string. How do I do this?

4 Answers

0 votes
answered Jan 16, 2010 by jonesy
>>> import urllib2
>>> s = urllib2.urlopen('http://www.google.com').read()
>>> s
<big long string here> 
0 votes
answered Apr 15, 2010 by nick-presta

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.

0 votes
answered Jan 18, 2014 by wilson

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

0 votes
answered Jan 12, 2016 by zhouqiang-wang

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

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...