【发布时间】:2013-06-17 16:23:44
【问题描述】:
当使用 CURL 时,我得到了结果。这是302 redirect。
hhr@hhr:~$ curl -I http://www.vevo.com/watch/kesha/crazy-kids/USRV81300226
HTTP/1.1 302 Moved Temporarily
Content-Length: 159
Content-Type: text/html; charset=utf-8
Location: http://www.youtube.com/watch?v=xdeFB7I0YH4
Server: Microsoft-IIS/7.0
X-AspNetMvc-Version: 3.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Expires: Mon, 17 Jun 2013 15:33:41 GMT
Cache-Control: max-age=0, no-cache, no-store
Pragma: no-cache
Date: Mon, 17 Jun 2013 15:33:41 GMT
Connection: keep-alive
当使用Python requests模块时,结果如下。这是200 ok。
import requests
import pprint
url = 'http://www.vevo.com/watch/kesha/crazy-kids/USRV81300226'
res = requests.get(url, proxies={'http': '127.0.0.1:8087'}, allow_redirects=False)
print 'status_code: ', res.status_code
print 'response_url: ', res.url
print 'headers: '
pprint.pprint(res.headers)
print 'history: ', res.history
还有输出:
status_code: 200
response_url: http://www.vevo.com/watch/kesha/crazy-kids/USRV81300226
headers:
{'access-control-allow-origin': '*',
'cache-control': 'max-age=0, no-cache, no-store',
'connection': 'keep-alive',
'content-encoding': 'deflate',
'content-length': '12310',
'content-type': 'text/html; charset=utf-8',
'date': 'Mon, 17 Jun 2013 16:17:35 GMT',
'expires': 'Mon, 17 Jun 2013 16:17:35 GMT',
'pragma': 'no-cache',
'server': 'Microsoft-IIS/7.0',
'vary': 'Accept-Encoding',
'via': 'HTTP/1.1 GWA',
'x-aspnet-version': '4.0.30319',
'x-aspnetmvc-version': '3.0',
'x-powered-by': 'ASP.NET'}
history: []
为什么结果不一样?
如何才能使用 Python 在 CURL 输出中获取 Location: http://www.youtube.com/watch?v=xdeFB7I0YH4 值?
【问题讨论】:
-
您需要比较请求标头;服务器根据请求标头做出不同的响应。什么标头切换此行为完全取决于服务器,一个黑匣子。
-
我用你的 python 代码得到 302。我省略了
proxies=...参数。 -
@falsetru:有趣;我得到了 200。
-
@MartijnPieters,也许 www 服务器会根据客户区域响应?
-
@MartijnPieters 如何检查请求标头?
标签: python redirect curl python-requests