[英]PHP to get html source, then parse values within certain DIV tags
我可以很好地獲取源代碼,但我現在希望能夠從特定的div中獲取數據:
$html = file_get_contents('http://www.website.com');
說$ html包含:
<div class="productData">
<div class="productDescription">Here is the product description</div>
<div class="productPrice">1.99</div>
</div>
我希望能夠在其中返回數據,並為所有事件執行此操作?
謝謝。
使用DOMDocument類 ,結合DOMXPath ,如下所示:
$url = 'http://www.website.com/';
$dom = new DOMDocument();
$dom->load($url);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query("//*[contains(@class, 'productData')]");
foreach ($nodes as $node) {
// do something
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.