简体   繁体   English

Python Requests库返回错误的状态代码

[英]Python Requests library returns wrong status code

The Python code below returns '403': 下面的Python代码返回'403':

import requests
url = 'http://bedstardirect.co.uk/star-collection-braemar-double-bedstead.html'
r = requests.get(url)
print r.status_code

But this page is valid and the script should return '200', as does the perl script below: 但是这个页面是有效的,脚本应该返回'200',下面的perl脚本也是如此:

use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
my $url = 'http://bedstardirect.co.uk/star-collection-braemar-double-bedstead.html';
$mech->get($url);
print $mech->status,"\n";

I have also checked with Firebug in Firefox and all requests have a '200' status code. 我还在Firefox中检查了Firebug,所有请求都有一个'200'状态代码。

I use Python Requests v1.2.0. 我使用Python Requests v1.2.0。

Seems your particular server requires a User-Agent header. 似乎您的特定服务器需要User-Agent标头。

Try: 尝试:
r = requests.get('http://bedstardirect.co.uk/star-collection-braemar-double-bedstead.html', headers={'User-Agent': 'a user agent'})

Edit: 编辑:
The default User-Agent on requests for my machine comes out as: python-requests/1.2.0 CPython/2.7.4 Darwin/12.3.0 我机器请求的默认用户代理如下: python-requests/1.2.0 CPython/2.7.4 Darwin/12.3.0

After some testing I found that any User-Agent that contains the word python will fail on this server. 经过一些测试后,我发现任何包含单词python User-Agent都会在此服务器上失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM