473,587 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A question about HTML::Parser.

3 New Member
I am sorry to bother again.
What I want to do is to input a htm file, then delete all the tables, strange html language symbol, and return a plain text.
What the code below did is:
It deletes all the html tags, like <TABLE>, <\Table>, <..> and etc, but it did not delete anything between <TABLE> and <\TABLE>, and there are some symbols like, &nbsp, $#151 and etc.
Please help me modify the file to do the job. Thanks very much!
Expand|Select|Wrap|Line Numbers
  1.        #!/usr/bin/perl -w
  2.        use strict;
  3.        use HTML::Parser;
  4.        # define the subclass
  5.        package IdentityParse;
  6.        use base "HTML::Parser";
  7.  
  8.        my @processed_html;
  9.  
  10.        sub text {
  11.           my ($self, $text) = @_;
  12.           # print out the text
  13.           push(@processed_html, $text);
  14.       }
  15.  
  16.       sub comment {
  17.       }
  18.  
  19.       sub start {
  20.           my $self = shift;
  21.           $self->{table_seen}++ if $_[0] eq  "table" ;
  22.           $self->SUPER::start(@_);
  23.  
  24.       }
  25.  
  26.       sub end {
  27.           my $self = shift;
  28.           $self->SUPER::end(@_);
  29.           $self->{table_seen}-- if $_[0] eq "table";
  30.  
  31.       }
  32.       sub output
  33.          {
  34.          my $self = shift;
  35.          unless ($self->{table_seen}) {
  36.                    $self->SUPER::output(@_);
  37.                 }
  38.          }
  39.  
  40.       my $p = new IdentityParse;
  41.       $p->parse_file("H:/Test Data/wmt2004.htm");
  42.       open OUT, '>', "H:/Test Data/text1-processed.txt" or die;
  43.       print OUT @processed_html;
  44.       close OUT;
  45.  
Jun 9 '10 #1
2 1899
kleach
15 New Member
@zhengmath
This will remove everything in tables.

Expand|Select|Wrap|Line Numbers
  1. while (<STDIN>) { $l.=$_; } $_=$l;
  2. $_=$l;
  3. while (/<table/is)
  4.  { s/(.*)<table.*?>.*?<\/table>/$1/igs; }
  5.  
  6. print ;
  7.  
The conversion of HTML::Entities to text is harder as many don't have text equiv. If you want to roll your own mapping many to a single character, just use s///.
Jun 10 '10 #2
chorny
80 Recognized Expert New Member
IMHO, XPath is better suited for such job.
Jun 11 '10 #3

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

Similar topics

1
11910
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. ...
0
2713
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
3118
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.
3
7726
by: Mark | last post by:
Hi, I am using a program that is ultra paranoid about start and end html tags. For example <p>This is a test <br>A new line The above code causes the program to fail
14
3132
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?
2
2548
by: Craig Kenisston | last post by:
Hi, Could someone recommend any decedent html parser ? All what I need to do is to extract the links from a given page. I've found several tools in codeproject but all seems to be a bit inmature, yet. I'd like to hear opinions and personal experiences with any c# html parser. Thanks in advance.
0
1230
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...
3
685
by: SEGACO | last post by:
Hi, Can someone tell me if C# includes something to parse HTML? Thanks.
2
2657
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...
5
7314
by: Johannes Bauer | last post by:
Hello group, I'm trying to use a htmllib.HTMLParser derivate class to parse a website which I fetched via httplib.HTTPConnection().request().getresponse().read(). Now the problem is: As soon as I pass the htmllib.HTMLParser UTF-8 code, it chokes. The code is something like this: prs = self.parserclass(formatter.NullFormatter())...
0
7918
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...
0
7843
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...
0
8206
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. ...
1
7967
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...
1
5713
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...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2353
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
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
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...

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.