在h2后面选择带有id的子标记的p标记


Select p tag after h2 that has a child with id

如何选择具有特定子标签的标签之后的p-标签?使用网络爬虫。http://symfony.com/doc/current/components/css_selector.html

$crawler->filter('h2 span#hello + p')->each(function ($node) {
    var_dump($node->html());
});

的例子:

<h2><span id="hello">Hi</span></h2>
<p>I want this p-tag, that is after the h2 above</p>
 <p>me too!</p>
<a>Not me!</a>
<h2>lol</h2>
<p>yo, not me</p>

通常最好使用DOMDocument类(http://php.net/manual/en/class.domdocument.php)遍历HTML,但也可以使用正则表达式:

// put the example HTML code into a string
$html = <<< EOF
<h2><span id="hello">Hi</span></h2>
<p>I want this p-tag, that is after the h2 above</p>
 <p>me too!</p>
<a>Not me!</a>
<h2>lol</h2>
<p>yo, not me</p>
EOF;
// set up a regular expression
$re = "/<h2[^>]*>.*?<span[^>]*id='"hello'"[^>]*>.*?<''/h2[^>]*>.*?(<p.*?)<[^''/p]/sim";
// get the match ... the (.*?) in the above regex
preg_match($re,$html,$matches);
print $matches[1];

将输出:

<p>I want this p-tag, that is after the h2 above<p>

<p>me too!</p>