PHP爬虫程序不适用于wikipedia
下面是我在id=Summary下输出文本的php代码。这个脚本在其他网站上运行良好,但在维基百科上不行。我还粘贴了下面的错误。维基百科是否限制解析器脚本?如果是这样的话,是否有任何解决方案可以解析并从wiki获取内容? 提前谢谢PHP爬虫程序不适用于wikipedia,php,parsing,web-crawler,wiki,Php,Parsing,Web Crawler,Wiki,下面是我在id=Summary下输出文本的php代码。这个脚本在其他网站上运行良好,但在维基百科上不行。我还粘贴了下面的错误。维基百科是否限制解析器脚本?如果是这样的话,是否有任何解决方案可以解析并从wiki获取内容? 提前谢谢 <?php function getElementByIdAsString($url, $id, $pretty = true) { $doc = new DOMDocument(); $ch = curl_init($url); c
<?php
function getElementByIdAsString($url, $id, $pretty = true) {
$doc = new DOMDocument();
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
// var_dump($doc->loadHTMLFile($url)); die;
error_reporting(E_ERROR | E_PARSE);
if(!$result) {
throw new Exception("Failed to load $url");
}
$doc->loadHTML($result);
// Obtain the element
$element = $doc->getElementById($id);
if(!$element) {
throw new Exception("An element with id $id was not found");
}
if($pretty) {
$doc->formatOutput = true;
}
// Return the string representation of the element
return $doc->saveXML($element);
}
//Here I am dispalying the output in bold text
echo getElementByIdAsString('https://en.wikipedia.org/wiki/A_Brief_History_of_Time', 'Summary');
?>
看起来像是这个的复制品: 原因是curl尝试验证cert,因此只需添加:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
消除这个问题,但我建议使用所有这些
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
你得到的卷曲误差是多少?此函数将返回SSL证书问题的错误,验证CA证书是否正常。详细信息:错误:14090086:SSL例程:SSL3\u获取\u服务器\u证书:证书验证失败可能重复我尝试过的,但不起作用..如果可能,您可以将我的代码更正为功能性..谢谢
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);