【问题标题】:reddit data collecting stops halfway through the php loopreddit 数据收集在 php 循环中途停止
【发布时间】:2016-08-30 07:46:14
【问题描述】:

伙计们,我已经使用此代码从 reddit.com 帖子中获取数据...这工作正常,直到 1500 个帖子(我需要获取 10000 个数据)。然后它给出一个错误提示

array_merge(): 参数 #1 不是数组

这个错误从我写array_merge的那一行触发

代码

<?php
$count = 0;
for($digit=0; $count<2001; $digit+=1){

        $jsondata = trim(file_get_contents("http://www.reddit.com/new/.json?count=$count&limit=100"));

        $json = json_decode($jsondata, true);

        $postdata[$digit] = $json['data']['children'];
        $mergedArray = array_merge($mergedArray, $postdata[$digit]);

        }

        foreach($mergedArray as $livedata){

            $output .= $livedata["data"]["ups"].", ";
            $output .= '"'.$livedata["data"]["title"].'", ';
            $output .= $livedata["data"]["id"].", ";
            $output .= $livedata["data"]["num_comments"].", ";
            $output .= $livedata["data"]["domain"]."\n\r";
            $output .= "<br />";

        }
        echo $output;
        ?>

任何人都可以发现错误并告诉我一种方法让我可以不间断地发布 10000 个帖子

【问题讨论】:

  • 有人帮帮我吗??

标签: php loops reddit array-merge


【解决方案1】:

如果您提供了在发生这种情况时获得的响应正文,将会非常有帮助;您可能收到了错误消息。

我不知道这是否是您现在遇到错误的原因,但您的方法有两个问题,您迟早会遇到。

首先,根据the API rules,您可能应该作为应用程序进行身份验证,并且应该指定用户代理;否则你会受到很大的限制。您现在很有可能收到 429,但没有检查它们。

其次,reddit 上的列表页面仅涵盖 1,000 项,因此您将无法直接从中获取 10,000 项。如果您确实需要 10k 个项目,您有两个主要选择:

  1. 在一段时间内一次收集 1000 项数据。
  2. 非常喜欢搜索 API 和时间戳,以获得可以拼凑在一起的许多数据片段。

您还没有具体说明为什么需要这些数据(或者为什么需要 10,000 个样本),但如果您只是在寻找一些用于执行分析的 reddit 帖子,其他人已经创建了 @ 的数据集987654322@和comments

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 2012-06-19
    • 1970-01-01
    • 2018-02-20
    • 2021-09-15
    相关资源
    最近更新 更多