1

是否可以使用 perl 中的 web::scraper 从网页输出 xml。例如,我的 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 > a", 'renners[]' => 'TEXT';
 process "table.reference > tr > td:nth-of-type(2)", 'landrenner[]' => 'TEXT';
  process "table.reference > tr > td:nth-of-type(3)", 'dom[]' => 'TEXT';
     };

    my $res = $teamsdata->scrape(URI->new($urlToScrape));

for my $i (0 .. $#{$res->{renners}}) {
    print "<PropertyList>\n";
print "<Property>\n";
print "<Name> ";
    print $res->{renners}[$i];
    print "\n";
print "</Name>";
 print "\n";
print "</Property>\n";
print "</PropertyList>\n";
    }

for my $j (0 .. $#{$res->{landrenner}}) {
    print "<ReturnValue>\n";
    print $res->{landrenner}[$j];
    print "\n";
 print "</ReturnValue>\n";
    }

for my $k (0 .. $#{$res->{dom}}) {
    print "<domversion>\n";
    print $res->{dom}[$k];
    print "\n";
 print "</domversion>\n";
    }

当我运行上面的代码时,我得到的所有输出如下:

<PropertyList>
<Property>
<Name>attributes</Name>
<Property>
<PropertyList>
<PropertyList>
<Property>
<Name>baseURI</Name>
<Property>
<PropertyList>
...
<ReturnValue>
Returns a collection of a node's attributes
</ReturnValue>
....
<domversion>
1
</domversion>
....

是否有可能得到如下输出:

<PropertyList>
<Property>
<Name>attributes</Name>
<ReturnValue>Returns a collection of a node's attributes</ReturnValue>
<DOMVersion>1</DOMVersion>
</Property> 
</PropertyList>

如何组合上述三个 forloop 以获得上述输出?

非常感谢

4

3 回答 3

1

我意识到这不是您正在寻找的,但请看一下HTML::Element。它有一个as_XML方法,可用于将 HTML 树转换为 XML。HTH。

于 2013-01-31T07:30:43.810 回答
1

您只需要将输出移动到第一个for循环中。由于您在 中的三个键中的每一个中都有相同数量的项目$res,因此您可以使用$i来访问所有单个项目。您将通过迭代从 中获得属于彼此的三个值$i

for my $i (0 .. $#{$res->{renners}}) {
  print <<"XML";
<PropertyList>
  <Property>
    <Name>$res->{renners}[$i]</Name>
    <ReturnValue>$res->{landrenner}[$i]</ReturnValue>
    <domversion>$res->{dom}[$i]</domversion>
  </Property>
</PropertyList>
XML
}

我将print语句更改为使用HERE 文档,因为它更易于阅读。我还将行更改my $res = $teamsdata->scrape(URI->new($urlToScrape));my $res = $rennersdata->scrape(URI->new($urlToScrape));因为$teamsdata未声明。

于 2013-01-31T12:51:14.170 回答
0

试试这个:只需重新排列打印语句:

print "<PropertyList>\n";   
for my $i (0 .. $#{$res->{renners}}) {    
    print "<Property>\n";
    print "<Name> ";
        print $res->{renners}[$i]; print "\n";
    print "</Name>"; print "\n";

    for my $j (0 .. $#{$res->{landrenner}}) {
        print "<ReturnValue>\n";
            print $res->{landrenner}[$j];print "\n";
        print "</ReturnValue>\n";
    }
    for my $k (0 .. $#{$res->{dom}}) {
        print "<domversion>\n";
            print $res->{dom}[$k];print "\n";
        print "</domversion>\n";
    }   
    print "</Property>\n";
}
print "</PropertyList>\n";
于 2013-01-31T12:50:01.947 回答