Php 如何从父div筛选子节点值,其中style="&引用;使用Goutte和Symfony DomCrawler?
我正试图使用php包从给定的wikiquote页面中刮取引用,它包装了Symfony组件:BrowserKit、CssSelector和 但是,在我的结果集中有一些我不想要的引号,来自的引号 以下是我到目前为止的情况:Php 如何从父div筛选子节点值,其中style="&引用;使用Goutte和Symfony DomCrawler?,php,css,symfony,goutte,domcrawler,Php,Css,Symfony,Goutte,Domcrawler,我正试图使用php包从给定的wikiquote页面中刮取引用,它包装了Symfony组件:BrowserKit、CssSelector和 但是,在我的结果集中有一些我不想要的引号,来自的引号 以下是我到目前为止的情况: use Goutte\Client; $client = new Client(); $crawler = $client->request('GET', 'http://en.wikiquote.org/wiki/Thomas_Jefferson'); //grab
use Goutte\Client;
$client = new Client();
$crawler = $client->request('GET', 'http://en.wikiquote.org/wiki/Thomas_Jefferson');
//grab all the children li's from the wikiquote page
$quotes = $crawler->filter('ul > li');
$quoteArray = [];
//foreach li with a node value that does not start with a number, push the node value onto quote array
//this filters out the table of contents <li> node values which I do not want
foreach($quotes as $quote)
{
if(!is_numeric(substr($quote->nodeValue, 0, 1)))
{
array_push($quoteArray, $quote->nodeValue);
}
}
我在想,如果我能从这个特定的部分获取li
节点值,我就可以从我上面的$quotarray
中过滤掉它们。我遇到的问题是,我无法确定如何从本节中选择子节点li
节点值
我已经尝试过选择具有以下变化的孩子:
$badQuotes = $crawler->filter('div[style="padding: .5em; border: 1px solid black; background-color:#FFE7CC"] > ul > li');
但这并没有返回我需要的节点值。有人知道怎么做或者我做错了什么吗?DomCrawler方法将
使用CSS选择器筛选节点列表
这比使用xpath的功能要弱。我猜CSS选择器无法将复杂查询转换为xpath表达式。所以,一个复杂的过滤器应该用方法来代替
使用XPath表达式筛选节点列表
因此,在您的情况下,请尝试使用filterXPath
方法:
$crawler->filterXPath("//div[contains(@style,'padding: .5em; border: 1px solid black; background-color:#FFE7CC')]");
$crawler->filterXPath("//div[contains(@style,'padding: .5em; border: 1px solid black; background-color:#FFE7CC')]");