仅从页面上的特定表格中选择WWW ::机械化和CSS选择器?

问题描述:

美好的一天,仅从页面上的特定表格中选择WWW ::机械化和CSS选择器?

我刮了一些页面,显示我需要在表中的数据。在页面上有多个表与以下内容:

<table class="dTable" cellspacing="1" cellpadding="1" border="0"> 

我想刮在表格单元格中的项目:

<td class="dCell" align="right"> 

有相同类别的网页上不幸的是许多细胞。此外,某些页面包含额外的dCells以获取更多信息。因此,指定特定的细胞形式的脚本:

my @thing = $mech->selector('td.dCell'); 

my $val = $thing[14]->text(); 

会给出不同的页面不同的结果,即。我不会得到我想要的所有时间。

所以,作为一个部分解决方案,我认为这将是最好的,从特定表中选择。

my @table = $mech->selector('table.dTable'); 

my @required = $table[3]->selector('td.dCell'); 

#the info is in the third dTable on the page 

#the third table does not contain changing data, ie. I can use required[1] and it will be the same all of the time. 

我尝试这样做,这是行不通的,接收到的错误:

MozRepl :: RemoteObject的::对象已在以下行无功能选择:

my @required = $table[3]->selector('td.dCell'); 

所以在这一点我卡住了。我感谢所有的协助。

您需要使用node选项selector的:

my @required = $mech->selector('td.dCell', { node => ... }); 

但是,为什么你不使用XPath?

my @required = $mech->xpath('//table[@class="dTable"][3]//td[@class="dCell"]'); 
+0

感谢ganga,起初它没有写结果,但后来我将它改成了'[@ class =“dTable] [2]',它工作的有趣的是,在html中它是第3个'dTable'出现 – surfer190 2013-04-11 07:44:32

+0

@StephenH在计算机科学中,你从0开始计数而不是1所以第一台具有位置0,第二个表中有1位等 – e1che 2013-04-11 08:16:07

+0

你说得好像我是一个完整的傻瓜。我的意思说第四个'dTable'出现,所以它是一个关闭... – surfer190 2013-04-11 08:28:54