Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.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 QueryPath 2.1.2 WAMP抓取脚本只返回5篇文章而不是43篇?暂停?_Php_Scrape_Querypath - Fatal编程技术网

为什么我的PHP QueryPath 2.1.2 WAMP抓取脚本只返回5篇文章而不是43篇?暂停?

为什么我的PHP QueryPath 2.1.2 WAMP抓取脚本只返回5篇文章而不是43篇?暂停?,php,scrape,querypath,Php,Scrape,Querypath,我试图从我的博客中抓取43篇博客文章并将它们存储在数组中,但当我打印数组时,它只返回前5篇(其余为空),而不是全部43篇。为什么?我怎样才能得到全部43个? 我从WAMP上的cmd.exe[命令行]运行此脚本 <?php require 'src/QueryPath/QueryPath.php'; $qp1 = htmlqp('http://myblog.com/blog'); $qp2 = htmlqp('http://myblog.com/blog/Page-2.html

我试图从我的博客中抓取43篇博客文章并将它们存储在数组中,但当我打印数组时,它只返回前5篇(其余为空),而不是全部43篇。为什么?我怎样才能得到全部43个? 我从WAMP上的cmd.exe[命令行]运行此脚本

    <?php

require 'src/QueryPath/QueryPath.php';


$qp1 = htmlqp('http://myblog.com/blog');
$qp2 = htmlqp('http://myblog.com/blog/Page-2.html');
$qp3 = htmlqp('http://myblog.com/blog/Page-3.html');
$qp4 = htmlqp('http://myblog.com/blog/Page-4.html');

foreach ($qp1->find('ol>li a[href],.jbReadon') as $item) {
    $links[] = $item->attr('href');
}

foreach ($qp2->find('ol>li a[href],.jbReadon') as $item) {
    $links[] = $item->attr('href');
}

foreach ($qp3->find('ol>li a[href],.jbReadon') as $item) {
    $links[] = $item->attr('href');
}

foreach ($qp4->find('ol>li a[href],.jbReadon') as $item) {
    $links[] = $item->attr('href');
}


print_r($links);



foreach ($links as $link) {
    $url = "http://myblog.com".$link;

    $content[] = htmlqp($url)->find('.jbIntroText p')->text();
}
print_r($content);




?>

在数组的键5之后,所有值都为空。 [我无法从笔记本电脑或网络上传图像,因此这里有指向cmd.exe屏幕截图的链接]


我显然是一个初学者,因此任何关于如何使此代码更简洁或如何更好地完成我的刮片原型的建议都将不胜感激。欢迎所有建设性的批评:-P

您可能希望在至少一个FOR循环的声明中添加一些打印声明。这里可能会发生一些事情。最有可能的两种情况是:

  • 筛选器可能仅匹配五个项目
  • HTML解析器可能会被某些标记阻塞。在本例中,它将尝试加载尽可能多的HTMLDOM
通过添加一些print语句,您可能可以看到它迭代了多少次


另外,如果你想在你的博客上获取文章列表,阅读RSS或Atom提要可能会更容易(尽管我想它可能没有你需要的所有信息)。

我已经解决了我的问题!!显然,我所需要的只是每次查询/刮取之间的一段时间延迟,因为我的博客是为了保护自己不被大量刮取或其他什么。我所要做的就是重写代码的第二部分,如下所示:

foreach ($links as $link) {
    $url = "http://myblog.com".$link;
    $count = count($links);
    $interval = 2; // Every three times...
    $wait = 2; // Wait two seconds.
        for ($i = 0; $i < $count; ++$i) {
        $content[] = htmlqp($url)->find('.jbIntroText p')->text();
        print_r($content);
            if ($i > 0 && $i % $interval == 0) {
            sleep($wait);
            }

        }
}
foreach($links作为$link){
$url=”http://myblog.com“$link;
$count=计数($links);
$interval=2;//每三次。。。
$wait=2;//等待两秒钟。
对于($i=0;$i<$count;++$i){
$content[]=htmlqp($url)->find('.jbIntroText p')->text();
打印(内容);
如果($i>0&&$i%$interval==0){
睡眠(等待);
}
}
}
感谢Technosophos在这里提出的想法

还感谢我将博客im转换为RSS/Atom提要的想法,因为很多时候博客都没有自己生成的RSS提要