473,566 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

confused by HTMLParser class

tried all kinds of combos to get this to work.
http://docs.python.org/lib/module-HTMLParser.html

from HTMLParser import HTMLParser

class MyHTMLParser(HT MLParser):

def handle_starttag (self, tag, attrs):
print "Encountere d the beginning of a %s tag" % tag

def handle_endtag(s elf, tag):
print "Encountere d the end of a %s tag" % tag
from HTMLParser import HTMLParser
import urllib
import myhtmlparser

x = MyHTMLParser(HT MLParser())
site = urllib.urlopen( "http://docs.python.org/lib/module-
HTMLParser.html ")
for row in site:
print x.handle_startt ag()
Jun 27 '08 #1
3 2108
On May 28, 11:20 am, globalrev <skanem...@yaho o.sewrote:
tried all kinds of combos to get this to work.
Did you try searching this group? There were recent posts discussing
basic usage of HTMLParser.

Throwing random code together is the least likely way to actually get
it to work.
x = MyHTMLParser(HT MLParser())
site = urllib.urlopen( "http://docs.python.org/lib/module-
HTMLParser.html ")
for row in site:
print x.handle_startt ag()
Why are you passing HTMLParser in to initialise MyHTMLParser?

Why are you iterating over site and expecting your instance of
MyHTMLParser to magically know about it?

Why haven't you read the urllib.urlopen docs, to see you need to do
a .read() to actually get the page data?

Why are you so resistant to reading some basic tutorials?
Jun 27 '08 #2
On May 28, 3:20*am, globalrev <skanem...@yaho o.sewrote:
tried all kinds of combos to get this to work.

http://docs.python.org/lib/module-HTMLParser.html

from HTMLParser import HTMLParser

class MyHTMLParser(HT MLParser):

* * def handle_starttag (self, tag, attrs):
* * * * print "Encountere d the beginning of a %s tag" % tag

* * def handle_endtag(s elf, tag):
* * * * print "Encountere d the end of a %s tag" % tag

from HTMLParser import HTMLParser
import urllib
import myhtmlparser

x = MyHTMLParser(HT MLParser())
site = urllib.urlopen( "http://docs.python.org/lib/module-
HTMLParser.html ")
for row in site:
* * print x.handle_startt ag()
this works fine to me:
from HTMLParser import HTMLParser

class MyHTMLParser(HT MLParser):

def handle_starttag (self, tag, attrs):
print "Encountere d the beginning of a %s tag" % tag

def handle_endtag(s elf, tag):
print "Encountere d the end of a %s tag" % tag

#from HTMLParser import HTMLParser
import urllib
#import mythmlparser

site = urllib.urlopen( "http://docs.python.org/lib/module-
HTMLParser.html ")
x = MyHTMLParser() # x = MyHTMLParser(HT MLParser())
x.feed(site.rea d())
x.close()
for row in site:
print x.handle_startt ag()
site.close()
You should also read this:
http://www.diveintopython.org/html_p...ting_data.html
for example
Jun 27 '08 #3
globalrev wrote:
tried all kinds of combos to get this to work.
In case you meant to say that you can't get it to work, consider using lxml
instead.

http://codespeak.net/lxml
http://codespeak.net/lxml/lxmlhtml.html

Stefan
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2484
by: Adonis | last post by:
When parsing my html files, I use handle_pi to capture some embedded python code, but I have noticed that in the embedded python code if it contains html, HTMLParser will parse it as well, and thus causes an error when I exec the code, raises an EOL error. I have a work around for this as I use different set of characters rather that <tag> use...
11
6047
by: Sean Cody | last post by:
I'm trying to take a webpage that has a nxn table of entries (bus times) and convert it to a 2D array (list of lists). Initially this was simple but I need to be able to access whole 'columns' of data so the 2D array cannot be sparse but in the HTML file I'm parsing there can be sparse entries which are repsented in the table as &nbsp...
2
2342
by: Matthew Wilson | last post by:
I want to parse an html file and extract my router's IP address. I wrote this code and I have python 2.3 installed: #! /usr/bin/env python import HTMLParser class HP(HTMLParser.HTMLParser): def handle_starttag(self, tag, data):
4
3321
by: Kevin T. Ryan | last post by:
Hi all - I'm somewhat new to python (about 1 year), and I'm trying to write a program that opens a file like object w/ urllib.urlopen, and then parse the data by passing it to a class that subclasses HTMLParser.HTMLParser. On the web page, however, there is javascript - and I think that is causing an error in parsing the data. Here's the...
1
2741
by: Rajarshi Guha | last post by:
Hi, I have some HTML that looks essentially consists of a series of <div>'s and each <div> having one of two classes (tnt-question or tnt-answer). I'm using HTMLParser to handle the tags as: class MyHTMLParser(HTMLParser.HTMLParser): def handle_starttag(self, tag, attrs): if len(attrs) == 1: cls,whichcls = attrs
9
2366
by: florent | last post by:
I'm trying to parse html documents from the web, using the HTMLParser class of the HTMLParser module (python 2.3), but some web documents are not fully valids. When the parser finds an invalid tag, he raises an exception. Then it seems impossible to resume the parsing just after where the exception was raised. I'd like to continue parsing an...
1
1870
by: Kenneth McDonald | last post by:
I'm writing a program that will parse HTML and (mostly) convert it to MediaWiki format. The two Python modules I'm aware of to do this are HTMLParser and htmllib. However, I'm currently experiencing either real or conceptual difficulty with both, and was wondering if I could get some advice. The problem I'm having with HTMLParser is simple;...
3
1726
by: ychaouche | last post by:
Hi, python experts. <console trace> chaouche@CAY:~/TEST$ python nettoyageHTML.py chaouche@CAY:~/TEST$ </console trace> This is the nettoyageHTML.py python script <code>
8
8537
by: jonbutler88 | last post by:
Just writing a simple website spider in python, keep getting these errors, not sure what to do. The problem seems to be in the feed() function of htmlparser. Traceback (most recent call last): File "spider.py", line 38, in <module> s.crawl(site) File "spider.py", line 30, in crawl self.parse(url) File "spider.py", line 21, in parse
0
7666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8108
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7644
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
2083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.