PHP-获取其他网站内容并解析此内容的最快方法

PHP-获取其他网站内容并解析此内容的最快方法,php,curl,optimization,web-crawler,urlfetch,Php,Curl,Optimization,Web Crawler,Urlfetch,我必须得到一些参数的用户的网站。我可以这样做,因为每个用户都有一个唯一的ID,我可以通过URL搜索用户: 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_use

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

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);
?>

<代码> 如果你真的需要500次查询不同的URL,也许你应该考虑异步方法。上面的问题是,最慢的部分(瓶颈)是curl请求本身。在等待响应时,代码没有执行任何操作


试着看一看(即,你会“几乎一次”发出500个请求,并在响应到来时异步处理它们)。

看一看我之前关于如何划分和征服这类工作的回答


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

这是每天都会发生的还是仅仅是一次性的?一次性的。最后,我想使用CRON启动此脚本,并在时间段内获得所有结果(20.000.000)(500个结果的1秒中断)…因此我应该创建一个包含500个URL的数组,然后在回调函数中根据此回调的响应创建预匹配?@NewbieUser抱歉,我没有时间为您编写完整的解决方案,我只是想指出,最有希望的方法似乎是异步解析/处理某个回调中获取的URL,这样执行就不像:fetch,wait,response,parse/process,fetch另一个,wait。。。但是像fetch1,fetch2,fetch3,response1,parse1,fetch4,response2等等。