【发布时间】:2019-03-08 19:31:11
【问题描述】:
我正在尝试使用 simple_html_dom.php 解析 HTML。我试图解析的 HTML 如下所示。我可以成功抓取每个产品名称:Product 1、Product 2、Product 3等
我还想从每个产品中获取itemprice_0。这是我遇到问题的地方。这是我的代码:
<?php
require_once 'simple_html_dom.php';
$html = file_get_html('https://www.webaddress.com');
foreach($html->find('span.productName') as $e)
echo $e.'<br />'; //successfully displays all product names
foreach($html->find('#itemprice_0') as $e)
echo $e; //doesn't display the item prices
foreach($html->find('.dollar') as $e)
echo $e; //doesn't display the dollar amounts
?>
这里是 HTML:
<span class="productName">Product 1</span>
<p class="price">
<strike>
<span class="dollar-symbol">$</span>
<span class="dollar">15</span><span class="dot">.</span>
<span class="cents">99</span></strike>
</p>
<p class="salePrice" id='itemprice_0'>
<span class="dollar-symbol">$</span>
<span class="dollar">13</span><span class="dot">.</span>
<span class="cents">99</span>
</p>
【问题讨论】:
-
我认为你错过了
innertext。试试echo $e->innertext; -
foreach($html->find('.salePrice') as $e) echo $e->children(2)->plainText;
-
感谢两位提供建议。 innertext 和 children(2)->plainText 都不成功。
标签: php html parsing dom scrape