DOMdocument 和 Xpath 数组问题
DOMdocument and Xpath array issue
我在 file_get_contents 和 DOMdocument 以及 Xpath 方面遇到了一些麻烦。
我正在尝试进行一些抓取。
所以我为网站 link 制作了一个数组。
array(9) {
[0]=>
string(34) "https://lions-mansion.jp/MF081014/"
[1]=>
string(34) "https://lions-mansion.jp/MF161026/"
[2]=>
string(34) "https://lions-mansion.jp/MF171045/"
[3]=>
string(34) "https://lions-mansion.jp/MF161016/"
[4]=>
string(34) "https://lions-mansion.jp/MF171010/"
}
尝试使用 foreach 进入这些 links。并尝试抓取 link rel 的 href!
foreach ($siteUrls as $sites){
@$html [] = file_get_contents($sites);
}
foreach ($html as $geturl)
{
$grabber = new \DOMXPath($geturl);
$mainLink [] = $grabber->query("//link[@rel='canonical']/@href");
}
var_dump($mainLink);
但最后遇到了这个错误。
Argument 1 passed to DOMXPath::__construct() must be an instance of
DOMDocument, string given
知道如何解决这个问题吗?我怎样才能得到 link rel url
?来自头部标签
libxml_use_internal_errors
: 禁用 libxml 错误并允许用户根据需要获取错误信息 http://php.net/manual/en/function.libxml-use-internal-errors.php
<?php
$siteUrls = [
"https://lions-mansion.jp/MF081014/",
"https://lions-mansion.jp/MF161026/",
"https://lions-mansion.jp/MF171045/",
"https://lions-mansion.jp/MF161016/",
"https://lions-mansion.jp/MF161016/"
];
foreach ($siteUrls as $sites){
@$html [] = file_get_contents($sites);
}
libxml_use_internal_errors(true);
foreach ($html as $geturl)
{
$dom = new DOMDocument();
$dom->loadHTML($geturl);
$grabber = new DOMXPath($dom);
$names = $grabber->query("//link[@rel='canonical']/@href");
foreach($names as $contextNode) {
$mainLink[] = $contextNode->value;
}
}
libxml_clear_errors();
var_dump($mainLink);
array (size=2)
0 => string 'https://lions-mansion.jp/MF161026/' (length=34)
1 => string 'https://lions-mansion.jp/MF171045/' (length=34)
我在 file_get_contents 和 DOMdocument 以及 Xpath 方面遇到了一些麻烦。
我正在尝试进行一些抓取。 所以我为网站 link 制作了一个数组。
array(9) {
[0]=>
string(34) "https://lions-mansion.jp/MF081014/"
[1]=>
string(34) "https://lions-mansion.jp/MF161026/"
[2]=>
string(34) "https://lions-mansion.jp/MF171045/"
[3]=>
string(34) "https://lions-mansion.jp/MF161016/"
[4]=>
string(34) "https://lions-mansion.jp/MF171010/"
}
尝试使用 foreach 进入这些 links。并尝试抓取 link rel 的 href!
foreach ($siteUrls as $sites){
@$html [] = file_get_contents($sites);
}
foreach ($html as $geturl)
{
$grabber = new \DOMXPath($geturl);
$mainLink [] = $grabber->query("//link[@rel='canonical']/@href");
}
var_dump($mainLink);
但最后遇到了这个错误。
Argument 1 passed to DOMXPath::__construct() must be an instance of DOMDocument, string given
知道如何解决这个问题吗?我怎样才能得到 link rel url
?来自头部标签
libxml_use_internal_errors
: 禁用 libxml 错误并允许用户根据需要获取错误信息 http://php.net/manual/en/function.libxml-use-internal-errors.php
<?php
$siteUrls = [
"https://lions-mansion.jp/MF081014/",
"https://lions-mansion.jp/MF161026/",
"https://lions-mansion.jp/MF171045/",
"https://lions-mansion.jp/MF161016/",
"https://lions-mansion.jp/MF161016/"
];
foreach ($siteUrls as $sites){
@$html [] = file_get_contents($sites);
}
libxml_use_internal_errors(true);
foreach ($html as $geturl)
{
$dom = new DOMDocument();
$dom->loadHTML($geturl);
$grabber = new DOMXPath($dom);
$names = $grabber->query("//link[@rel='canonical']/@href");
foreach($names as $contextNode) {
$mainLink[] = $contextNode->value;
}
}
libxml_clear_errors();
var_dump($mainLink);
array (size=2)
0 => string 'https://lions-mansion.jp/MF161026/' (length=34)
1 => string 'https://lions-mansion.jp/MF171045/' (length=34)