473,513 Members | 2,428 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 1896
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
11906
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
2711
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
3108
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...
3
7712
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
3118
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...
2
2540
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...
0
1226
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...
3
685
by: SEGACO | last post by:
Hi, Can someone tell me if C# includes something to parse HTML? Thanks.
2
2650
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...
5
7304
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...
0
7260
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
7160
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...
1
7099
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
7525
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
5685
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,...
1
5086
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...
0
4746
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...
0
3233
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...
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.