PHP获取html源代码,然后解析某些DIV标记中的值
我可以很好地获取源代码,但我现在希望能够从特定div中获取数据:PHP获取html源代码,然后解析某些DIV标记中的值,php,parsing,html,Php,Parsing,Html,我可以很好地获取源代码,但我现在希望能够从特定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">
$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>
这是产品说明
1.99
我希望能够在中返回数据,并对所有事件执行此操作
谢谢。结合以下内容使用:
$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
}
执行此操作或使用
preg\u match
函数匹配字符串并进行操作否!我确实尝试过这样做,但我得到了以下结果:警告:DOMDocument::loadHTML()[DOMDocument.loadHTML]:意外的结束标记:head-in-Entity
如果您使用修改后的代码(将$url更改为您想要使用的任何代码),我会得到:警告:DOMDocument::loadHTML()[DOMDocument.loadHTML]:htmlparserentityref:expecting';'在实体
中,这可能与包含javascript的页面有关吗?