473,791 Members | 3,090 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Use C++ to parse HTML

Hi, guys. I am now developing an application
in which I need to fetch some html page, and then
parsing it to get some intended content in it.

Because HTML is not a standard XML format, so I
am curious about how should it be parsed?

Any help and suggestion will be appreciated very
much, thanks in advance!
Feb 6 '07 #1
9 15698
Bo Yang wrote:
Hi, guys. I am now developing an application
in which I need to fetch some html page, and then
parsing it to get some intended content in it.

Because HTML is not a standard XML format, so I
am curious about how should it be parsed?

Any help and suggestion will be appreciated very
much, thanks in advance!
Run it though HTML tidy first, that gives you clean XHTML to parse.

--
Ian Collins.
Feb 6 '07 #2
Bo Yang wrote:
Hi, guys. I am now developing an application
in which I need to fetch some html page, and then
parsing it to get some intended content in it.

Because HTML is not a standard XML format, so I
am curious about how should it be parsed?

Any help and suggestion will be appreciated very
much, thanks in advance!
The same way you'd parse anything else.
- Specify a grammar for what you want to parse.
- Split the input into a stream of tokens (I suggesting using flex or a
similar tool).
- Read in tokens until you can match a production in the grammar (I
suggest using bison or a similar tool).

If you don't have any previous experience with lexical analysis and
parsing then writing an HTML parser is probably too huge of a first
step. The book "Compilers: Principles, Techniques, and Tools" is the
place most people go to start learning about this stuff.

--
Alan Johnson
Feb 6 '07 #3
Alan Johnson wrote:
Bo Yang wrote:
>Hi, guys. I am now developing an application
in which I need to fetch some html page, and then
parsing it to get some intended content in it.

Because HTML is not a standard XML format, so I
am curious about how should it be parsed?

Any help and suggestion will be appreciated very
much, thanks in advance!


The same way you'd parse anything else.
- Specify a grammar for what you want to parse.
- Split the input into a stream of tokens (I suggesting using flex or a
similar tool).
- Read in tokens until you can match a production in the grammar (I
suggest using bison or a similar tool).

If you don't have any previous experience with lexical analysis and
parsing then writing an HTML parser is probably too huge of a first
step. The book "Compilers: Principles, Techniques, and Tools" is the
place most people go to start learning about this stuff.
That's easier said than done with HTML, it breaks a lot of the XML
grammar rules (optional closing tags) in conforming markup and there is
a lot of realy crap (frontpage) generated html out there which violates
HTML grammar.

I had the unfortunate task of having to parse some of this before I
discovered HTML tidy.

--
Ian Collins.
Feb 6 '07 #4
Ian Collins wrote:
Alan Johnson wrote:
>Bo Yang wrote:
>>Hi, guys. I am now developing an application
in which I need to fetch some html page, and then
parsing it to get some intended content in it.

Because HTML is not a standard XML format, so I
am curious about how should it be parsed?

Any help and suggestion will be appreciated very
much, thanks in advance!

The same way you'd parse anything else.
- Specify a grammar for what you want to parse.
- Split the input into a stream of tokens (I suggesting using flex or a
similar tool).
- Read in tokens until you can match a production in the grammar (I
suggest using bison or a similar tool).

If you don't have any previous experience with lexical analysis and
parsing then writing an HTML parser is probably too huge of a first
step. The book "Compilers: Principles, Techniques, and Tools" is the
place most people go to start learning about this stuff.
That's easier said than done with HTML, it breaks a lot of the XML
grammar rules (optional closing tags) in conforming markup and there is
a lot of realy crap (frontpage) generated html out there which violates
HTML grammar.

I had the unfortunate task of having to parse some of this before I
discovered HTML tidy.
My view on that is that if it can't be parsed using the HTML grammar,
then it, by definition, is not HTML. :-)

Of course being pragmatic one typically realizes that what they REALLY
want is something that is almost, but not entirely unlike an HTML
parser. Still, I think trying to first define a grammar for whatever it
is you really want to parse (this thing that is sort of like HTML) is
still the best approach. But then, I've never tried it myself.

--
Alan Johnson
Feb 6 '07 #5
Alan Johnson wrote:
Ian Collins wrote:
>Alan Johnson wrote:
>>The same way you'd parse anything else.
- Specify a grammar for what you want to parse.
- Split the input into a stream of tokens (I suggesting using flex or a
similar tool).
- Read in tokens until you can match a production in the grammar (I
suggest using bison or a similar tool).

If you don't have any previous experience with lexical analysis and
parsing then writing an HTML parser is probably too huge of a first
step. The book "Compilers: Principles, Techniques, and Tools" is the
place most people go to start learning about this stuff.
That's easier said than done with HTML, it breaks a lot of the XML
grammar rules (optional closing tags) in conforming markup and there is
a lot of realy crap (frontpage) generated html out there which violates
HTML grammar.

I had the unfortunate task of having to parse some of this before I
discovered HTML tidy.

My view on that is that if it can't be parsed using the HTML grammar,
then it, by definition, is not HTML. :-)

Of course being pragmatic one typically realizes that what they REALLY
want is something that is almost, but not entirely unlike an HTML
parser. Still, I think trying to first define a grammar for whatever it
is you really want to parse (this thing that is sort of like HTML) is
still the best approach. But then, I've never tried it myself.
I agree with that, my suggestion to use HTML tidy is based on my own
unfortunate experiences with parsing almost, but not entirely unlike
HTML HTML!

The XML grammar used by XHTML is way easier to define and parse.

--
Ian Collins.
Feb 6 '07 #6
Hi, guys. I am now developing an application
in which I need to fetch some html page, and then
parsing it to get some intended content in it.

Because HTML is not a standard XML format, so I
am curious about how should it be parsed?
Take a look at boost:spirit. It's a parser generator that's pretty
easy to use once you got the idea.
Feb 6 '07 #7
On Feb 6, 3:00 am, Bo Yang <strug...@mail. nankai.edu.cnwr ote:
Hi, guys. I am now developing an application
in which I need to fetch some html page, and then
parsing it to get some intended content in it.

Because HTML is not a standard XML format, so I
am curious about how should it be parsed?

Any help and suggestion will be appreciated very
much, thanks in advance!

If tidy can convert this to xml, use it. Then use the apache software
foundation sex(sax) parser to do the parsing. Spend your time having
fun

Feb 6 '07 #8
On 6 Lut, 21:37, "sasoon" <alexander.geor g...@gmail.comw rote:
On Feb 6, 3:00 am, Bo Yang <strug...@mail. nankai.edu.cnwr ote:
Hi, guys. I am now developing an application
in which I need to fetch some html page, and then
parsingit to get some intended content in it.
Because HTML is not a standard XML format, so I
am curious about how should it be parsed?
Any help and suggestion will be appreciated very
much, thanks in advance!

If tidy can convert this to xml, use it. Then use the apache software
foundation sex(sax) parser to do theparsing. Spend your time having
fun

See how Visual BNF tool parses code at www.intralogic.eu

Mar 20 '07 #9
On 6 fév, 07:00, Alan Johnson <a...@yahoo.com wrote:
Ian Collins wrote:
Alan Johnson wrote:
Bo Yang wrote:
>Hi, guys. I am now developing an application
in which I need to fetch some html page, and then
parsing it to get some intended content in it.
>Because HTML is not a standard XML format, so I
am curious about how should it be parsed?
>Any help and suggestion will be appreciated very
much, thanks in advance!
The same way you'd parse anything else.
- Specify a grammar for what you want to parse.
- Split the input into a stream of tokens (I suggesting using flex or a
similar tool).
- Read in tokens until you can match a production in the grammar (I
suggest using bison or a similar tool).
If you don't have any previous experience with lexical analysis and
parsing then writing an HTML parser is probably too huge of a first
step. The book "Compilers: Principles, Techniques, and Tools" is the
place most people go to start learning about this stuff.
That's easier said than done with HTML, it breaks a lot of the XML
grammar rules (optional closing tags) in conforming markup and there is
a lot of realy crap (frontpage) generated html out there which violates
HTML grammar.
I had the unfortunate task of having to parse some of this before I
discovered HTML tidy.

My view on that is that if it can't be parsed using the HTML grammar,
then it, by definition, is not HTML. :-)
That's not totally true: if it can't be parsed using the HTML grammar,
then it's not really HTML but you have to treat it as HTML anyway. So
you'll have to add the missing </tabletags and so on. This is why
HTML is such a nightmare, because your parser should try to understand
the HTML to the best of its ability, often by trying to understand
what the HTML writer wanted to do.
Of course being pragmatic one typically realizes that what they REALLY
want is something that is almost, but not entirely unlike an HTML
parser. Still, I think trying to first define a grammar for whatever it
is you really want to parse (this thing that is sort of like HTML) is
still the best approach. But then, I've never tried it myself.

--
Alan Johnson
Mar 20 '07 #10

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

Similar topics

23
2592
by: Charles Law | last post by:
Does anyone have a regex pattern to parse HTML from a stream? I have a well structured file, where each line is of the form <sometag someattribute='attr'>text</sometag> for example <SPAN CLASS='myclass'>A bit of text</SPAN>, or Just some text, without tags
13
4245
by: DH | last post by:
Hi, I'm trying to strip the html and other useless junk from a html page.. Id like to create something like an automated text editor, where it takes the keywords from a txt file and removes them from the html page (replace the words in the html page with blank space) I'm new to python and could use a little push in the right direction, any ideas on how to implement this? Thanks!
0
2277
by: dodoG | last post by:
Hello, How to parse html file tags <h1>-<h4> to excel file with c#. 1. How i create excel 2007 object . 2. What should i do in visual studio 2005 (which references in com object). 3. How i create from excel 2007 object excel file. 4. How i doing parser.
0
9669
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
9515
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
10427
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
10207
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
9995
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
9029
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
5431
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...
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2916
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.