【问题标题】:Whats wrong with my PHP scraper script?我的 PHP 爬虫脚本有什么问题?
【发布时间】:2013-06-07 09:08:08
【问题描述】:

这是我的 PHP 脚本:

<?php

function scrape(){

$f=fopen("list.txt","r") or exit("Unable to open file!");

    while (!feof($f))
{
        $site=stream_get_line($f,4096,"\n");
        $url="www.majesticseo.com/reports/site-explorer/summary/".$site."?IndexDataSource=F";


            $ch = curl_init();
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_URL, $url);
            $data = curl_exec($ch);
            curl_close($ch);



        $regex = '~External Backlinks\s*</p>\s*<p style="font-size: 150%;">\s*<b>(.+?)</b>~';

        $result=preg_match($regex,$data,$match);


        $link_count=$match[1];
        echo($site." ".$link_count);
        echo("</br>"); }

}

$ch=curl_init();

curl_setopt($ch, CURLOPT_URL, 'www.majesticseo.com/account/login');

curl_setopt($ch,CURLOPT_POST,1);

curl_setopt($ch, CURLOPT_POSTFIELDS, 'EmailAddress=myemail@email.com&Password=mypassword123');

curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$store=curl_exec($ch);

scrape();

curl_close($ch);
?>

它的问题是,scrape() 函数和登录部分在单独测试时可以工作,但是,当我想在登录 curl 会话中进行 scrape() 时,它似乎在没有登录的情况下进行了抓取。我知道这一点因为已经达到免登录爬取次数上限,不返回任何数据。

为什么会这样?如何让我的脚本在登录时抓取数据?

【问题讨论】:

    标签: php curl scraper


    【解决方案1】:

    已解决!我更改了代码,因此我只有 1 个 CURL init。我没有意识到我实际上开始了 2 节课!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-04
      • 2014-06-29
      • 1970-01-01
      • 1970-01-01
      • 2019-03-26
      • 1970-01-01
      • 2012-08-08
      相关资源
      最近更新 更多