1
我正在嘗試學習網絡抓取並使用此示例獲取頁面中的鏈接。有沒有更好的方法來做到這一點,或者例如什麼是最簡單的方法來獲得h1?簡單的Web抓取PHP Xpath DOM
$html = file_get_contents('page.html');
//parse the html into a DOMDocument
$dom = new DOMDocument();
@$dom->loadHTML($html);
//grab all the links on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");
for ($i = 0; $i < $hrefs->length; $i++) {
$href = $hrefs->item($i);
$url = $href->getAttribute('href');
echo "<br />Link: $url";
}
使用PHP的捲曲函數與domdocument而不是file_get_contents來刮取它很容易和強大 –