在下面的代码中,无论是否存在,优惠券值始终为空白。
我试图仅在优惠券存在时提取代码和标题,但它不会工作 - 它只会返回一个空白页。
$url="http://www.grabon.in/amazon-coupons/";
$storename="AMAZON";
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,$url);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1');
$html = curl_exec($curl_handle);
curl_close($curl_handle);
$mydoc = new DOMDocument();
libxml_use_internal_errors(TRUE); //disable libxml errors
if(empty($html)) die("EMPTY HTML");
//echo ($html);
$mydoc->loadHTML($html);
libxml_clear_errors(); //remove errors for yucky html
$my_xpath = new DOMXPath($mydoc);
$nodes = $my_xpath->query( '//div[@id="category_coupons"]' );
foreach( $nodes as $node )
{
$code=$my_xpath->query( 'article[@class="sm-coupon"]/div[@class="smc-actions"]/div[@class="smc-actions-r"]/div[@class="sm-code"]/span', $node );
if ($code->length>0){
$coupon = $code->item(0)->nodeValue ;
$title = $my_xpath->query( 'article[@class="sm-coupon"]/div[@class="smc-info"]/h3', $node )->item(0)->nodeValue;
echo $storename.",".$title.",".$coupon."<br>";
}
}
答案 0 :(得分:0)
我不熟悉您使用的语法,但我想您想使用找到的nodes
来查找子文章......如果是这样,您需要使用XPath
之类的
./article[@class="sm-coupon"]/div[@class="smc-info"]/h3
在./
元素上node
点,只使用
article[@class="sm-coupon"]...
未指定./
表示您需要root
article
元素
<强>更新强>
如果您只想获得包含代码的articles
的标题,请使用以下XPath
:
//div[@class="smc-actions" and ./descendant::div[@class="sm-code"]/span[text()]]/preceding-sibling::div[@class="smc-info"]/h3
代码的 XPath
只是
//div[@class="sm-code"]/span[text()]
最终代码:
$titles = $my_xpath->query( '//div[@class="smc-actions" and./descendant::div[@class="sm-code"]/span[text()]]/preceding-sibling::div[@class="smc-info"]/h3' );
$coupons = $my_xpath->query( '//div[@class="sm-code"]/span[text()]' );
foreach(range(0, $titles->length-1) as $index)
{ echo $storename.",".$titles->item($index)->nodeValue.",".$coupons->item($index)->nodeValue."<br>"; }