我正在学习一个教程,它展示了如何编写一个解析网页并找到所有链接的程序。但是,这个程序只在使用http的页面上工作。每当我尝试在具有证书(https)的站点上运行它时,它会引发以下错误:
致命错误: Uncaught : DOMDocument::loadHTML():参数#1 ($source)不能在C:\xampp\htdocs\froogal\classes\DomDocumentParser.php(14):堆栈跟踪中为空:#0 C:\xampp\htdocs\froogal\classes\DomDocumentParser.php(14):DOMDocument->loadHTML('') #1 C:\xampp\htdocs\froogal\crawl.php(6):DomDocumentParser->__construct('http://www.appl...') #2 C:\xampp\htdocs\froogal\crawl.php(18):在第14行的C:\xampp\htdocs\froogal\classes\DomDocumentParser.php中抛出followLinks('http://www.appl...') #3 {main}
DomDocumentParser.php文件的代码是:
<?php
class DomDocumentParser {
private $doc;
public function __construct($url) {
$options = array(
'http'=>array('method'=>"GET", 'header'=>"User-Agent: doodleBot/0.1\n")
);
$context = stream_context_create($options);
$this->doc = new DomDocument();
@$this->doc->loadHTML(file_get_contents($url, false, $context));
}
public function getlinks() {
return $this->doc->getElementsByTagName("a");
}
}
?>crawl.php的代码是:
<?php
include("classes/DomDocumentParser.php");
function followLinks($url) {
$parser = new DomDocumentParser($url);
$linkList = $parser->getLinks();
foreach($linkList as $link) {
$href = $link->getAttribute("href");
echo $href . "<br>";
}
}
$startUrl = "http://www.apple.com";
followLinks($startUrl);
?>发布于 2022-07-19 13:48:18
我也犯了同样的错误。然后,我发现file_get_contents()函数在检索数据时会产生UTF-8问题。你可以用一个小窍门解决这个问题。当导入数据时,它将文件设置为UTF-8,并且它工作得很好,就好像数据在UTF-8中一样。它对我有效,你也可以试试。您所需要做的就是更改这一行:
@$this->document->loadHTML('<?xml encoding="UTF-8">'.file_get_contents($url,false,$context));https://stackoverflow.com/questions/68570727
复制相似问题