perl模块web :: scraper

时间:2013-02-01 10:44:13

标签: html perl web-scraping perl-module

我想从网页中仅为标题节点对象方法提取文本。具体的html部分如下:

<h2>Node Object Properties</h2>
<p>The &quot;DOM&quot; column indicates in which DOM Level the property was introduced.</p>

<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>

<h2>Node Object Methods</h2>
<p>The &quot;DOM&quot; column indicates in which DOM Level the method was introduced.</p>
<table class="reference">
<tr>
<th width="33%" align="left">Method</th>
<th width="61%" align="left">Description</th>
<th style="text-align:center;">DOM</th>
</tr>
<tr>
    <td><a href="met_node_appendchild.asp">appendChild()</a></td>
    <td>Adds a new child node, to the specified node, as the last child node</td>
    <td style="text-align:center;">1 </td>
</tr>
<tr>
    <td><a href="met_node_clonenode.asp">cloneNode()</a></td>
    <td>Clones a node</td>
    <td style="text-align:center;">1 </td>
</tr>
<tr>
    <td><a href="met_node_comparedocumentposition.asp">compareDocumentPosition()</a></td>
    <td>Compares the document position of two nodes</td>
    <td style="text-align:center;">1 </td>
</tr>
<tr>
    <td>getFeature(<span class="parameter">feature</span>,<span class="parameter">version</span>)</td>
    <td>Returns a DOM object which implements the specialized APIs 
    of the specified feature and version</td>
    <td style="text-align:center;">3 </td>
</tr>
<tr>
    <td>getUserData(<span class="parameter">key</span>)</td>
    <td>Returns the object associated to a key on a this node. The 
    object must first have been set to this node by calling setUserData with the 
    same key</td>
    <td style="text-align:center;">3 </td>
</tr>
<tr>
    <td><a href="met_node_hasattributes.asp">hasAttributes()</a></td>
    <td>Returns true if a node has any attributes, otherwise it 
    returns false</td>
    <td style="text-align:center;">2 </td>
</tr>
<tr>
    <td><a href="met_node_haschildnodes.asp">hasChildNodes()</a></td>
    <td>Returns true if a node has any child nodes, otherwise it 
    returns false</td>
    <td style="text-align:center;">1 </td>
</tr>
<tr>
    <td><a href="met_node_insertbefore.asp">insertBefore()</a></td>
    <td>Inserts a new child node before a specified, existing, child node</td>
    <td style="text-align:center;">1 </td>
</tr>
</table>

如果我写下以下内容在perl中:

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

for my $i (0 .. $#{$res2->{renners}}) {
  print $res2->{renners}[$i];
print "\n";
}

我得到所有标签的文本,即

attributes
baseURI
.
.
.
.
insertBefore()

wheras我只需要节点对象方法的标签文本i-e:

appendChild()
.
.
.
insertBefore()

我应该在代码中修改什么?请回复。

2 个答案:

答案 0 :(得分:0)

可能尝试匹配提取的文本中的函数括号。

if ($res2->{renners}[$i] =~ m!\(\)$! ){
    print $res2->{renners}[$i],"\n"
} 

这仅适用于节点对象函数附加括号的情况,即进程调用不会将它们删除,并且所有函数(您需要)都附加括号。

答案 1 :(得分:0)

process "table.reference > tr > td > a[contains(text(),'()')]"应该这样做。