Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing html tags with Python

Tags:

python-3.x

I have been given an url and I want to extract the contents of the <BODY> tag from the url. I'm using Python3. I came across sgmllib but it is not available for Python3.

Can someone please guide me with this? Can I use HTMLParser for this?

Here is what i tried:

import urllib.request
f=urllib.request.urlopen("URL")
s=f.read()

from html.parser import HTMLParser
class MyHTMLParser(HTMLParser):
    def handle_data(self, data):
        print("Encountered   some data:", data)

parser = MyHTMLParser()
parser.feed(s)

this gives me error : TypeError: Can't convert 'bytes' object to str implicitly

like image 516
gsb Avatar asked Feb 01 '12 20:02

gsb


1 Answers

To fix the TypeError change line #3 to

s = str(f.read())

The web page you're getting is being returned in the form of bytes, and you need to change the bytes into a string to feed them to the parser.

like image 95
pycoder112358 Avatar answered Oct 17 '22 17:10

pycoder112358