473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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 15672
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.cnwrote:
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.georg...@gmail.comwrote:
On Feb 6, 3:00 am, Bo Yang <strug...@mail.nankai.edu.cnwrote:
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.comwrote:
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
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...
13
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...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
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,...

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.