【问题标题】:Scraping attempts getting 403 error抓取尝试得到 403 错误
【发布时间】:2018-04-25 02:52:10
【问题描述】:

我正在尝试抓取一个网站,但无论我尝试什么都会收到 403 Forbidden 错误:

  1. wget
  2. CURL(命令行和 PHP)
  3. Perl WWW::Mechanize
  4. PhantomJS

我尝试了上述所有方法,无论是否使用代理,更改用户代理并添加引荐来源标头。

我什至从我的 Chrome 浏览器复制了请求标头,并尝试使用 PHP Curl 发送我的请求,但我仍然收到 403 Forbidden 错误。

关于触发网站阻止请求的原因以及如何绕过的任何意见或建议?

PHP CURL 示例:

$url ='https://www.vitacost.com/productResults.aspx?allCategories=true&N=1318723&isrc=vitacostbrands%3aquadblock%3asupplements&scrolling=true&No=40&_=1510475982858';
$headers = array(
            'accept:application/json, text/javascript, */*; q=0.01',
            'accept-encoding:gzip, deflate, br',
            'accept-language:en-US,en;q=0.9',               
            'referer:https://www.vitacost.com/productResults.aspx?allCategories=true&N=1318723&isrc=vitacostbrands:quadblock:supplements',
            'user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36',
            'x-requested-with:XMLHttpRequest',
);

$res = curl_get($url,$headers);
print $res;
exit;

function curl_get($url,$headers=array(),$useragent=''){ 
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, true);           
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);   
    curl_setopt($curl, CURLOPT_ENCODING, '');            
    if($useragent)curl_setopt($curl, CURLOPT_USERAGENT,$useragent);             
    if($headers)curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

    $response = curl_exec($curl);       

    $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
    $header = substr($response, 0, $header_size);
    $response = substr($response, $header_size);


    curl_close($curl);  
    return $response;
 }

这是我经常得到的回应:

<HTML><HEAD>
<TITLE>Access Denied</TITLE>
</HEAD><BODY>
<H1>Access Denied</H1>

You don't have permission to access     

  "http&#58;&#47;&#47;www&#46;vitacost&#46;com&#47;productResults&#46;aspx&#63;" 
on this server.<P>
Reference&#32;&#35;18&#46;55f50717&#46;1510477424&#46;2a24bbad
</BODY>
</HTML>

【问题讨论】:

  • 你设置用户代理的方式错误...应该使用CURLOPT_USERAGENT选项
  • @FlashThunder,“CURLOPT_USERAGENT”选项在那里,当我发送 $useragent 变量时它被设置。我试过两种方式设置用户代理,使用标题和“CURLOPT_USERAGENT”。我不认为这与为什么这不起作用有关。

标签: php perl curl phantomjs scrape


【解决方案1】:

首先,请注意该网站不喜欢网络抓取。正如@KeepCalmAndCarryOn 在评论中指出的那样,该网站有一个/robots.txt,它明确要求机器人不要抓取网站的特定部分,包括您想要抓取的部分。虽然没有法律约束力,但好公民会遵守此类要求。

此外,该网站似乎采用了明确的保护措施来防止抓取,并试图确保这确实是一个浏览器。看起来该站点位于 Akamai CDN 的后面,所以反抓取保护可能来自此 CDN。

但我接受了 Firefox 发送的请求(有效),然后尝试尽可能简化它。以下内容目前适用于我,但如果网站更新其浏览器检测,当然可能会失败:

use strict;
use warnings;
use IO::Socket::SSL;

(my $rq = <<'RQ') =~s{\r?\n}{\r\n}g;
GET /productResults.aspx?allCategories=true&N=1318723&isrc=vitacostbrands%3aquadblock%3asupplements&scrolling=true&No=40&_=151047598285 HTTP/1.1
Host: www.vitacost.com
Accept: */*
Accept-Language: en-US
Connection: keep-alive

RQ

my $cl = IO::Socket::SSL->new('www.vitacost.com:443') or die;
print $cl $rq;
my $hdr = '';
while (<$cl>) {
    $hdr .= $_;
    last if $_ eq "\r\n";
}
warn "[header done]\n";
my $len = $hdr =~m{^Content-length:\s*(\d+)}mi && $1 or die "no length";
read($cl,my $buf,$len);
print $buf;

有趣的是,如果我删除 Accept 标头,我会得到 403 Forbidden。如果我改为删除Accept-Language,它只会挂起。而且有趣的是,它似乎不需要 User-Agent 标头。

编辑:看起来机器人检测也使用发件人的源 IP 作为功能。虽然上面的代码适用于两个不同的系统,但它无法适用于第三个系统(托管在 Digitalocean)并且只是挂起。

【讨论】:

  • 如果您查看vitacost.com/robots.txt,您会发现他们不想抓取productResults - 这都是建议,但成为一个好公民是件好事en.wikipedia.org/wiki/Robots_exclusion_standard
  • @KeepCalmAndCarryOn:感谢您的意见。我已经编辑了答案以包含它的本质。
  • @SteffenUllrich,此时我真的很想弄清楚为什么我的浏览器请求有效,而我使用的所有其他工具却没有,即使我发送相同的标头。我尝试在我的服务器和个人计算机上运行您的代码,但它只是挂起。删除 Accept 标头会导致 403 Forbidden 是正确的,但我仍然无法让服务器返回响应。这可能与 SSL 证书有关吗?
  • @user735247:这与证书无关。我宁愿建议将请求的源 IP 包含在机器人检测中。它适用于我尝试过的两个系统,但它在第三个系统(DigitalOcean 服务器)中失败。如果您已尝试使用浏览器成功访问站点的系统中的代码,它也可能会成功。
  • 关于挂起...与 IP 无关...在 Debian Lynx 上相同的 ip - 在 Windows Opera 上挂起 - 有效
猜你喜欢
  • 2018-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多