【问题标题】:php crawler for wiki getting error [closed]wiki的php爬虫出错[关闭]
【发布时间】:2016-03-25 03:57:04
【问题描述】:

在下面的代码中,我尝试使用 php 代码从网站中提取内容,当我使用 getElementByIdAsString('www.abebooks.com/9780143418764/Love-Story-Singh-Ravinder-0143418769/ plp', '概要');

但是当我使用相同的代码从维基百科中提取内容时它不起作用,getElementByIdAsString('https://en.wikipedia.org/wiki/A_Brief_History_of_Time', 'Summary');

以下是我的代码以及使用后者时遇到的异常。有人可以更正我的代码以根据 id 提取维基百科内容

提前致谢。

<?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');

?>

例外

Fatal error: Uncaught exception 'Exception' with message 'Failed to load http://en.wikipedia.org/wiki/A_Brief_History_of_Time' in C:\xampp\htdocs\example2.php:18 Stack trace: #0 C:\xampp\htdocs\example2.php(40): getElementByIdAsString() #1 {main} thrown in C:\xampp\htdocs\example2.php on line 18

您的帮助将非常有用:-)

【问题讨论】:

    标签: php parsing web-crawler


    【解决方案1】:

    尝试添加:

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    

    在评论中讨论后更新:

    <?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);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    
        $result = curl_exec($ch);
    
        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;
        }
    
        $output = '';
        $node = $element->parentNode;
    
        while(true) {
            $node = $node->nextSibling;
            if(!$node) {
                break;
            }
            if($node->nodeName == 'p') {
                $output .= $node->nodeValue;
            }
            if($node->nodeName == 'h2') {
                break;
            }
        }
    
        return $output;
    }
    
    //Here I am dispalying the output in bold text
    var_dump(getElementByIdAsString('https://en.wikipedia.org/wiki/A_Brief_History_of_Time', 'Summary'));
    

    您可能还可以使用 xPaths 或只使用整个响应并使用正则表达式剪切任何您想要的内容

    【讨论】:

    • Liszka 这次它没有返回任何错误,但是我得到一个没有任何内容的空白页面.. 无论如何可以在不使用 id 的情况下提取特定内容
    • 当我运行这段代码时,我得到了“Summary”所以基本上我认为它在你使用你的作为 getElementById 的功能(因此与在 chrome 控制台中使用 $("#Summary") 的效果相同。您要达到什么目的?也许尝试 var_dump 输出除了 echo 它?var_dump(getElementByIdAsString('en.wikipedia.org/wiki/A_Brief_History_of_Time', 'Summary '));
    • 我只是想提取摘要选项卡下的文本。
    • 很棒的家伙 :-) 非常感谢 :-)
    • 没问题,我的荣幸:)
    猜你喜欢
    • 1970-01-01
    • 2012-01-31
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2012-02-09
    相关资源
    最近更新 更多