473,698 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HTMLParser handler_startta g misses lots of tags!

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.H TMLParser):

def handle_starttag (self, tag, data):
print "tag is %s." % (tag)

def handle_comment( self, data):
print "caught a comment: %s." % (data)

def handle_data(sel f, data):
if "IP" in data:
print "Caught %s." % data

hp = HP()
out = open('routersta tus.html')
for line in out:
hp.feed(line)
I figured that when I ran this on the html code at the bottom of this
file, it would print every tag, but instead, this is what I got:

tag is html.
tag is head.
tag is meta.
tag is meta.
tag is meta.
tag is meta.
tag is meta.
tag is title.
tag is link.
tag is script.
tag is body.
tag is form.

The program seems to take a vacation after the opening form tag. What
am I doing wrong?

Finally, this is the html code I am trying to parse:

<html>

<head>
<meta http-equiv="content-type" content="text/html;charset=IS O-8859-1">
<meta name="generator " content="Adobe GoLive 5">
<META http-equiv='Pragma' CONTENT='no-cache'>
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META http-equiv='Refresh' CONTENT='20'>
<title>router form</title>
<link rel="stylesheet " href="form.css" >
<script language="javas cript" type="text/javascript">
<!-- hide script from old browsers
function loadhelp(num) {

parent.helpfram e.document.loca tion.href="help/help"+num+".htm l"

}
function newwindow(F)
{
if((F.status.va lue =="checked")||( F.EncapPTelstra .value=="checke d")||(F.EncapAo lDhcp.value=="c hecked"))
window.open('en atherstatus.htm ', 'enstatherstatu s', 'width=380,heig ht=450,status=y es');
else if((F.EncapPPTP .value =="checked"))
window.open('pp tpstatus.htm', 'pptpstatus', 'width=380,heig ht=320,status=y es');
else
window.open('pp poestatus.htm', 'pppoestatus', 'width=380,heig ht=320,status=y es');

}
//-->
</script>
</head>
<body bgcolor="#fffff f" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0 " onload="loadhel p('_SysStatus') ">
<form method="POST">
<input type=hidden name=status value=>
<input type="hidden" name=EncapPTels tra value=>
<input type="hidden" name=EncapPPTP value=>
<input type="hidden" name=EncapAolDh cp value=>
<table border="0" cellpadding="0" cellspacing="3" width="100%">
<tr>
<td colspan="2">
<h1>Router Status</h1>
</td>
</tr>
<!-- RULE //-->
<tr>
<td colspan="2">
<img src="img/liteblue.gif" width="100%" height="2" border="0">
</td>
</tr>
<!-- END RULE //-->
<tr>
<td width="60%">
<b>Account Name</b>
</td>
<td width="40%">

</td>
</tr>

<tr>
<td width="60%">
<b>Firmware Version </b>
</td>
<td width="40%">
4.13 Aug 20 2003
</td>
</tr>

<!-- RULE //-->
<tr>
<td colspan="2">
<img src="img/liteblue.gif" width="100%" height="2" border="0">
</td>
</tr>
<!-- END RULE //-->
<tr>
<td colspan="2">
<span class="subhead" >Internet Port </span>
</td>
</tr>
<tr>
<td width="60%">
<b>MAC Address </b>
</td>
<td width="40%">
00:09:5b:29:3d: b4
</td>
</tr>
<tr>
<td width="60%">
<b>IP Address </b>
</td>
<td width="40%">
66.72.206.129
</td>
</tr>
<tr>
<td width="60%">
<b>DHCP </b>
</td>
<td width="40%">
None
</td>
</tr>
<tr>
<td width="60%">
<b>IP Subnet Mask </b>
</td>
<td width="40%">
None
</td>
</tr>
<tr>
<td width="60%">
<b>Domain Name Server</b>
</td>
<td width="40%">
66.73.20.40
</td>
</tr>
<tr>
<td width="60%">
<b></b>
</td>
<td width="40%">
206.141.193.55
</td>
</tr>
<!-- RULE //-->
<tr>
<td colspan="2">
<img src="img/liteblue.gif" width="100%" height="2" border="0">
</td>
</tr>
<!-- END RULE //-->
<tr>
<td colspan="2">
<span class="subhead" >LAN Port </span>
</td>
</tr>
<tr>
<td width="60%">
<b>MAC Address </b>
</td>
<td width="40%">
00:09:5b:29:3d: b3
</td>
</tr>
<tr>
<td width="60%">
<b>IP Address </b>
</td>
<td width="40%">
192.168.0.1
</td>
</tr>
<tr>
<td width="60%">
<b>DHCP </b>
</td>
<td width="40%">
Server
</td>
</tr>
<tr>
<td width="60%">
<b>IP Subnet Mask </b>
</td>
<td width="40%">
255.255.255.0
</td>
</tr>

</table>
<TABLE border=0 width="100%">
<tr width="100%">
<td>
<img src="img/liteblue.gif" width="100%" height="2" border="0">
</td>
</tr>
<TR width="100%">
<TD>

<span class="subhead" >Wireless Port </span>
</TD>
</TR>

</TABLE>

<TABLE width="100%" border=0>

<TR>
<TD width="60%"><b> MAC Address
(BSSID) </b></TD>
<TD width="40%">00: 09:5b:29:3d:b3</TD></TR>
</table>
<TABLE width="100%" cellSpacing=2 border=0>
<TD width="60%"><b> Name (SSID)</b></TD>
<TD width="40%">nat chieland</TD></tr>
<TD width="60%"><b> Region</b></TD>
<TD width="40%">USA </TD></tr>
<TD width="60%"><b> Channel</b></TD>
<TD width="40%">1</TD></tr>

</table>
<TABLE width="100%" cellSpacing=2 border=0>

<tr>
<td colspan="2">
<img src="img/liteblue.gif" width="100%" height="2" border="0">
</td>
</tr>

<tr>
<td align='center'>
<input type="BUTTON" value="Show Statistics" onclick="window .open('mtenSysS tatistics.htm', 'static','width =500,height=200 ,status=yes, resizable=yes') ;">
<INPUT onclick="newwin dow(this.form); " type=button value="Connecti on Status">
</TD>
</tr>
</TABLE>
</form>
</body>

</html>
Jul 18 '05 #1
2 2352
> The program seems to take a vacation after the opening form tag. What
am I doing wrong?
<input type=hidden name=status value=>


I can't believe that this value=-thingy is valid html....

Regards,

Diez

Jul 18 '05 #2
Matthew Wilson wrote:
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.H TMLParser):

def handle_starttag (self, tag, data):
print "tag is %s." % (tag)

def handle_comment( self, data):
print "caught a comment: %s." % (data)

def handle_data(sel f, data):
if "IP" in data:
print "Caught %s." % data

hp = HP()
out = open('routersta tus.html')
for line in out:
hp.feed(line)
I figured that when I ran this on the html code at the bottom of this
file, it would print every tag, but instead, this is what I got:

tag is html.
tag is head.
tag is meta.
tag is meta.
tag is meta.
tag is meta.
tag is meta.
tag is title.
tag is link.
tag is script.
tag is body.
tag is form.

The program seems to take a vacation after the opening form tag. What
am I doing wrong?


Nothing, but your input file is not valid HTML and seems to puzzle the
parser. I recommend running it through tidy before you feed it to the
parser.

Peter
Jul 18 '05 #3

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

Similar topics

1
2497
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 something like (tag) then revert it back to <tag> via another function, I was wondering if there...
11
6062
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 entities. The sparse output breaks my ability to use entire columns and have entries correspond properly....
4
3335
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 error: Traceback (most recent call last): File "<stdin>", line 1, in ?
1
2747
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
2372
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 html document even if an invalid tag was found. Is it possible to do this ? Here is a little non...
1
1880
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; I don't seem to be getting the actual text in the HTML document. I've implemented the do_data...
1
1425
by: Just Another Victim of the Ambient Morality | last post by:
HTMLParser is behaving in, what I find to be, strange ways and I would like to better understand what it is doing and why. First, it doesn't appear to translate HTML escape characters. I don't know the actual terminology but things like &amp; don't get translated into & as one would like. Furthermore, not only does HTMLParser not translate it properly, it seems to omit it altogether! This prevents me from even doing the translation...
8
8562
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
3
2118
by: globalrev | last post by:
tried all kinds of combos to get this to work. http://docs.python.org/lib/module-HTMLParser.html from HTMLParser import HTMLParser class MyHTMLParser(HTMLParser):
0
8604
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8895
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6518
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
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
2
2330
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.