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

Home Posts Topics Members FAQ

HTML parsing bug?

Python 2.3.5 seems to choke when trying to parse html files, because it
doesn't realize that what's inside <!-- --> is a comment in HTML,
even if this comment is inside <script> </script>, especially if it's a
comment inside that script code too.

The html file:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><ti tle>Choke on this</title>
<script language="JavaS cript">
<!--
// </ht ml> - this is a comment in JavaScript, which is itself inside
an HTML comment
-->
</script>
</head>
<body>
Hey there
</body>
</html>
The Python program:

from urllib2 import urlopen
from HTMLParser import HTMLParser
f = urlopen("file:///PATH_TO_THE_ABO VE/index.html")
p = HTMLParser()
p.feed(f.read() )

Jan 30 '06 #1
6 1529
G.
> // </ht ml> - this is a comment in JavaScript, which is itself inside
an HTML comment


This is supposed to be one line. Got wrapped during posting.

Jan 30 '06 #2

<g_************ **@yahoo.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
Python 2.3.5 seems to choke when trying to parse html files, because it
doesn't realize that what's inside <!-- --> is a comment in HTML,
even if this comment is inside <script> </script>, especially if it's a
comment inside that script code too.


Actually, you are technically incorrect; try validating the code you posted.
Google found this explanation: http://lachy.id.au/log/2005/05/script-comments
Feeding even slightly invalid HTML to the standard library parser will often
choke it. If you can't guarantee clean sources, best use Tidy first or another
parser entirely.

Jan 30 '06 #3
> this is a comment in JavaScript, which is itself inside an HTML comment

Don't nest HTML comments. Occasionaly it may break the browsers as
well.

(I remember this from one of the weirdest of bughunts : whenever the
number of characters between nested HTML comments was divisible by four
the page would render incorrectly ... or something of that sorts)

i.

Jan 30 '06 #4
"Istvan Albert" <is***********@ gmail.com> wrote:
this is a comment in JavaScript, which is itself inside an HTML comment


Don't nest HTML comments. Occasionaly it may break the browsers as
well.


Did you read the post? He didn't nest HTML comments. He put a Javascript
comment inside an HTML comment, inside a <script></script> pair. Virtually
every page with Javascript does exactly the same thing.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Feb 1 '06 #5
g_************* *@yahoo.com wrote:
Python 2.3.5 seems to choke when trying to parse html files, because it
doesn't realize that what's inside <!-- --> is a comment in HTML,
even if this comment is inside <script> </script>, especially if it's a
comment inside that script code too.


nope. what's inside <!-- --> is not a comment if it's inside a <script>
or <style> tag. read the spec:

http://www.w3.org/TR/REC-html40/types.html#type-cdata

"Although the STYLE and SCRIPT elements use CDATA for their data
model, for these elements, CDATA must be handled differently by
user agents. Markup and entities must be treated as raw text and
passed to the application as is. The first occurrence of the
character sequence "</" (end-tag open delimiter) is treated as
terminating the end of the element's content. In valid documents,
this would be the end tag for the element."

in your case, the first occurrence of "</" is not the end tag.

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() )

</F>

Feb 1 '06 #6
>> this is a comment in JavaScript, which is itself inside an HTML comment
Did you read the post?


misread it rather ...

Feb 2 '06 #7

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

Similar topics

16
2892
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed loaded into cache, the slideshow doesn't look very nice. I am not sure how/when to call the slideshow() function to make sure it starts after the preload has been completed.
82
6310
by: Eric Lindsay | last post by:
I have been trying to get a better understanding of simple HTML, but I am finding conflicting information is very common. Not only that, even in what seemed elementary and without any possibility of getting wrong it seems I am on very shaky ground . For example, pretty much every book and web course on html that I have read tells me I must include <html>, <head> and <body> tag pairs. I have always done that, and never questioned it. ...
1
1849
by: anagai | last post by:
Im wondering if generating html objects such as tabels and rows in javascript is faster than typing the html directly? Seems when you do it in javascript you have to download alot of code and would slow down displaying the page. while if you just type the html, it requires less bandwidth and display faster? is parsing html to display in browser slower than doing it through dom to display the same html objects on the page?
59
6997
by: Lennart Björk | last post by:
Hi All, I have a tiny program: <!doctype HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>MyTitle</title> <meta http-equiv="Content-Type" content="text/html;
28
2574
by: Timothy Larson | last post by:
A couple years ago it seemed like XHTML was the direction of most web markup, a foregone conclusion. Now I return to the scene and I see many here recommending that authors stick to HTML, albeit in strict mode. What happened? Why the shift? What's the problem with XHTML that has everyone saying it be avoided in most cases? Can I take it that this group seems to think HTML5 is going to be the future direction of the web rather than...
1
2425
by: yonido | last post by:
hello, my goal is to get patterns out of email files - say "message forwarding" patterns (message forwarded from: xx to: yy subject: zz) now lets say there are tons of these patterns (by gmail, outlook, etc) - and i want to create some rules of how to get them out of the mail's html body. so at first i tried using regular expressions: for example - "any pattern that starts with a <p> and contains "from:"..." etc.
4
4859
by: Rick Walsh | last post by:
I have an HTML table in the following format: <table> <tr><td>Header 1</td><td>Header 2</td></tr> <tr><td>1</td><td>2</td></tr> <tr><td>3</td><td>4</td></tr> <tr><td>5</td><td>6</td></tr> </table> With an XSLT styles sheet, I can use for-each to grab the values in
4
6831
by: Neil.Smith | last post by:
I can't seem to find any references to this, but here goes: In there anyway to parse an html/aspx file within an asp.net application to gather a collection of controls in the file. For instance what I'm trying to do is upload a html file onto the web server, convert it to aspx file and then parse it for input tags/controls, which in turn will become fields in a newly created database table. Clearly when the aspx file is called the...
22
1857
by: John | last post by:
Hello, I have a php include command in my website and the script shows up. However, I need for the script to show up on the right side of page (there is enough room there)... but for some reason it shows up at the bottom. I placed the include command in the correct part of my html file. Is there any way to give some sort of command on where you want
11
4573
by: Tim Arnold | last post by:
hi, I've got lots of xhtml pages that need to be fed to MS HTML Workshop to create CHM files. That application really hates xhtml, so I need to convert self-ending tags (e.g. <br />) to plain html (e.g. <br>). Seems simple enough, but I'm having some trouble with it. regexps trip up because I also have to take into account 'img', 'meta', 'link' tags, not just the simple 'br' and 'hr' tags. Well, maybe there's a simple way to do that...
0
8683
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
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
9170
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...
0
9031
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8871
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...
0
7739
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...
0
5862
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
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
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

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.