Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 Python请求库返回错误的状态代码_Python 2.7_Http Status Code 403_Python Requests - Fatal编程技术网

Python 2.7 Python请求库返回错误的状态代码

Python 2.7 Python请求库返回错误的状态代码,python-2.7,http-status-code-403,python-requests,Python 2.7,Http Status Code 403,Python Requests,下面的Python代码返回“403”: import requests url = 'http://bedstardirect.co.uk/star-collection-braemar-double-bedstead.html' r = requests.get(url) print r.status_code 但此页面是有效的,脚本应返回“200”,下面的perl脚本也是如此: use WWW::Mechanize; my $mech = WWW::Mechanize->new();

下面的Python代码返回“403”:

import requests
url = 'http://bedstardirect.co.uk/star-collection-braemar-double-bedstead.html'
r = requests.get(url)
print r.status_code
但此页面是有效的,脚本应返回“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";
我还检查了Firefox中的Firebug,所有请求都有“200”状态码


我使用Python Requests v1.2.0。

似乎您的特定服务器需要一个用户代理头

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

编辑:
我的机器请求的默认用户代理为:
python请求/1.2.0 CPython/2.7.4 Darwin/12.3.0


经过一些测试,我发现任何包含单词
python
的用户代理都会在此服务器上失败。

这很奇怪,因为默认情况下请求会发送一个。