473,748 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HTML File Parsing

I'm having problems parsing an HTML file with the following syntax :

<TABLE cellspacing=0 cellpadding=0 ALIGN=CENTER BORDER=1 width='100%'>
<TH BGCOLOR='#c0c0c 0' Width='3%'>User ID</TH>
<TH Width='10%' BGCOLOR='#c0c0c 0'>Name</TH><TH width='7%'
BGCOLOR='#c0c0c 0'>Date</TH>
and so on....

whenever I feed the parser with such file I get the error :

Traceback (most recent call last):
File "C:\Documen ts and Settings\Admini strator\My Documents\works pace
\thread\src\par ser.py", line 91, in <module>
p.parse(thechan ge)
File "C:\Documen ts and Settings\Admini strator\My Documents\works pace
\thread\src\par ser.py", line 16, in parse
self.feed(s)
File "C:\Python25\li b\HTMLParser.py ", line 110, in feed
self.goahead(0)
File "C:\Python25\li b\HTMLParser.py ", line 152, in goahead
k = self.parse_endt ag(i)
File "C:\Python25\li b\HTMLParser.py ", line 316, in parse_endtag
self.error("bad end tag: %r" % (rawdata[i:j],))
File "C:\Python25\li b\HTMLParser.py ", line 117, in error
raise HTMLParseError( message, self.getpos())
HTMLParser.HTML ParseError: bad end tag: "</TH BGCOLOR='#c0c0c 0'>", at
line 515, column 45

Googling around I've found a solution to a similar situation, over and
over again :
http://64.233.169.104/search?q=cache...ient=firefox-a

but coding :

you can disable proper parsing by setting the CDATA_CONTENT_E LEMENTS
attribute on the parser instance, before you start parsing. by
default, it is
set to
CDATA_CONTENT_E LEMENTS = ("script", "style")
setting it to an empty tuple disables HTML-compliant handling for
these
elements:
p = HTMLParser()
p.CDATA_CONTENT _ELEMENTS = ()
p.feed(f.read() )

didn't solve my problem. I've made a little modification then to
HTMLParser.py instead that solved the problem, as follows:
original: endtagfind = re.compile('</\s*([a-zA-Z][-.a-zA-Z0-9:_]*)?(.*)
\s*>')
my version : endtagfind = re.compile('</\s*([a-zA-Z][-.a-zA-Z0-9:_]*)
\s*>')

it worked ok for all the files I needed and also for a different file
I also parse using the same library. I know it might sound stupid but
I was just wondering if there's a better way of solving that problem
than just modifying the standard library. Any clue ?

thx in advance,
Felipe.
Oct 28 '08 #1
2 3613
Felipe De Bene wrote:
I'm having problems parsing an HTML file with the following syntax :

<TABLE cellspacing=0 cellpadding=0 ALIGN=CENTER BORDER=1 width='100%'>
<TH BGCOLOR='#c0c0c 0' Width='3%'>User ID</TH>
<TH Width='10%' BGCOLOR='#c0c0c 0'>Name</TH><TH width='7%'
BGCOLOR='#c0c0c 0'>Date</TH>
and so on....

whenever I feed the parser with such file I get the error :

HTMLParser.HTML ParseError: bad end tag: "</TH BGCOLOR='#c0c0c 0'>", at
line 515, column 45
Your HTML page is not HTML, i.e. it is broken. Python's HTMLParser is not made
for parsing broken HTML. However, you can use the parse of lxml.html to fix up
your HTML for you.

http://codespeak.net/lxml/

Stefan
Oct 28 '08 #2
On Oct 28, 6:18 pm, Stefan Behnel <stefan...@behn el.dewrote:
Felipe De Bene wrote:
I'm having problems parsing anHTMLfile with the following syntax :
<TABLE cellspacing=0 cellpadding=0 ALIGN=CENTER BORDER=1 width='100%'>
<TH BGCOLOR='#c0c0c 0' Width='3%'>User ID</TH>
<TH Width='10%' BGCOLOR='#c0c0c 0'>Name</TH><TH width='7%'
BGCOLOR='#c0c0c 0'>Date</TH>
and so on....
whenever I feed the parser with such file I get the error :
HTMLParser.HTML ParseError: bad end tag: "</TH BGCOLOR='#c0c0c 0'>", at
line 515, column 45

YourHTMLpage is notHTML, i.e. it is broken. Python's HTMLParser is not made
for parsing brokenHTML. However, you can use the parse of lxml.htmlto fix up
yourHTMLfor you.

http://codespeak.net/lxml/

Stefan
Actually i fetch from an application that i thought it should act like
this and as I told you, the program is ready to be shipped so
rewriting an entire class that has public methods would be a real
pain. I really had to find a way to work this out by using the
python's parser instead of external libraries. But thanks anyway for
the clue, I might start working on a similar project next and this
library may be a good and a less painful path. Thanks :D
Felipe.

Oct 30 '08 #3

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

Similar topics

5
2067
by: Cliff Roman | last post by:
I have a league for a game where we get exports after every session in HTML format It is broken down into 3 sections and each section has a Table with the results Right now I have to create 3 csv files manually out of it I would like to learn how to create a PHP script that can parse the data for me out of the html results and create the csv files that I need
1
1768
by: Raja Balaji R | last post by:
hi, like xml dom and xmlreader used for working with xml files. how can i do this with html files. i want find all the hyperlink tags <a> and change the href attribute to my own value. Thanks & Regards
1
2462
by: Christoph Bisping | last post by:
Hello! Maybe someone is able to give me a little hint on this: I've written a vb.net app which is mainly an interpreter for specialized CAD/CAM files. These files mainly contain simple movement and drawing instructions like "move to's" and "change color's" optionally followed by one or more numeric (int or float) arguments. My problem is that the parsing algorithm I've currently implemented is extremely slow.
2
2066
by: Robin | last post by:
Hello I need to read the rendered text of a html file. Does anybody know how to do that? Thanks
2
1957
by: Xavier | last post by:
Hi, I've just download Xerces2 Java and I'd like to parse an HTML file using the HTMLDOMImplementation found in the org.apache.html.dom package. First I try : DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
14
7350
by: Rob Meade | last post by:
Hi all, I'm working on a project where there are just under 1300 course files, these are HTML files - my problem is that I need to do more with the content of these pages - and the thought of writing 1300 asp pages to deal with this doesn't thrill me. The HTML pages are provided by a training company. They seem to be "structured" to some degree, but I'm not sure how easy its going to be to parse the page.
5
2333
by: moddster | last post by:
Hi Guys. I am a newbie to perl and need some help with a problem. PROBLEM: I have to parse an HTML file and get rid of all the HTML tags and count the number of sumbissions a person has through out the dates found. The condition is that multiple submissions by the same person on the same date is counted as 1. I have already gotten rid of the HTML tags using: #!/usr/bin/perl -w use strict;
1
1432
by: Shriphani | last post by:
Hello, I have a html file over here by the name guide_ind.html and it contains links to other html files like guides.html#outline . How do I point BeautifulSoup (I want to use this module) to guides.html#outline ? Thanks Shriphani P.
29
2148
by: lenbell | last post by:
It's old stupid and lazy here again I have been wanting to keep using my WYSIWYG (What You See Is What You Get - for my fellow stupids) html editor. But I was told that you HAD to rename your files to .PHP so they would be parsed correctly. Oh contraire if you are hosted by Apache and have some access to the .htaccess file mechanism In my case through the "cPanel" and then "Apache Handlers"
0
8831
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,...
0
9552
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9326
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
8245
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
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
6076
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();...
1
3315
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
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
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.