【问题标题】:extracting specific information using web::scraper in perl在 perl 中使用 web::scraper 提取特定信息
【发布时间】:2013-01-31 11:07:01
【问题描述】:

我是网络抓取的新手。我想知道是否可以在 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

为了获得上述输出,我必须进行哪些更改?

【问题讨论】:

    标签: perl


    【解决方案1】:

    通过调整 CSS 选择器告诉它你只想要第二个 td;而不是"table.reference &gt; tr &gt; td""table.reference &gt; tr &gt; td:nth-of-type(2)"

    【讨论】:

    • (经过一点 RTFS,我发现 nth-of-type 在 Web::Scraper 中比 nth-child 效率更高,因为它将 CSS 转换为 XPath。)
    • 我正要写一个nth-child 解决方案,完全没有意识到性能影响。很棒的发现!
    • 是否可以从上面的代码中得到如下输出:` attributesReturns a collection of a node's attributes1 DOMVersion>`
    • 是否可以从上面的代码中得到如下输出:&lt;Name&gt;attributes&lt;/Name&gt; &lt;ReturnValue&gt;Returns a collection of a node's attributes&lt;/ReturnValue&gt; &lt;DOMVersion&gt;1&lt;/DOMVersion&gt;
    猜你喜欢
    • 2011-09-21
    • 2011-02-19
    • 2015-04-10
    • 2021-03-04
    • 1970-01-01
    • 1970-01-01
    • 2014-11-23
    • 2019-05-09
    • 1970-01-01
    相关资源
    最近更新 更多