perl - 在 Perl 中使用 web::scraper 提取特定信息

标签 perl

我是网络抓取新手。我想知道是否可以使用 perl 中的 web::scraper 提取特定信息。例如,我的 html 如下所示(我从 URL 中获取了 html 的一部分):

 <table class="reference">
     <tr>
     <th width="23%" align="left">Property</th>
     <th width="71%" align="left">Description</th>
     <th style="text-align:center;">DOM</th>
     </tr>
     <tr>
        <td><a href="prop_node_attributes.asp">attributes</a></td>
        <td>Returns a collection of a node's attributes</td>
        <td style="text-align:center;">1</td>
     </tr>

     <tr>
        <td><a href="prop_node_baseuri.asp">baseURI</a></td>
        <td>Returns the absolute base URI of a node</td>
        <td style="text-align:center;">3</td>
     </tr>
     <tr>
        <td><a href="prop_node_childnodes.asp">childNodes</a></td>
        <td>Returns a NodeList of child nodes for a node</td>
        <td style="text-align:center;">1</td>
     </tr>
     <tr>
        <td><a href="prop_node_firstchild.asp">firstChild</a></td>
        <td>Returns the first child of a node</td>
        <td style="text-align:center;">1</td>
     </tr>
     <tr>
        <td><a href="prop_node_lastchild.asp">lastChild</a></td>
        <td>Returns the last child of a node</td>
        <td style="text-align:center;">1</td>
     </tr>
     <tr>
        <td><a href="prop_node_localname.asp">localName</a></td>
        <td>Returns the local part of the name of a node</td>
        <td style="text-align:center;">2</td>
     </tr>
     <tr>
        <td><a href="prop_node_namespaceuri.asp">namespaceURI</a></td>
        <td>Returns the namespace URI of a node</td>
        <td style="text-align:center;">2</td>
     </tr>
     <tr>
        <td><a href="prop_node_nextsibling.asp">nextSibling</a></td>
        <td>Returns the next node at the same node tree level</td>
        <td style="text-align:center;">1</td>
     </tr>
     <tr>
        <td><a href="prop_node_nodename.asp">nodeName</a></td>
        <td>Returns the name of a node, depending on its type</td>
        <td style="text-align:center;">1</td>
     </tr>
     <tr>
        <td><a href="prop_node_nodetype.asp">nodeType</a></td>
        <td>Returns the type of a node</td>
        <td style="text-align:center;">1</td>
     </tr>
     <tr>
        <td><a href="prop_node_nodevalue.asp">nodeValue</a></td>
        <td>Sets or returns the value of a node, depending on its 
        type</td>
        <td style="text-align:center;">1</td>
     </tr>
     <tr>
        <td><a href="prop_node_ownerdocument.asp">ownerDocument</a></td>
        <td>Returns the root element (document object) for a node</td>
        <td style="text-align:center;">2</td>
     </tr>
     <tr>
        <td><a href="prop_node_parentnode.asp">parentNode</a></td>
        <td>Returns the parent node of a node</td>
        <td style="text-align:center;">1</td>
     </tr>
     <tr>
        <td><a href="prop_node_prefix.asp">prefix</a></td>
        <td>Sets or returns the namespace prefix of a node</td>
        <td style="text-align:center;">2</td>
     </tr>
     <tr>
        <td><a href="prop_node_previoussibling.asp">previousSibling</a></td>
        <td>Returns the previous node at the same node tree level</td>
        <td style="text-align:center;">1</td>
     </tr>
     <tr>
        <td><a href="prop_node_textcontent.asp">textContent</a></td>
        <td>Sets or returns the textual content of a node and its 
        descendants</td>
        <td style="text-align:center;">3</td>
     </tr>
     </table>

所以我的 Perl 代码如下:

#!/usr/bin/perl
use warnings;
use strict;
use URI;
use Web::Scraper;

# website to scrape
my $urlToScrape = "http://www.w3schools.com/jsref/dom_obj_node.asp";

my $rennersdata = scraper {
 process "table.reference > tr > td", 'landrenner[]' => 'TEXT';
 };

my $res = $teamsdata->scrape(URI->new($urlToScrape));
for my $i (0 .. $#{$res->{landrenner}}) {

print $res->{landrenner}[$i];
print "\n";
}

当我运行上面的代码时,我得到了 td 标记内的所有文本。 i-e 对于 $i=0,输出为:

attributes
Returns a collection of a node's attributes
1

有没有可能我只能得到输出:

Returns a collection of a node's attributes

为了获得上述输出,我必须更改什么?

最佳答案

通过调整 CSS 选择器告诉它您只需要第二个 td;而不是 "table.reference > tr > td" 执行 "table.reference > tr > td:nth-of-type(2)"

关于perl - 在 Perl 中使用 web::scraper 提取特定信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14617374/

相关文章:

perl - 在 Ksh 和 Perl 中并行执行任务的最佳方式是什么?

perl - 在较新的 Getopt::Long 如何设置默认可选值

windows - Perl:获取二进制支持的最低操作系统

perl - 在 Perl 中, ||运算符(operator)工作?

linux - 解压文件而不使用第一个目录

regex - 为什么再添加一种替代方法会使我的正则表达式慢600倍?

perl - 如何在不使用 Perl 中的附加库的情况下发送电子邮件附件?

java - 我如何从 Java 调用 Perl?

perl - 从 Perl 脚本内部在 alpine linux 上的 zsh 上获取 `Can' t exec "command"` 错误

shell - SED 或 AWK 全部替换为另一个文件中的模式