如何使用 symfony dom 解析器
How to use symfony dom parser
我正在尝试使用 Symfony 爬虫。
所以我检查了这个article。
我想要做的是获取3,335.00
(第二个参数)
现在,我尝试这样的句子,但它是错误的。
$crawler = $crawler->filter('body > div[@class="cell_label"]');
我该怎么做?
<body>
<div class="cell__label"> Value1 </div> <div class="cell__value cell__value_"> 2,355.00 </div>
<div class="cell__label"> Value2 </div> <div class="cell__value cell__value_"> 3,355.00 </div>
<div class="cell__label"> Value3 </div> <div class="cell__value cell__value_"> 4,355.00 </div>
</body>
$crawler = new Crawler($url);
$crawler = $crawler->filter('body > div[@class="cell_label"]');//// no work...
foreach ($crawler as $domElement) {
var_dump($domElement);
}
我可以在这里看到几个问题:
使用 $crawler->filter()
意味着您必须传递 css selector 作为参数,而不是 XPath 表达式,因此请使用 'body > div.cell__label'
或 'body div[class^="cell__"]'
如果你需要 select 所有 div
和 class 以 cell__
开头,顺便说一句,你在 cell_label
中有错字(一个下划线)。
The Crawler accepts DOMNodeList, DOMNode, array 或 string 作为构造函数参数,而不是 url 到远程资源(但我假设它可能只是您在那里使用的任意变量名称)。从技术上讲 url 也是一个字符串,但不是 XML 格式的字符串。
如果要使用 XPath 表达式,请使用 $crawler->filterXPath()
,例如:
$nodes = $crawler->filterXPath('//div[contains(@class, "cell__label")]');
这是一份关于如何使用 XPath 的文档 - https://www.w3.org/TR/xpath/
抓取工具过滤器可以像选择器一样处理 jQuery,因此您可以:
$crawler = $crawler->filter('.cell__value');
我正在尝试使用 Symfony 爬虫。
所以我检查了这个article。
我想要做的是获取3,335.00
(第二个参数)
现在,我尝试这样的句子,但它是错误的。
$crawler = $crawler->filter('body > div[@class="cell_label"]');
我该怎么做?
<body>
<div class="cell__label"> Value1 </div> <div class="cell__value cell__value_"> 2,355.00 </div>
<div class="cell__label"> Value2 </div> <div class="cell__value cell__value_"> 3,355.00 </div>
<div class="cell__label"> Value3 </div> <div class="cell__value cell__value_"> 4,355.00 </div>
</body>
$crawler = new Crawler($url);
$crawler = $crawler->filter('body > div[@class="cell_label"]');//// no work...
foreach ($crawler as $domElement) {
var_dump($domElement);
}
我可以在这里看到几个问题:
使用
$crawler->filter()
意味着您必须传递 css selector 作为参数,而不是 XPath 表达式,因此请使用'body > div.cell__label'
或'body div[class^="cell__"]'
如果你需要 select 所有div
和 class 以cell__
开头,顺便说一句,你在cell_label
中有错字(一个下划线)。The Crawler accepts DOMNodeList, DOMNode, array 或 string 作为构造函数参数,而不是 url 到远程资源(但我假设它可能只是您在那里使用的任意变量名称)。从技术上讲 url 也是一个字符串,但不是 XML 格式的字符串。
如果要使用 XPath 表达式,请使用
$crawler->filterXPath()
,例如:$nodes = $crawler->filterXPath('//div[contains(@class, "cell__label")]');
这是一份关于如何使用 XPath 的文档 - https://www.w3.org/TR/xpath/
抓取工具过滤器可以像选择器一样处理 jQuery,因此您可以:
$crawler = $crawler->filter('.cell__value');