【问题标题】:PHP - The fastest way to get content of another website and parse this contentPHP - 获取另一个网站内容并解析此内容的最快方法
【发布时间】:2016-03-28 16:32:49
【问题描述】:

我必须从网站上获取一些用户参数。我可以这样做,因为每个用户都有一个唯一的 ID,我可以通过 URL 搜索用户:

http://page.com/search_user.php?uid=X

所以我在 for() 循环中添加了这个 URL,我试图得到 500 个结果:

<?php

$start = time();
$results = array();

for($i=0; $i<= 500; $i++)
{
    $c = curl_init();
    curl_setopt($c, CURLOPT_URL, 'http://page.com/search_user.php?uid='.$i);
    curl_setopt($c, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; pl; rv:1.9.1.2) Gecko/20090729 desktopsmiley_2_2_5643778701369665_44_71 DS_gamingharbor Firefox/3.5.2 (.NET CLR 3.5.30729)');
    curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    $p = curl_exec($c);
    curl_close($c);

    if ( preg_match('"<span class=\"uname\">(.*?)</span>"si', $p, $matches) )
    {
        $username = $matches[1];
    }
    else
    {
        continue;
    }

    preg_match('"<table cellspacing=\"0\">(.*?)</table>"si', $p, $matches);
    $comments = $matches[1];

    preg_match('"<tr class=\"pos\">(.*?)</tr>"si', $comments, $matches_pos);
    preg_match_all('"<td>([0-9]+)</td>"si', $matches_pos[1], $matches);
    $comments_pos = $matches[1][2];

    preg_match('"<tr class=\"neu\">(.*?)</tr>"si', $comments, $matches_neu);
    preg_match_all('"<td>([0-9]+)</td>"si', $matches_neu[1], $matches);
    $comments_neu = $matches[1][2];

    preg_match('"<tr class=\"neg\">(.*?)</tr>"si', $comments, $matches_neg);
    preg_match_all('"<td>([0-9]+)</td>"si', $matches_neg[1], $matches);
    $comments_neg = $matches[1][2];

    $comments_all = $comments_pos+$comments_neu+$comments_neg;

    $about_me = 0;
    if ( preg_match('"<span>O mnie</span>"si', $p) )
    {
        $about_me = 1;
    }

    $results[] = array('comments' => $comments_all, 'about_me' => $about_me, 'username' => $username);
}

echo 'Generated in: <b>'.(time()-$start).'</b> seconds.<br><br>';
var_dump($results);
?>

最后我得到了结果: - 一切都在 135 秒内生成。

然后我将 curl 替换为 file_get_contents(),得到:155 秒。

获得此结果的方法是否比 curl 更快?我必须从另一个页面获得 20.000.000 个结果,而 135 秒对我来说太长了。

谢谢。

【问题讨论】:

  • 这是每天都会发生的事情还是只是偶尔发生?
  • 一次性。最后,我想使用 CRON 启动此脚本并按时间部分获取所有结果(20.000.000)(500 个结果中断 1 秒)..

标签: php curl optimization web-crawler urlfetch


【解决方案1】:

如果你真的需要查询不同的 URL 500 次,也许你应该考虑异步方法。上面的问题是最慢的部分(瓶颈)是 curl 请求本身。在等待响应时,您的代码什么也不做。

尝试查看PHP asynchronous cURL with callback(即,您将“几乎同时”发出 500 个请求并在响应到来时处理它们 - 异步)。

【讨论】:

  • 所以我应该创建一个包含 500 个 URL 的数组,然后在回调函数中根据此回调的响应创建 preg_match'es?
  • @NewbieUser 对不起,我没有时间为你写完整的解决方案,我只是想指出最有希望的方法似乎是解析/处理获取的 URL一些异步回调,因此执行不像:获取,等待,响应,解析/处理,获取另一个,等待......但是像fetch1,fetch2,fetch3,response1,parse1,fetch4,response2等。跨度>
【解决方案2】:

看看我之前关于如何分治这类工作的回答。

debugging long running PHP script

在你的情况下,我会说你遵循相同的想法,但你会进一步将请求分成 500 个组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多