簡體   English   中英

simple_html_dom 中的內部文本

[英]innertext in simple_html_dom

為什么內部文本不活動

這是 HTML 代碼

[這是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;
}
?>

我已經嘗試了很多測試用例,但它不起作用:“<謝謝P/s 我使用了庫 simple_html_dom

問題出在您的選擇器上,我更改了產品名稱的選擇器,它剛剛起作用,還使用 ​​curl 來提高爬行速度

<?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>";
}
?>

如果要獲取標記的內容並包含在 h3 中,則該行中存在語法錯誤

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

我建議檢查以下語法

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM