DOMXPath->评估没有找到我需要的div

时间:2012-04-12 15:29:14

标签: php curl web-scraping domxpath

美好的一天,

我试图抓住结果并取得了成功,但我现在陷入困境。

下面的代码显示有一个DIV类为'vsc',而内部则是一个类为'r'的H3。我可以使用(// h3 [@ class ='r'// a)获取H3标签内的锚点。

我的问题是下面的表格中还有一个带有'r'类的H3,我不希望表格中有任何链接。

<li class="g">
<div class="vsc" pved="0CD4QkgowAA" bved="0CD8QkQo" sig="m15">
<h3 class="r">
<a href="https://ameriloan.com/" class="l" onmousedown="return          rwt(this,'','','','1','AFQjCNEazKuyTuAyYgnAT3MqI3aJoiAlZw','','0CDwQFjAA',null,event)">
</h3>
<div class="vspib" aria-label="Result details" role="button" tabindex="0">
<div class="s">
</div>
<table cellpadding="0" cellspacing="0" class="nrgt">

这是我用来刮掉所有锚点的脚本,但是它不能只检索'vsc'DIV中的H3锚点:

function getURL($url)


{
$ch=curl_init();
// This allows the script to accept HTTPS certificates "blindly"
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_HTTP_VERSION,'CURL_HTTP_VERSION_1_1' );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Follows redirects
curl_setopt($ch, CURLOPT_MAXREDIRS, 6);  // follows up to 6 redirects
$ret = curl_exec($ch);
return $ret;
}
$i = 0;
$rawKeyword = 'EXAMPLE';
$keyword = str_replace(' ', '+', $rawKeyword);

$url = "http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=".$keyword;

//get the HTML through cURL function
$html = getURL($url);

// parse the html into a DOMDocument
$dom = new DOMDocument();
@$dom->loadHTML($html);

// grab all data
$xpath = new DOMXPath($dom);

// XPath eval to get page links and titles 
//$elementContent = $xpath->evaluate("//h3[@class='r']//a");
$elementContent = $xpath->evaluate("//div[@class='vsc']//h3[@class='r']//a");


// Print results
foreach ($elementContent as $content) {
  $i++;
  $clean = trim($content->getAttribute('href'), "/url?q=");
  echo '<strong>'.$i.'</strong>: <h3 style=" clear:none !important; font-size:10px; letter-spacing:0.1em; line-height:2.6em; text-transform:uppercase;">'.$content->textContent.'</h3><br/>'.$clean.'<br /><br />';
}

我的评估查询出了什么问题?

@jdwilemo - 你是正确的方式我试图只用一个'vsc'类来获得DIV内的锚点。这里有更多的表格代码,它显示了另一个带有“r”类的H3 DIV ......

<table cellpadding="0" cellspacing="0" class="nrgt">
<tbody>
<tr class="mslg">
<td style="vertical-align: top; ">
<div class="sld vsc" pved="0CIYBEJIKMAE" bved="0CIcBEJEK" sig="Q_U">
<span class="tl">
<h3 class="r">
<a href="https://example.com/?page=ent_cs_login" class="l" onmousedown="return rwt(this,'','','','2','AFQjCNEyANjoolNXGFnLVKH3S1j4CO1qQw','','0CIQBEIwQMAE',null,event)">
</h3>
</span>
<div class="vspib" aria-label="Result details" role="button" tabindex="0">
<div class="s">
</div>
</li>

所有内容都包含在'li'标签中。该表是'li'标记中的最后一个元素。我想得到&lt; H3 class ='r'&gt;锚没有得到&lt; H3 class ='r'&gt;在'li'元素末尾的表内锚定。我希望我清除了......

1 个答案:

答案 0 :(得分:1)

如果我正确地理解了你的问题,那么你只需要一个h3的锚点,其中class = r AND,它位于一个带有class = vsc 的 div下面。但是你得到了多个H3节点。

如果这是正确的,您还需要在查询中指定div的类,就像使用h3一样://div[@class='vsc']/h3[@class='r'//a

如果不是这种情况,那么请更新详细信息和更广泛的xml示例更新您的问题,其中包含您所指的模糊数据,我会优化我的答案,希望这会有所帮助!

注意:使用“//”告诉XPath从“root”开始或开始,所以// hath的XPath将返回名称=“h3”的所有节点

修改 如果你想要div中的锚而不是table元素中的锚,只需使用像这样的祖先函数:

//h3[@class='r' and not(ancestor::table)]//a

希望这会有所帮助,如果我需要澄清其他任何内容,请告诉我们!