473,651 Members | 2,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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 1493
numberwhun
3,509 Recognized Expert Moderator Specialist
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
11912
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 HTML::Parser itself can somehow preform the filtering, but how do I get it back as a string? I'd appreciate some sample code if anyone has any. Sorry if this is a real n00b question. Thanks a lot, Mitchua
0
2715
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 program is given below. The HTML is in a string to keep the example simple. The same problem appears with HTML in a file.
3
3120
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 (/usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi/HTML/) doesn't seem to contain the "real" parsing statements.
6
2831
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
3133
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 the html file. How to go about it?
0
1232
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 because I am doing something wrong). As it is said in http://xmlsoft.org/encoding.html the parser should detect the encoding. So I tested it putting an utf-8 word in a file and it does not detect it (it generates a wrong string). Example:
6
3083
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 formatter >>> parser=htmllib.HTMLParser(formatter.NullFormatter()) >>> parser.feed(html)
2
2660
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 use javascript or Jscript to work with the HTML code in the text string, in the same way that one works with XML code in a text string using the XML parser. If I was able to load the text string containing the HTML code succesfully, I would be...
2
1583
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 iterate through the entire DOM, building up a DOM path, but the stdlib parsers aren't calling handle_endtag() for any implicitly closed tags. I looked at BeautifulSoup, but it only seems to work by first parsing the entire document, then allowing...
0
8273
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
8693
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...
1
8456
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
7293
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
6156
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
5603
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
2694
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
1
1904
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1584
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.