Reputation: 49
I am trying to extract the text only from the html page and want to ignore or bypass the html escape signs "<" and ">". I am copying the part the html page that i used for extraction of text:
<table class="reference">
<tr>
<th align="left" width="25%">Tag</th>
<th align="left" width="75%">Description</th>
</tr>
<tr>
<td><a href="tag_comment.asp"><!--...--></a></td>
<td>Defines a comment</td>
</tr>
<tr>
<td><a href="tag_doctype.asp"><!DOCTYPE></a> </td>
<td>Defines the document type</td>
</tr>
<tr>
<td><a href="tag_a.asp"><a></a></td>
<td>Defines a hyperlink</td>
</tr>
<tr>
<td><a href="tag_abbr.asp"><abbr></a></td>
<td>Defines an abbreviation</td>
</tr>
<tr>
...
My perl code is:
my $urlToScrape = "http://www.w3schools.com/tags/";
# prepare data
my $teamsdata = scraper {
process "table.reference > tr > td > a ", 'tags[]' => 'TEXT';
process "table.reference > tr > td > a ", 'urls[]' => '@href';
};
# scrape the data
my $res = $teamsdata->scrape(URI->new($urlToScrape));
print "<HTML_tags>\n";
for my $i ( 0 .. $#{$res->{urls}}) {
print FILE " <tag_Name> $res->{tags}[$i] </tag_Name>\n ";
}
print "</HTML_tags>\n";
The output I get is the following:
<HTML_tags>
<tag_Name> <!--...--> </tag_Name>
<tag_Name> <!DOCTYPE> </tag_Name>
<tag_Name> <a> </tag_Name>
<tag_Name> <abbr> </tag_Name>
</HTML_tags>
whereas I want output as:
<HTML_tags>
<tag_Name> !--...-- </tag_Name>
<tag_Name> !DOCTYPE </tag_Name>
<tag_Name> a </tag_Name>
<tag_Name> abbr </tag_Name>
</HTML_tags>
Can anyone tell what do I have to change inorder to get the above output? Many Thanks.
Upvotes: 0
Views: 645
Reputation: 1342
Brute Force:
$res->{tags}[$i] =~ s/[\<\>]//gs; ## Added line
print FILE " <tag_Name> $res->{tags}[$i] </tag_Name>\n ";
Upvotes: 2