时时商务社区

标题: php查找任何页面上的所有链接的方法 [打印本页]

作者: 阿情    时间: 2018-2-14 06:01

            使用DOM,你可以轻松从任何页面上抓取链接,代码示例如下:
[U]复制代码[/U] 代码如下:
$html = file_get_contents('http://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 length; $i++) {
$href = $hrefs->item($i);
$url = $href->getAttribute('href');
echo $url.'
';
}
            
            
        




欢迎光临 时时商务社区 (http://bbs.4435.cn/) Powered by Discuz! X3.2