DOMCrawler未正确转储数据以进行分析


DOMCrawler not dumping data properly for parsing

我正在使用Symfony、Goutte和DOMCrawler来抓取页面。不幸的是,这个页面有许多老式的数据表,没有ID、类或识别因素。因此,我试图通过解析从请求中返回的源代码来找到一个表,但我似乎无法访问任何信息

我认为当我尝试过滤它时,它只过滤第一个节点,而这不是我想要的数据所在的位置,所以它什么都不返回。

所以我有一个CCD_ 1对象。我试着循环浏览以下内容以获得我想要的内容:

$title = $crawler->filterXPath('//td[. = "Title"]/following-sibling::td[1]')->each(funtion (Crawler $node, $i) {
        return $node->text();
});

我不知道Crawler $node是什么,我只是从网页上的例子中得到的。如果我能做到这一点,那么它将循环通过$crawler对象中的每个节点,并找到我真正想要的东西。

下面是一个页面示例:

<table> 
<tr>
    <td>Title</td>
    <td>The Harsh Face of Mother Nature</td>
   <td>The Harsh Face of Mother Nature</td>
</tr>
.
.
.
</table>

这只是一张桌子,有很多桌子,除了这张桌子,还有一大堆乱七八糟的东西。有什么想法吗?

(注意:早些时候,我可以为$crawler对象应用一个过滤器来获得我需要的一些信息,然后我serialize()信息,最后有了一个字符串,这很有意义。但我再也无法获得字符串了,我想知道为什么。)

DomCrawler html()函数不会按照函数描述转储整个html:

http://api.symfony.com/2.6/Symfony/Component/DomCrawler/Crawler.html#method_html

它只返回它在您的案例中所做的第一个节点。

您可以使用http://php.net/manual/en/domdocument.savehtml.php因为DomCrawler是一组SplObjectStorage。

$html = $crawler->getNode(0)->ownerDocument->saveHTML();

如果您查看Crawler::html()的源代码,您将看到它正在执行以下操作:

$html = '';
foreach ($this->getNode(0)->childNodes as $child) {
    $html .= $child->ownerDocument->saveHTML($child);
}
return $html;