PHP Scrape文章摘录如可读性

Alf*_*lfo 6 php web-scraping

我已经看到了这个问题,但它并不能满足我的需求.该问题的答案要么是:从元描述标签中提升,第二个是为您已经拥有主体的文章生成摘录.

我想要做的实际上是得到的前几句文章,像可读性一样.这不是最好的方法吗?HTML解析?这是我目前使用的,但这不是很可靠.

function guessExcerpt($url) {
    $html = file_get_contents_curl($url);

    $doc = new DOMDocument();
    @$doc->loadHTML($html);

    $metas = $doc->getElementsByTagName('meta');

    for ($i = 0; $i < $metas->length; $i++)
    {
        $meta = $metas->item($i);
        if($meta->getAttribute('name') == 'description')
            $description = $meta->getAttribute('content');

    }

    return $description;
}

function file_get_contents_curl($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}
Run Code Online (Sandbox Code Playgroud)

Muh*_*rar 8

这是PHP中的可读性端口:https://github.com/feelinglucky/php-readability.就试一试吧.提取结果类似于Readability(因为它实现了Readability的算法).

require 'lib/Readability.inc.php';

$html = file_get_contents_curl($url);

$Readability     = new Readability($html, $html_input_charset); // default charset is utf-8
$ReadabilityData = $Readability->getContent();

$title   = $ReadabilityData['title'];
$content = $ReadabilityData['content'];
Run Code Online (Sandbox Code Playgroud)

然后你可以使用一些句子$content作为摘录.

  • @andrewliu我用来获取缩略图的方法是多一点代码.我使用`preg_match_all`和`/<img.+src =([\'"])([^ \'"] +)\ 1.*?\ /?>/i`并构建了一个包含所有图像的数组.通过解析上面示例中的`$ url`,我可以通过确定baseurl将相对图像路径转换为绝对路径.然后我呈现一个带有绝对图像路径的数组供用户选择.您可以使用简单的脚本创建缩略图并将其保存在Web服务器上以显示给用户,或使用HTML进行缩放.**发布一个新问题并粘贴链接,然后我可以给你特定的代码.** (2认同)