Php 我在寻找一种更好的方法,用curl从url中抓取内容

Php 我在寻找一种更好的方法,用curl从url中抓取内容,php,parsing,curl,screen-scraping,Php,Parsing,Curl,Screen Scraping,我正在寻找一种更好的方法来使用curl从url中抓取内容,我希望您对多线程或其他想法有所了解。我喜欢保存5.000.000多个站点的html代码 函数curl\u下载($Url){ 如果(!function_存在('curl_init')){ die('对不起,没有安装卷曲!'); } $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,$URL); curl_setopt($ch,CURLOPT_REFERER,”http://www.url.de/?a

我正在寻找一种更好的方法来使用curl从url中抓取内容,我希望您对多线程或其他想法有所了解。我喜欢保存5.000.000多个站点的html代码

函数curl\u下载($Url){
如果(!function_存在('curl_init')){
die('对不起,没有安装卷曲!');
}
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$URL);
curl_setopt($ch,CURLOPT_REFERER,”http://www.url.de/?aktion=suche");
curl_setopt($ch,CURLOPT_USERAGENT,“Mozilla/1.0”);
curl_setopt($ch,CURLOPT_头,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_超时,10);
$output=curl\u exec($ch);
卷曲关闭($ch);
返回$output;
}
$i=“1”;

而($i我正在使用多线程curl快速检查许多代理

这是我为您的需要准备的代码

$mc = curl_multi_init();
$i=1;
while ($i <= 450000) {
    $thread_no = $i;
    $c [$thread_no] = curl_init();
    curl_setopt($c [$thread_no], CURLOPT_URL, 'http://www.url.de/id='.$i.'&land=be');
    curl_setopt($c [$thread_no], CURLOPT_HEADER, 0);
    curl_setopt($c [$thread_no], CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c [$thread_no], CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($c [$thread_no], CURLOPT_TIMEOUT, 10);
    curl_multi_add_handle($mc, $c [$thread_no]);
    $i++;
}

do {
    while (($execrun = curl_multi_exec($mc, $running)) == CURLM_CALL_MULTI_PERFORM) ;
    if ($execrun != CURLM_OK) break;
    while ($done = curl_multi_info_read($mc)) {
        $html=curl_multi_getcontent($done['handle']);
        mysql_query("INSERT INTO hb (content) VALUES('$html')");
        curl_multi_remove_handle($mc, $done ['handle']);
    }
} while ($running);
curl_multi_close($mc);
$mc=curl\u multi\u init();
$i=1;

虽然($i多API不是多线程的,它在同一个线程中执行多个传输。这是真的,当我在45000和@Gimo中更改$i时,可能您当时达到了输出并发连接的限制,在我看来,您应该在每个线程中创建另一个循环,例如50000,并从cmd运行脚本。@DanielStenberg绝对正确,我在b中写的广告词。@diaraf当我一次尝试超过10000个时,mysql数据库中只有空字段
$proxies = $proxyL;
$mc = curl_multi_init();
for ($thread_no = 0; $thread_no < count($proxies); $thread_no++) {
    $c [$thread_no] = curl_init();
    curl_setopt($c [$thread_no], CURLOPT_URL, SERVER_URL . '/checkProxy.php');
    curl_setopt($c [$thread_no], CURLOPT_HEADER, 0);
    curl_setopt($c [$thread_no], CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c [$thread_no], CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($c [$thread_no], CURLOPT_TIMEOUT, 10);
    curl_setopt($c [$thread_no], CURLOPT_PROXY, trim($proxies [$thread_no]));
    curl_setopt($c [$thread_no], CURLOPT_PROXYTYPE, 0);
    curl_multi_add_handle($mc, $c [$thread_no]);
}

do {
    while (($execrun = curl_multi_exec($mc, $running)) == CURLM_CALL_MULTI_PERFORM) ;
    if ($execrun != CURLM_OK) break;
    while ($done = curl_multi_info_read($mc)) {
        $info = curl_getinfo($done ['handle']);
        if (curl_multi_getcontent($done['handle']) == 'ok') {
            $proxy[] = $proxies [array_search($done['handle'], $c)];
        }
        curl_multi_remove_handle($mc, $done ['handle']);
    }
} while ($running);
curl_multi_close($mc);