【发布时间】:2022-10-07 23:13:25
【问题描述】:
我们都在网页的标题中看到/使用过这样的内容:
<link rel="stylesheet" href="https://somedomain.com/">
如何使用php从标题中获取所有链接标签?
就像
get_meta_tags(url);
检索元标记。
【问题讨论】:
我们都在网页的标题中看到/使用过这样的内容:
<link rel="stylesheet" href="https://somedomain.com/">
如何使用php从标题中获取所有链接标签?
就像
get_meta_tags(url);
检索元标记。
【问题讨论】:
有多种方法可以抓取页面并对其进行解析。
require_once("simple_html_dom.php");
$pageContent = file_get_html("http://example.com");
foreach ($pageContent->find("link") as $link){
`enter code here`echo $link->href . "<br>";
}
在我的例子中,我将使用一个快速的“file_get_contents”来完成工作。您可能想要发出正确的 CURL 请求。
$html = file_get_contents('http://www.exmaple.com');
$doc = new DOMDocument();
$doc->loadHTML($html);
$xp = new DOMXPath($doc);
$res = $xp->query('//link');
if($res->length > 0){
foreach ($res as $node){
echo $node -> nodeValue;
}
}
【讨论】: