python - python 请求模块和 curl 的不同结果

标签 python redirect curl python-requests

当使用 CURL 时,我得到了结果。这是一个 302 重定向

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 值?

最佳答案

res = requests.get(url, allow_redirects=False)

没有 proxies=...,我得到以下输出。

status_code:  302
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-length': '159',
 'content-type': 'text/html; charset=utf-8',
 'date': 'Mon, 17 Jun 2013 16:32:15 GMT',
 'expires': 'Mon, 17 Jun 2013 16:32:15 GMT',
 'location': 'http://www.youtube.com/watch?v=xdeFB7I0YH4',
 'pragma': 'no-cache',
 'server': 'Microsoft-IIS/7.0',
 'vary': 'Accept-Encoding',
 'x-aspnet-version': '4.0.30319',
 'x-aspnetmvc-version': '3.0',
 'x-powered-by': 'ASP.NET'}
history:  []

使用的请求:0.13.2

对于请求 1.2.3,我得到了类似的结果。

关于python - python 请求模块和 curl 的不同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17152296/

相关文章:

http - Akka-http POST 结果为 "The server-side HTTP version is not supported"

javascript - 用 pako(javascript 中的 zlib)压缩,用 zlib(python)解压不起作用

python - S3.Client.upload_file() 和 S3.Client.upload_fileobj() 有什么区别?

python - 将 Pandas 数据框导出到 Excel 多表文件的正确方法是什么?

c# - ASP.NET MVC 重定向 OnEnd

javascript - 柏树中的应用程序重定向,外部没有

python - 删除后 SQLAlchemy 仍然能够从 session 中获取对象

c++ - 为 shell 输出重定向创建文件

javascript - 使用 Cookies 中间件给出未定义的值

redirect - 不使用-v则不显示301 curl