【问题标题】:Pyton requests: Extracting data from payloadPython 请求:从有效负载中提取数据
【发布时间】:2021-12-31 14:47:39
【问题描述】:

这是我在这里的第一个问题。

目前我正在研究 Python 请求模块。我正在尝试自动化我需要传递 csrf 令牌的任务。 csrf 令牌可以在先前请求的负载中找到。

如何从 Payload 中提取价值?

示例负载:

值1:ABCD

值2:EFGH

csrf_token:我正在寻找的令牌

value3: 假

对不起,如果这是一个愚蠢或简单的问题,但我现在无法解决。

感谢您的帮助!

【问题讨论】:

  • 这能回答你的问题吗? Passing csrftoken with python Requests
  • @RoseGod 我不这么认为。在网页内容中,我可以找到 2 个不具有相同值的 csrf 令牌,我认为我需要从有效负载中选择一个。或者我可以只使用内容中的其中一个吗?
  • 什么是示例有效负载真的吗? 您只是在显示一些文本。是 JSON 格式吗?不要试图为我们解释;向我们展示真实的细节。你改变的越少越好。见How to Ask

标签: python python-requests extract csrf payload


【解决方案1】:

以下是您可能要查找的数据所在位置的一些示例。

import requests


response = requests.get('http://some_url')
# raise an exception if the status  code != 200
response.raise_for_status()

# the contents of the response. (bytes)
contents = response.contents

# the contents of the response if the contents are 
# formatted as JSON. (dictionary)
contents = response.json()

# the headers of the response. (dictionary)
headers = response.headers

# You also have to consider if you are using the correct HTTP protocol
payload = {}
response = requests.put('http://some_url', json=payload)
response = requests.post('http://some_url', json=payload)

# the responses are going to function just like when using the "get" protocol

【讨论】:

  • 首先,谢谢!与此同时,我环顾四周并从页面内容中提取了 csrf 令牌,但它不起作用。它给了我错误 b'{"error":"CSRF Token mismatch"}' 正如我上面所说,我认为正确的 csrf 令牌位于请求表单数据(有效负载)中。有没有办法从那里提取它?
  • 我需要确切了解您要获取的内容。您在“有效载荷”数据之前提到“表单”数据。这可能是一个帖子而不是一个获取。也许您正在寻找的数据封装在对帖子的响应中。
  • 对不起,我现在发现的代码有误。现在能够在 html 中找到令牌。感谢您回答我并尝试提供帮助:)
猜你喜欢
  • 2015-02-06
  • 2013-01-09
  • 1970-01-01
  • 2017-05-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-30
  • 1970-01-01
相关资源
最近更新 更多