如何使用PHP文档解析HTML?
我这里有一个HTML块:如何使用PHP文档解析HTML?,php,html,parsing,domdocument,Php,Html,Parsing,Domdocument,我这里有一个HTML块: <div class="title"> <a href="http://test.com/asus_rt-n53/p195257/"> Asus RT-N53 </a> </div> <table> <tbody> <tr> <td class="price-status">
<div class="title">
<a href="http://test.com/asus_rt-n53/p195257/">
Asus RT-N53
</a>
</div>
<table>
<tbody>
<tr>
<td class="price-status">
<div class="status">
<span class="available">Yes</span>
</div>
<div name="price" class="price">
<div class="uah">758<span> ua.</span></div>
<div class="usd">$ 62</div>
</div>
一个丑陋的解决方案是将价格结果转换为只保留数字:
echo (int) $price->nodeValue;
或者,您可以查询以查找div内的跨度,并将其从price(prices foreach内)中删除:
编辑:
要检索链接,只需使用getAttribute()
并获取href
链接:
$model->getAttribute('href')
你当前的代码有什么问题?所以我得到了一个名字和价格,但我需要更多和一个链接。而这样的单位在一个页面上并不多,因为它可以在一个循环中做什么呢?现在看起来像是-名字-价格--价格,必要时:-名称-价格-链接您必须阅读
href
属性。也许这有帮助:。所有东西的价格都可以,但如何获得链接?">
$span = $xpath->query('//div[@class="uah"]/span')->item(0);
$price->removeChild($span);
echo $price->nodeValue;
$model->getAttribute('href')