从 python 请求中的 response.headers 获取位置
Posted
技术标签:
【中文标题】从 python 请求中的 response.headers 获取位置【英文标题】:get location from response.headers in python requests 【发布时间】:2021-09-18 20:44:00 【问题描述】:我正在使用 python requests
并执行 post
import requests
response = requests.post('https://petdogs.net/search/?input=abcdefgh',
headers=HEADERS,
allow_redirects=False)
print(response.headers)
这些是我可以在浏览器的开发人员工具中看到的 response
标头中的值,我想获取 location
的值
content-language: en-gb
content-length: 0
content-type: text/html; charset=utf-8
date: Wed, 07 Jul 2021 17:44:52 GMT
location: /product/id=12345/
server: nginx/1.14.0 (Ubuntu)
vary: Accept-Language, Cookie, Origin
x-content-type-options: nosniff
x-frame-options: DENY
但是当我做print(response.headers)
时,我只看到这个
'Server': 'nginx/1.14.0 (Ubuntu)', 'Date': 'Wed, 07 Jul 2021 18:23:45 GMT',
'Content-Type': 'text/html', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive',
'X-Frame-Options': 'DENY', 'Vary': 'Accept-Language, Origin',
'Content-Language': 'en', 'X-Content-Type-Options': 'nosniff', 'Content-Encoding': 'gzip'
而location
不见了
我看到几个回答都谈到了
'Access-Control-Expose-Headers': 'Location'
但我不知道它是否正确和/或如何正确使用它。
我也尝试过使用urllib
import urllib.request as urllib2
>>> f = urllib2.urlopen('https://petdogs.net/search/?input=abcdefgh')
>>> print(f.headers)
但这会回应
Server: nginx/1.14.0 (Ubuntu)
Date: Thu, 08 Jul 2021 11:12:58 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 128053
Connection: close
X-Frame-Options: DENY
Vary: Cookie, Accept-Language, Origin
Content-Language: en
X-Content-Type-Options: nosniff
Set-Cookie: csrftoken=xxxxxx; expires=Thu, 07 Jul 2022 11:12:57 GMT; Max-Age=31449600; Path=/; SameSite=Lax
Set-Cookie: sessionid=bbbbbb; expires=Thu, 22 Jul 2021 11:12:57 GMT; HttpOnly; Max-Age=1209600; Path=/; SameSite=Lax
如何获得location
的值?
【问题讨论】:
将请求设置为allow_redirects=False
会有帮助吗?如response = requests.post('https://petdogs.net/search/?input=abcdefgh', headers=HEADERS, allow_redirects=False)
@AlmogAtNailo 它没有任何区别。我仍然得到同样的回应。 'Server': 'nginx/1.14.0 (Ubuntu)', 'Date': 'Thu, 08 Jul 2021 11:51:23 GMT', 'Content-Type': 'text/html', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'X-Frame-Options': 'DENY', 'Vary': 'Accept-Language, Origin', 'Content-Language': 'en', 'X-Content-Type-Options': 'nosniff', 'Content-Encoding': 'gzip'
【参考方案1】:
使用 urllib,很简单。
from urllib import request, parse
def create_post():
payload = 'title':'Pyton Requests','body':'Requests are qwesome','userId':1
data = parse.urlencode(payload).encode()
response = request.Request('https://jsonplaceholder.typicode.com/posts', data)
resp = request.urlopen(response)
print(resp.headers['Location'])
def main():
# """ Main entry point of the app """
# print("hello world")
create_post()
if __name__ == "__main__":
""" This is executed when run from the command line """
main()
【讨论】:
from urllib import request, parse
response = request.Request('https://petdogs.net/search/?input=abcdefgh')
resp = request.urlopen(response)
print(resp.headers['location'])
当我这样做时,我得到的输出为None
你能看看上面的吗? @Vivek
@raj247,你能分享一下实际的网址吗,我找不到这个 petdos 网址。以上是关于从 python 请求中的 response.headers 获取位置的主要内容,如果未能解决你的问题,请参考以下文章