2017-10-16 247 views
-2

Why do inner text is not active的innerText在simple_html_dom

Here is HTML code

[这里是HTML代码]

<ul class="product"> 
<li class="product col-md-4 col-sm-4 col-xs-6 "><div class="product-header"> 
<a href="/so-mi-octopus-xanh-soc-trang-p5163098.html"> 
<img src="//cdn.nhanh.vn/cdn/store/17863/ps/20170925/0ctopus_thumb_450x600.jpg" class="attachment-shop_catalog size-shop_catalog wp-post-image"> 
</a><div class="buttons"> 
<a href="/so-mi-octopus-xanh-soc-trang-p5163098.html" rel="nofollow" class="button add_to_cart_button"> 
<i class="fa fa-shopping-bag" aria-hidden="true"></i> 
<span class="screen-reader-text">Thêm vào giỏ</span></a> 
<a data-product_id="5163098" class="button btnFav" rel="nofollow"> 
<i class="fa fa-heart-o" aria-hidden="true"></i> 
<span class="screen-reader-text">Yêu thích</span> 
</a></div></div><h3><a href="/so-mi-octopus-xanh-soc-trang-p5163098.html">Sơ mi Octopus xanh sọc trắng</a></h3><span class="price"> 
<span class="woocommerce-Price-amount amount"> 
400,000 ₫       </span> 
</span></li> 
</ul> 

[这里是我的代码]

<?php 
require "simple_html_dom.php"; 
$html=file_get_html("http://zuhaus.vn/zu-design-pc150502.html?page=1"); 
$ds=$html->find("ul.products li"); 
foreach ($ds as $sp) { 
    # code... 
    $price=$sp->find("span.price span",0); 
    echo $price; 
    $name=$sp->find("h3 a",1)->innertext; 
    echo $name; 
} 
?> 

我已经尝试了很多的测试用例但它不会工作:“< T汉克斯你 P/S我用库simple_html_dom

+1

请将代码放在问题中,而不是在外部网站上。在您自助**后,StackOverflow上的用户将帮助您。参观:http://stackoverflow.com/tour。做一些研究:https://meta.stackoverflow.com/q/261592/1011527。 了解如何发布好问题:http://stackoverflow.com/help/how-to-ask。和http://stackoverflow.com/help/mcve。 – Nic3500

回答

0

如果你想获得一个标签的内容,并列入H3那么你有一个语法错误

$name = $sp->find("h3 a", 1)->innertext; 

我建议检查以下

0语法行
$name = $sp->find('h3', 1)->find('a', 1)->innertext; 
+0

如果我的答案帮助解决了问题,请回报点数 –

1

的问题是你的选择,我改变了选择的产品的名称,它只是工作,还使用卷曲,以提高快速爬行

<?php 
require "simple_html_dom.php"; 



$ch = curl_init("http://zuhaus.vn/zu-design-pc150502.html?page=1"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$curl_scraped_page = curl_exec($ch); 

$html = new simple_html_dom(); 
$html->load($curl_scraped_page); 

$ds=$html->find("ul.products li"); 
foreach ($ds as $sp) { 
    # code... 
    $price=$sp->find("span.price span",0); 
    echo $price; 
    $name=$sp->find("a",3)->innertext; // this is where the problem on your code 
    echo $name; 

echo "</br>"; 
} 
?> 
+0

$ name = $ sp-> find(“h3 a”,1) - > innertextxt; echo $ name;现在我的代码一样,但它不会工作。谢谢你^^ –

+0

你能不能把你的代码粘贴到这里? –

+0

我重新编辑了我的代码。这是我第一次在这里发布问题^^:“< –