【发布时间】:2013-08-08 10:39:46
【问题描述】:
我想从我的另一个域中抓取一个 div 的页面包含:
<div id="thisone">
<p>Stuff</p>
</div>
<div id="notthisone">
<p>More stuff</p>
</div>
使用这个 php...
<?php
$page = file_get_contents('http://thisite.org/source.html');
$doc = new DOMDocument();
$doc->loadHTML($page);
foreach ($doc->getElementsByTagName('div') as $node) {
echo $doc->saveHtml($node), PHP_EOL;
}
?>
...给我http://thisite.org/source.html 上的所有 div,带有 html。但是,我只想通过 id 为“thisone”的 div,但使用:
foreach ($doc->getElementById('thisone') as $node) {
什么都没有。
【问题讨论】:
标签: php html web-scraping scrape