473,386 Members | 1,753 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,386 software developers and data experts.

HTML::Parser problem parsing special charcters in HTML file.

Hello All,

I am trying to extract text from the HTML using the following code,

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use HTML::Parser 3.00 ();
  3.  
  4. my %inside;
  5. my $p;
  6. sub tag
  7. {
  8. }
  9.  
  10. sub text
  11. {
  12.     return if $inside{script} || $inside{style};
  13.     print $_[0];
  14. }
  15.  
  16. open(my $fh, shift) || die;
  17.  
  18. $p = HTML::Parser->new(api_version => 3,
  19.           handlers    => [start => [\&tag, "tagname, '+1'"],
  20.                   end   => [\&tag, "tagname, '-1'"],
  21.                   text  => [\&text, "dtext"],
  22.                  ],
  23.           marked_sections => 1,
  24.     );
  25.  
  26. while(<$fh>)
  27. {
  28.     $p->parse($_) || die "Can't open file: $!\n";;
  29. }
It does so only falters when it finds the special characters like ’ or the likes. I wish to process these either to ignore them or replace them with some sensible ASCII chars. I tried the encode module it does not yield anything useful.

Any help is appreciated.

Regards,
Atul.
Mar 19 '08 #1
1 1481
numberwhun
3,509 Expert Mod 2GB
Hello All,

I am trying to extract text from the HTML using the following code,

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use HTML::Parser 3.00 ();
  3.  
  4. my %inside;
  5. my $p;
  6. sub tag
  7. {
  8. }
  9.  
  10. sub text
  11. {
  12.     return if $inside{script} || $inside{style};
  13.     print $_[0];
  14. }
  15.  
  16. open(my $fh, shift) || die;
  17.  
  18. $p = HTML::Parser->new(api_version => 3,
  19.           handlers    => [start => [\&tag, "tagname, '+1'"],
  20.                   end   => [\&tag, "tagname, '-1'"],
  21.                   text  => [\&text, "dtext"],
  22.                  ],
  23.           marked_sections => 1,
  24.     );
  25.  
  26. while(<$fh>)
  27. {
  28.     $p->parse($_) || die "Can't open file: $!\n";;
  29. }
It does so only falters when it finds the special characters like ’ or the likes. I wish to process these either to ignore them or replace them with some sensible ASCII chars. I tried the encode module it does not yield anything useful.

Any help is appreciated.

Regards,
Atul.

Sorry, I don't know the answer but hopefully one of our experts will be able to assist you.

My appologies for the delay in getting an answer to your question.

Regards,

Jeff
Apr 17 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Mitchua | last post by:
I am trying to use HTML::Parser to parse an HTML file, remove all HTML tags (including comments, etc.), replace all ENTITIES (e.g. &amp), and put the result into a variable as a string. I figure...
0
by: Himanshu Garg | last post by:
Hello, I am using HTML::Parser to extract text from html pages from http://bbc.co.uk/urdu/ However the encoding of the input text seems to change to some unknown encoding in the output. The...
3
by: Himanshu Garg | last post by:
Hello, I am trying to pinpoint an apparent bug in HTML::Parser. The encoding of the text seems to change incorrectly if the locale isn't set properly. However Parser.pm in the directory...
6
by: wilk | last post by:
Is anybody know here any class in .NET that would help me to parse html in C# ? Or maybe you can even tell me how to do it? -- -- -------------------------------------- Pozdrawiam WILK...
14
by: WUV999U | last post by:
Hi I am fairly familiar in C but not much. I want to know how I can write a html parser in C that only parses for the image file in the html file and display or print all the images found in...
0
by: icoba | last post by:
Hi, I am parsing html documents using the html parser from libxml2, and if the encoding is included in the document it works perfectly but if it is not, I think it does not work well (probably...
6
by: Sakcee | last post by:
html = '<html><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" <head></head> <body bgcolor=#ffffff>\r\n Foo foo , blah blah </body></html>' >>> import htmllib >>> import...
2
by: David Virgil Hobbs | last post by:
Loading text strings containing HTML code into an HTML parser in a Javascript/Jscript I would like to know, how one would go about loading a text string containing HTML code, so as to be able to...
2
by: Chris | last post by:
Can anyone recommend a good HTML/XHTML parser, similar to HTMLParser.HTMLParser or htmllib.HTMLParser, but able to intelligently know that certain tags, like <br>, are implicitly closed? I need to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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
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...

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.