javascript - 使用 php 抓取主要内容

标签 javascript php jquery html regex

我正在构建一个导入工具,就像 medium.com 故事导入工具一样,到目前为止我已经使用了这段代码

include('includes/import/simple_html_dom.php');
// get DOM from URL or file
$html = file_get_html('https://neilpatel.com/blog/starting-over/');

// find all link
foreach($html->find('a') as $e) 
    echo $e->href . '<br>';

// find all image
foreach($html->find('img') as $e)
    echo $e->src . '<br>';

// find all image with full tag
foreach($html->find('img') as $e)
    echo $e->outertext . '<br>';

// find all div tags with id=gbar
foreach($html->find('div#gbar') as $e)
    echo $e->innertext . '<br>';

// find all span tags with class=gb1
foreach($html->find('span.gb1') as $e)
    echo $e->outertext . '<br>';

// find all td tags with attribite align=center
foreach($html->find('td[align=center]') as $e)
    echo $e->innertext . '<br>';

// extract text from table
echo $html->find('td[align="center"]', 1)->plaintext.'<br><hr>';

// extract text from HTML
echo $html->plaintext;

但是这种抓取整个页面是否有可能只找到并抓取主要内容,就像媒体导入工具对任何链接所做的那样

请解决这个问题,我怎样才能达到这种结果

最佳答案

我不完全确定你在问什么/试图做什么..但我会试一试。

您正在尝试识别主要内容区域 - 仅抓取所需的信息而没有任何垃圾或不需要的内容。

我的方法是使用格式良好的 HTML 页面的通用结构和良好做法。考虑一下:

  • 主要文章将封装在页面上唯一的ARTICLE标签中。
  • 文章中的 H1 标签将作为标题。
  • 我们知道使用了一些重复 ID,例如(main_content、main_article 等)。

总结您的目标的这些规则并构建一个按优先级排序的标识符列表 -> 然后您可以尝试解析目标直到找到其中一个标识符 - 这表明您确定了主要内容区域。

这是一个示例 -> 使用您提供的 URL:

$search_logic = [
    "#main_content",
    "#main_article",
    "#main",
    "article",
];

// get DOM from URL or file
$html = file_get_contents('https://neilpatel.com/blog/starting-over/');
$dom = new DOMDocument ();
@$dom->loadHTML($html);

//
foreach ($search_logic as $logic) {

    $main_container = null;

    //Search by ID or By tag name:
    if ($logic[0] === "#") {
        //Serch by ID:
        $main_container = $dom->getElementById(ltrim($logic, '#'));
    } else {
        //Serch by tag name:
        $main_container = $dom->getElementsByTagName($logic);
    }

    //Do we have results:
    if (!empty($main_container)) {

        echo "> Found main part identified by: ".$logic."\n";
        $article = isset($main_container->length) ? $main_container[0] : $main_container; // Normalize the container.

        //Parse the $main_container:
        echo " - Example get the title:\n";
        echo "\t".$article->getElementsByTagName("h1")[0]->textContent."\n\n";

        //You can stop the iteration:
        //break;

    } else {
        echo "> Nothing on the page containing: ".$logic."\n\n";
    }
}

如您所见,未找到 ID 的第一个,因此我们继续在列表中尝试直到找到我们想要的结果 -> 一组好的标记名/ID 就足够了。

结果如下:

> Nothing on the page containing: #main_content

> Nothing on the page containing: #main_article

> Found main part identified by: #main
 - Example get the title:
    If I Had to Start All Over Again, I Would…

> Found main part identified by: article
 - Example get the title:
    If I Had to Start All Over Again, I Would…

希望我有所帮助。

关于javascript - 使用 php 抓取主要内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55064292/

相关文章:

javascript - 使用 Jquery Ajax PHP 提交表单

javascript - Flex - JavaScript 适用于 bin-debug,但不适用于已部署的服务器

php - 与 Arduino、PHP 和 OpenWrt 的串行连接。漏洞?

javascript - 隐藏没有类别的元素

javascript - 异步操作后运行 jQuery Tablesorter 两次

javascript - 似乎无法正确设置全局变量

javascript - 为什么我不能将函数放入 jQuery(document).ready(function() { }

php - PHP中的iframe与来自SQL的字符串

php - 一次初始化数组共享值

javascript - jquery - 像插件一样在选定的 div 中添加 html 内容