繁体   English   中英

PHP Dom抓取大量数据

[英]PHP Dom Scraping large amount of data

我必须从8000页x每页25条记录中收集一些数据。 大约有200.000条记录。 问题是服务器在一段时间后拒绝了我的请求。 尽管我听说它运行起来很慢,但是我使用simple_html_dom作为抓取的库。 这是示例数据:

<table>
<tr>
<td width="50%" valign="top" style="font-size:12px;border-bottom:1px dashed #a2a2a2;">Data1</td>
<td width="50%" valign="top" style="font-size:12px;border-bottom:1px dashed #a2a2a2;">Data2</td>
</tr>
<tr>
<td width="50%" valign="top" style="font-size:12px;border-bottom:1px dashed #a2a2a2;">Data3</td>
<td width="50%" valign="top" style="font-size:12px;border-bottom:1px dashed #a2a2a2;">Data4</td>
</tr>
</table>

而php抓取脚本是:

<?php

$fileName = 'output.csv';

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename={$fileName}");
header("Expires: 0");
header("Pragma: public");

$fh = @fopen('php://output', 'w');


ini_set('max_execution_time', 300000000000);

include("simple_html_dom.php");

for ($i = 1; $i <= 8846; $i++) {

    scrapeThePage('url_to_scrape/?page=' . $i);
    if ($i % 2 == 0)
        sleep(10);

}

function scrapeThePage($page)
{

    global $theData;


    $html = new simple_html_dom();
    $html->load_file($page);

    foreach ($html->find('table tr') as $row) {
        $rowData = array();
        foreach ($row->find('td[style="font-size:12px;border-bottom:1px dashed #a2a2a2;"]') as $cell) {
            $rowData[] = $cell->innertext;

        }

        $theData[] = $rowData;
    }
}

foreach (array_filter($theData) as $fields) {
    fputcsv($fh, $fields);
}
fclose($fh);
exit();

?>

如您所见,我在for循环中添加了10秒的睡眠间隔,因此我不会对服务器施加压力。 当它提示我下载CSV时,其中包含以下几行:

警告 :file_get_contents(url_to_scrape /?page = 8846):无法打开流:HTTP请求失败! HTTP / 1.0 500内部服务器错误致命错误 :在第1113行的D:\\ www \\ htdocs \\ ucmr \\ simple_html_dom.php中的非对象上调用成员函数find()

8846页面确实存在,它是脚本的最后一页。 页码在上述错误中有所不同,因此有时例如在800页会出现错误。 有人可以让我知道在这种情况下我在做什么错。 任何意见将是有益的。

致命错误可能是因为$html$row不是对象,它变为null 您应该始终尝试检查对象是否正确创建。 也许还有方法$html->load_file($page); 如果加载页面失败,则返回false。

也要熟悉instanceof有时变得很有帮助。

另一个编辑:您的代码完全没有数据验证。 没有地方可以检查未初始化的变量,已卸载的对象或执行有错误的方法。 您应该始终在代码中使用这些代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM