Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP简单HTML DOM解析器:获取所有帖子_Php_Dom_Simple Html Dom - Fatal编程技术网

PHP简单HTML DOM解析器:获取所有帖子

PHP简单HTML DOM解析器:获取所有帖子,php,dom,simple-html-dom,Php,Dom,Simple Html Dom,我有一个简单的任务。 获取页面中的所有文章,以及每篇文章的所有图片 今天我决定用 作为示例,我使用了以下代码: <?php include("simple_html_dom.php"); $sitesToCheck = array( array( 'url' => 'http://googleblog.blogspot.ru/', 'search_element' => 'h2.title a', 'get_eleme

我有一个简单的任务。 获取页面中的所有文章,以及每篇文章的所有图片

今天我决定用

作为示例,我使用了以下代码:

<?php

include("simple_html_dom.php");

$sitesToCheck = array(
    array(
        'url' => 'http://googleblog.blogspot.ru/',
        'search_element' => 'h2.title a',
        'get_element' => 'div.post-content'
    ),
    array(
        // 'url' => '',            // Site address with a list of of articles
        // 'search_element' => '', // Link of Article on the site
        // 'get_element' => ''     // desired content
    )
);

$s = microtime(true);

foreach($sitesToCheck as $site)
{
    $html = file_get_html($site['url']);

    foreach($html->find($site['search_element']) as $link)
    {
        $content   = '';
        $savePath  = 'cachedPages/'.md5($site['url']).'/';
        $fileName  = md5($link->href);

        if ( ! file_exists($savePath.$fileName))
        {
            $post_for_scan = file_get_html($link->href);

            foreach($post_for_scan->find($site["get_element"]) as $element)
            {
                $content .= $element->plaintext . PHP_EOL;
            }

            if ( ! file_exists($savePath) && ! mkdir($savePath, 0, true))
            {
                die('Unable to create directory ...');
            }

            file_put_contents($savePath.$fileName, $content);
        }
    }
}

$e = microtime(true);

echo $e-$s;

我对这个库也有类似的问题。改用PHP的DOMDocument:

$doc = new DOMDocument;
$doc->loadHTML($html);
$links = $doc->getElementsByTagName('a');
foreach ($links as $link) {
  doSomethingWith($link->getAttribute('href'), $link->nodeValue);
}

关于“简单”部分,请参见下文不过,说真的,上一次我(几个月前)检查它时,simple_html_dom仍然是一堆热气腾腾的东西。DOMDocument+DOMXPath占用了大约1/5的空间和时间。字面上我通过去掉它,将内存使用量和运行时间减少了80%。你不应该过分依赖这一点,但是如果你事先知道某个进程需要很长时间,请尝试
set\u time\u limit(0)
,但是在任何地方都使用它并不是一个好的做法。这将防止php在进程超过最大执行时间(本例中为120秒)时终止进程,并且它将一直运行到完成为止。问题是。。。如果你在程序中犯了一个导致永远运行的错误,那么你的程序将坐在服务器上消耗资源,直到采取手动操作为止。只是为了让我听起来不像一个狂热的憎恨者,简单的html dom可能有一个好处。如果您的HTML被严重损坏,看起来不再像HTML,DOMDocument可能无法很好地处理它。像simple_html_dom这样的库在处理此类垃圾时可能会做得更好,因为它是为处理古怪的标记而设计的。但是,很少有人需要解析一个坏得让DOMDocument无法处理的文档。至少,我从来没有处理过它。@cHao是的,与加载XML不同,HTML不需要格式良好就可以加载。对……因此,任何具有伪能力的解析器都或多或少可以工作。像simple_html_dom这样的解析器,专门用于处理格式错误的html,可能有更好的机会看到作者想要的文档。但是HTML必须被完全破坏,以便有一个更宽容的解析器来证明对资源/性能的影响。谢谢。现在需要了解如何使用诸如
div.post-content
table.wrapper td.content
div p a
等查询来获取项目。