【问题标题】:How to filter children node values from parent div where style = "..." using Goutte and Symfony DomCrawler?如何使用 Goutte 和 Symfony DomCrawler 从父 div 中过滤子节点值,其中 style = "..."?
【发布时间】:2015-08-10 06:23:06
【问题描述】:

我正在尝试使用 php 包 Goutte 从给定的 wikiquote 页面中抓取引号,该包包含 Symfony 组件:BrowserKit、CssSelector 和 DomCrawler

但是,我的结果集中有一些我不想要的引号,来自misattributed section 的引号。

这是我目前所拥有的:

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);
    }
}

我现在关注的问题是如何从错误分配的部分中过滤掉引号。此部分包含在具有 style 属性的父 div 中:

style="padding: .5em; border: 1px solid black; background-color:#FFE7CC"

我在想,如果我能以某种方式从这个特定部分获取li 节点值,我就可以从上面的$quoteArray 中过滤掉它们。我遇到的问题是我无法弄清楚如何从此部分中选择子 li 节点值。

我尝试选择具有以下变体的孩子:

$badQuotes = $crawler->filter('div[style="padding: .5em; border: 1px solid black; background-color:#FFE7CC"] > ul > li');

但这并没有返回我需要的节点值。有谁知道该怎么做或我做错了什么?

【问题讨论】:

    标签: php css symfony goutte domcrawler


    【解决方案1】:

    DomCrawler filter 方法会

    使用 CSS 选择器过滤节点列表。

    这不如使用 xpath 强大。我猜 CSS 选择器无法将您的复杂查询转换为 xpath 表达式。所以,一个复杂的过滤器应该通过filterXPath 方法来完成,而不是

    使用 XPath 表达式过滤节点列表。

    因此,在您的情况下,请尝试使用filterXPath 方法:

    $crawler->filterXPath("//div[contains(@style,'padding: .5em; border: 1px solid black; background-color:#FFE7CC')]");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-05
      • 1970-01-01
      • 2016-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多