gpt4 book ai didi

python - 通过 Python 访问响应头位置

转载 作者:行者123 更新时间:2023-12-05 04:10:03 25 4
gpt4 key购买 nike

我目前正在尝试访问从 GET 请求到 url 的响应 header 中的位置字段:https://dbr.ee/aUJA/d ?。目前,我已经可以通过这段Python代码查看location字段:

import requests
r = requests.get('hhttps://dbr.ee/aUJA/d?', allow_redirects=False, headers={'User-Agent': 'Mozilla/5.0'})
print r.headers

但是输出的是错误的location字段

{'Status': '302 Found', 'X-Request-Id': '9e968067-1bee-4cc9-9305-19d45d5cb6ea', 'X-XSS-Protection': '1; mode=block', 'X-Content-Type-Options': 'nosniff', 'Transfer-Encoding': 'chunked', 'Set-Cookie': '__cfduid=d21c538fd46c153a046bf461ca281978d1499637583; expires=Mon, 09-Jul-18 21:59:43 GMT; path=/; domain=.dbr.ee; HttpOnly, ahoy_visitor=f4f1c08c-add3-45c0-8325-675b1caf3048; path=/; expires=Tue, 09 Jul 2019 21:59:44 -0000, ahoy_visit=cdbb4ca8-3272-473c-8562-03596d88ec0f; path=/; expires=Mon, 10 Jul 2017 01:59:44 -0000, ahoy_track=true; path=/, SERVERID=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/', 'X-Runtime': '0.006820', 'Server': 'cloudflare-nginx', 'Connection': 'keep-alive', 'Location': 'hhttps://dbr.ee/aUJA', 'Cache-Control': 'no-cache', 'Date': 'Sun, 09 Jul 2017 21:59:44 GMT', 'X-Frame-Options': 'SAMEORIGIN', 'Content-Type': 'text/html; charset=utf-8', 'CF-RAY': '37be8d52fdc83822-ATL'}

这是:

'Location': 'hhttps://dbr.ee/aUJA'

在网站上,实际的响应头是这样的(通过 Chrome 开发者工具查看)

cache-control:no-cache cf-ray:37be8bacacb437d4-ATL content-type:text/html; charset=utf-8 date:Sun, 09 Jul 2017 21:58:36 GMT location:hhttps://s.dbr.ee/sffc/python%2Dlogo%2Dmaster%2Dv3%2DTM.png.zip?temp_url_sig=41ebabb749293a6fe3f3ec82c5ab8ec01b0ed053&temp_url_expires=1499637816&filename=python-logo-master-v3-TM.png.zip;&attachment server:cloudflare-nginx set-cookie:ahoy_visit=f7d15e42-155c-443f-a637-22c3681863a5; path=/; expires=Mon, 10 Jul 2017 01:58:36 -0000 set-cookie:_dbree_session=U2x6akdCbUJ4c28wdW9MeUFYOXo1QUVxLzV3ZVNxcGtTWW1jbVdkWEdPOWZPMWFiOEl4M0VWY1dOWGNYTjNubEJoVWJHejRCTlQwQlkwL0UrM09QallTMzhFZlU3RFBBTDZxaW9xcGRMeXNlQS9mZFByYTZQWTM0ZlBHMU50ekhhTkt1bjZENXJHRnc2a3dWeGY2d3BBPT0tLVNKOTJnL0Q3SjloWEc0MTZqTnRPNFE9PQ%3D%3D--2dd8f3e77a673f385c9a231af426b55f1d1f71c0; domain=dbr.ee; path=/; HttpOnly set-cookie:SERVERID=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/ status:302 status:302 Found x-content-type-options:nosniff x-frame-options:SAMEORIGIN x-request-id:f57f3ca7-c7aa-4449-a2d7-7b5014010d0f x-runtime:0.015892 x-xss-protection:1; mode=block

位置在哪里

location:hhttps://s.dbr.ee/sffc/python%2Dlogo%2Dmaster%2Dv3%2DTM.png.zip?temp_url_sig=41ebabb749293a6fe3f3ec82c5ab8ec01b0ed053&temp_url_expires=1499637816&filename=python-logo-master-v3-TM.png.zip;&attachment

这是我试图用 Python 抓取的下载链接。单击“直接下载”按钮后,这会出现在开发人员工具中。

如何获取 header 以在 Python 中显示正确的字段位置?

*由于不允许我发布超过 2 个链接,链接已在 http 前面用 h 修改,但对于问题的上下文是必需的

最佳答案

看起来问题是缺少引用 header 。一旦我将它添加到您的代码中,我就会得到适当的 302 重定向响应,以及正确的 Location header :

import requests
r = requests.get('https://dbr.ee/aUJA/d?', allow_redirects=False, headers={
'Referer': 'https://dbr.ee/aUJA'
})
print(r.headers)

产生:

{'Date': 'Sun, 09 Jul 2017 23:44:55 GMT', 'Content-Type': 'text/html; charset=utf-8', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Set-Cookie': '__cfduid=d071cba66cc515ca7f2bc620362c6d46d1499643895; expires=Mon, 09-Jul-18 23:44:55 GMT; path=/; domain=.dbr.ee; HttpOnly, ahoy_visitor=64d9f580-781e-4037-8951-ce57b73df720; path=/; expires=Tue, 09 Jul 2019 23:44:55 -0000, ahoy_visit=802132cc-4e0e-4089-9be5-49f05223f567; path=/; expires=Mon, 10 Jul 2017 03:44:55 -0000, SERVERID=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/', 'Status': '302 Found', 'Cache-Control': 'no-cache', 'X-XSS-Protection': '1; mode=block', 'X-Request-Id': '14a0d0df-c14d-477d-b87c-b6edb823619c', 'Location': 'https://s.dbr.ee/sffc/python%2Dlogo%2Dmaster%2Dv3%2DTM.png.zip?temp_url_sig=084b2b71c8c12df993d528e991a5b44e46e974ef&temp_url_expires=1499644195&filename=python-logo-master-v3-TM.png.zip;&attachment', 'X-Runtime': '0.006968', 'X-Frame-Options': 'SAMEORIGIN', 'X-Content-Type-Options': 'nosniff', 'Server': 'cloudflare-nginx', 'CF-RAY': '37bf2769fda80fa5-YYZ'}

关于python - 通过 Python 访问响应头位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45001501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com