Javascript 使用php刮取主要内容

Javascript 使用php刮取主要内容,javascript,php,jquery,html,regex,Javascript,Php,Jquery,Html,Regex,我正在构建一个导入工具,就像media.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)

我正在构建一个导入工具,就像media.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;
include('includes/import/simple_html_dom.php');
//从URL或文件获取DOM
$html=file\u get\u html('https://neilpatel.com/blog/starting-over/');
//查找所有链接
foreach($html->find('a')作为$e)
echo$e->href
'; //查找所有图像 foreach($html->find('img')作为$e) echo$e->src'
'; //查找带有完整标记的所有图像 foreach($html->find('img')作为$e) echo$e->outertext。“
'; //查找id=gbar的所有div标记 foreach($html->find('div#gbar')作为$e) 回显$e->innertext。“
'; //查找class=gb1的所有跨度标记 foreach($html->find('span.gb1')作为$e) echo$e->outertext。“
'; //查找属性align=center的所有td标记 foreach($html->find('td[align=center]”)作为$e) 回显$e->innertext。“
'; //从表中提取文本 echo$html->find('td[align=“center”]”,1)->纯文本。

; //从HTML中提取文本 echo$html->纯文本;
但是,这种对整个页面的刮取是不是可以像媒体导入工具对任何链接所做的那样,只找到并刮取主要内容


请解决这个问题,我怎样才能达到这样的效果呢?我不太清楚你在问什么。。但我会试试看

您正试图识别主要内容区域—只获取所需的信息,而不包含任何垃圾或不需要的内容

我的方法是使用格式良好的HTML页面的通用结构和良好实践。考虑这一点:

  • 主文章将封装在页面上唯一的
    article
    标记中
  • 文章上的
    H1
    标记将作为其标题
  • 我们知道使用了一些重复ID,例如(主内容、主文章等)
总结目标上的这些规则,并构建一个按优先级排序的标识符列表->然后您可以尝试解析目标,直到找到其中一个标识符-这表示您标识了主要内容区域

下面是一个示例->使用您提供的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…

希望我能帮上忙。

请告诉我们您到目前为止为解决问题所做的努力。主要问题可能是您如何认识主要内容,如果你能定义如何识别它,那会很有帮助。我已经尝试了上面的代码,得到了整个页面,我只想要主要的内容,比如从哪里开始和ends@NigelRen是的,你是对的,但是我们想为每个url创建一个通用工具,这样我就可以确定主要文章的开始和结束位置,就像只识别文本一样会议内容article@NigelRen我希望你能理解我的观点,每个url内容,标签都是不同的,所以我如何识别文章内容的开始和结束谢谢你的帮助,我们可以选择这个选项,但是url内容不包含任何上述标签,我们有没有其他方法可以在jquery中做到这一点,Javascript您是否使用过media.com故事导入工具?