如何使用PHP抓取指定页面中的全部链接地址?
- 内容介绍
- 文章标签
- 相关推荐
本文共计155个文字,预计阅读时间需要1分钟。
通过使用此代码段,您可以轻松地提取网页上的所有链接。以下是简化后的代码:
php$=file_get_contents('http://www.example.com');$dom=new DOMDocument();@$dom->loadHTML($);// 提取所有链接$xpath=new DOMXPath($dom);$links=$xpath->query('//a/@href');foreach ($links as $link) { echo $link->nodeValue . \n;}
$html = file_get_contents('www.example.com'); $dom = new DOMDocument(); @$dom->loadHTML($html); // grab all the 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 $url.' '; }
本文共计155个文字,预计阅读时间需要1分钟。
通过使用此代码段,您可以轻松地提取网页上的所有链接。以下是简化后的代码:
php$=file_get_contents('http://www.example.com');$dom=new DOMDocument();@$dom->loadHTML($);// 提取所有链接$xpath=new DOMXPath($dom);$links=$xpath->query('//a/@href');foreach ($links as $link) { echo $link->nodeValue . \n;}
$html = file_get_contents('www.example.com'); $dom = new DOMDocument(); @$dom->loadHTML($html); // grab all the 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 $url.' '; }

