Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read html from a url in python 3

Tags:

python

html

url

I looked at previous similar questions and got only more confused.

In python 3.4, I want to read an html page as a string, given the url.

In perl I do this with LWP::Simple, using get().

A matplotlib 1.3.1 example says: import urllib; u1=urllib.urlretrieve(url). python3 can't find urlretrieve.

I tried u1 = urllib.request.urlopen(url), which appears to get an HTTPResponse object, but I can't print it or get a length on it or index it.

u1.body doesn't exist. I can't find a description of the HTTPResponse in python3.

Is there an attribute in the HTTPResponse object which will give me the raw bytes of the html page?

(Irrelevant stuff from other questions include urllib2, which doesn't exist in my python, csv parsers, etc.)

Edit:

I found something in a prior question which partially (mostly) does the job:

u2 = urllib.request.urlopen('http://finance.yahoo.com/q?s=aapl&ql=1')  for lines in u2.readlines():     print (lines) 

I say 'partially' because I don't want to read separate lines, but just one big string.

I could just concatenate the lines, but every line printed has a character 'b' prepended to it.

Where does that come from?

Again, I suppose I could delete the first character before concatenating, but that does get to be a kloodge.

like image 718
user1067305 Avatar asked Jun 11 '14 01:06

user1067305


2 Answers

Note that Python3 does not read the html code as a string but as a bytearray, so you need to convert it to one with decode.

import urllib.request  fp = urllib.request.urlopen("http://www.python.org") mybytes = fp.read()  mystr = mybytes.decode("utf8") fp.close()  print(mystr) 
like image 144
davidgh Avatar answered Sep 22 '22 09:09

davidgh


Try the 'requests' module, it's much simpler.

#pip install requests for installation  import requests  url = 'https://www.google.com/' r = requests.get(url) r.text 

more info here > http://docs.python-requests.org/en/master/

like image 34
Aaron T. Avatar answered Sep 23 '22 09:09

Aaron T.