尝试回显元素的属性时尝试获取非对象的 属性
Trying to get property of non-object when trying to echo the attribute of element
我不断收到错误 Trying to get 属性 of non-object on the line
$title->$my_xpath->query
当 运行 脚本。我有正确的节点和路径,但直到无法正常工作
$nodes = $my_xpath->query( '//div[@class="info_coupon"]' );
foreach( $nodes as $node )
{
$title = $my_xpath->query( 'a', $node )->item(0)->nodeValue;
echo $title;
$code = $my_xpath->query( 'a/@data-code', $node );
if( $code->length>0 ) {
$coupon = $code->item(0)->nodeValue ;
echo $coupon;
}
}
该页面上有一些 javascript 操作生成了 <a>
。但本质上(当你得到初始$html
)时,有none.
这是您从代码的初始行中得到的片段:
$url = "http://zoutons.com/stores/paytm-coupons/";
$html = file_get_contents($url); <-- this one contains
这个:
<div class="info_coupon">
<span rel="nofollow" data-lnu="aHR0cDovL3RyYWNraW5nLnZjb21taXNzaW9uLmNvbS9hZmZfYz9vZmZlcl9pZD0xMDIyJmFmZl9pZD0yMDYwJnVybD1odHRwcyUzQSUyRiUyRnBheXRtLmNvbSUyRiUzRnV0bV90ZXJtJTNEe2FmZmlsaWF0ZV9pZH0=" href="http://zoutons.com/stores/paytm-coupons/?#cid=31215" class="heading affiliate affiliate_map c_data_31215" data-affiliate="aHR0cDovL3RyYWNraW5nLnZjb21taXNzaW9uLmNvbS9hZmZfYz9vZmZlcl9pZD0xMDIyJmFmZl9pZD0yMDYwJnVybD1odHRwcyUzQSUyRiUyRnBheXRtLmNvbSUyRiUzRnV0bV90ZXJtJTNEe2FmZmlsaWF0ZV9pZH0=" data-id="31215" data-code="NEW50" data-link_type="text" store="Paytm">GET FREE Rs.50/- ON RECHARGE (VALID TILL – APRIL 27)
</span>
原来真的没有<a>
但您要查找的数据实际上在 <span>
中:
href="http://zoutons.com/stores/paytm-coupons/?#cid=31215"
data-code="NEW50"
所以就把它放在那里:
$nodes = $my_xpath->query( '//div[@class="info_coupon"]' );
foreach( $nodes as $node )
{
$title = $my_xpath->evaluate('string(./span/@href)', $node);
$code = $my_xpath->evaluate('string(./span/@data-code)', $node);
echo $title;
echo $code;
}
我不断收到错误 Trying to get 属性 of non-object on the line
$title->$my_xpath->query
当 运行 脚本。我有正确的节点和路径,但直到无法正常工作
$nodes = $my_xpath->query( '//div[@class="info_coupon"]' );
foreach( $nodes as $node )
{
$title = $my_xpath->query( 'a', $node )->item(0)->nodeValue;
echo $title;
$code = $my_xpath->query( 'a/@data-code', $node );
if( $code->length>0 ) {
$coupon = $code->item(0)->nodeValue ;
echo $coupon;
}
}
该页面上有一些 javascript 操作生成了 <a>
。但本质上(当你得到初始$html
)时,有none.
这是您从代码的初始行中得到的片段:
$url = "http://zoutons.com/stores/paytm-coupons/";
$html = file_get_contents($url); <-- this one contains
这个:
<div class="info_coupon">
<span rel="nofollow" data-lnu="aHR0cDovL3RyYWNraW5nLnZjb21taXNzaW9uLmNvbS9hZmZfYz9vZmZlcl9pZD0xMDIyJmFmZl9pZD0yMDYwJnVybD1odHRwcyUzQSUyRiUyRnBheXRtLmNvbSUyRiUzRnV0bV90ZXJtJTNEe2FmZmlsaWF0ZV9pZH0=" href="http://zoutons.com/stores/paytm-coupons/?#cid=31215" class="heading affiliate affiliate_map c_data_31215" data-affiliate="aHR0cDovL3RyYWNraW5nLnZjb21taXNzaW9uLmNvbS9hZmZfYz9vZmZlcl9pZD0xMDIyJmFmZl9pZD0yMDYwJnVybD1odHRwcyUzQSUyRiUyRnBheXRtLmNvbSUyRiUzRnV0bV90ZXJtJTNEe2FmZmlsaWF0ZV9pZH0=" data-id="31215" data-code="NEW50" data-link_type="text" store="Paytm">GET FREE Rs.50/- ON RECHARGE (VALID TILL – APRIL 27)
</span>
原来真的没有<a>
但您要查找的数据实际上在 <span>
中:
href="http://zoutons.com/stores/paytm-coupons/?#cid=31215"
data-code="NEW50"
所以就把它放在那里:
$nodes = $my_xpath->query( '//div[@class="info_coupon"]' );
foreach( $nodes as $node )
{
$title = $my_xpath->evaluate('string(./span/@href)', $node);
$code = $my_xpath->evaluate('string(./span/@data-code)', $node);
echo $title;
echo $code;
}