python - Python请求 - 响应编码不正确
问题描述
我正在发出登录请求,但是尽管登录成功,但响应已被编码。使用 response.content 时看起来像 base64:
\x13h(\xa6\xe4-!\xea\xa4\xa3m\x1dT\x8e}\x94\\\x16\xce5`\x90\xa5\xa3\x92a\x90\xc7\xb2\x8dG\xab3H\xfc2,\xd4x\xe4\xf1I\xf2\x89A\xad\xaeJ\xa4\xdeu\x84\xce\x14\x86,vB\xdd\xb4\x84\xf2\xdc{N.\x8d\xf0Rk@\xda\n\xe2\x88\xb5\xde\xb2\xed\x9b8sM6\x12l\xc8\x8b}\xae\xcd\x99\xb2\xd1\x08a\x0eF\xf1\x8e\xdcp\xf3\x10%U\xb5\xd15\xaf&\\F\xe0\x84iQ\x8ae2\xd6\xba\xb3\xc2\xc45\xb2L\x1e\x9ag\x835\xea\xd8\x9e3Y9\x17\x82=,[\xf0\x80rd7\xcb2\x08\xc1\x02\x06i\xb9\xb9\xab\n\xb0\x8a\'\xfc\xdcK\xf5\xf0\xff
但是,在解码时它是无效的。在 Firefox 中,它显示为 json 字符串: JSON Response in Firefox
并使用 response.text 它返回不可读的废话:
��}���p�~�Ns�=�9�q��"���SS%�+�<��M��4��}1�
我需要 python 响应看起来像我尝试查看响应标头进行编码的 firefox 响应,但什么都没有。
这是我的请求代码:
import cfscrape
import json
import requests
s = requests.Session()
scraper = cfscrape.create_scraper(sess=s) # returns a CloudflareScraper instance
# Or: scraper = cfscrape.CloudflareScraper() # CloudflareScraper inherits from requests.Session
headers = {"Host": "www.runebet.com",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; rv:66.0) Gecko/20100101 Firefox/66.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
"Upgrade-Insecure-Requests": "1",
"TE": "Trailers"}
data = '{"form":{"accept_tos":true,"aid":"493895","game_mode":"rs3","username":"gtgg3wfwfo","password":"gggggg1!!","password_confirmation":"gggggg1!!"},"fp":"","ref":""}'
r = scraper.post("https://www.runebet.com/api/auth/register", headers=headers, data=data)
print(r.content)
r = requests.utils.dict_from_cookiejar(scraper.cookies)
print(r["cf_clearance"])
headers["Cookie"] = "cf_clearance=" + r['cf_clearance'] + ""
headers["Content-Type"] = "application/json;charset=utf-8"
headers["X-Requested-With"] = "XMLHttpRequest"
headers["Referer"] = "https://www.runebet.com/login"
headers["Accept"] = "application/json, text/plain, */*"
#print(headers)
s = requests.Session()
data = '{"username":"gtgg3wfwfo","password":"gggggg1!!","recaptcha":"BAD","fp":"7ec5779306942b38559d73723988f5c7"}'
r = s.post("https://runebet.com/api/token-login", data=data, headers=headers)
r.encoding = 'br'
print(r.text)
data='{"amount":2500,"source":"poker"}'
r = s.post("https://www.runebet.com/api/poker/transfer", data=data, headers=headers)
r.encoding = 'br'
print(r.text)
#print(bytearray.fromhex(r.text).decode())
#print(r.text.decode('utf-8'))
对解码响应有任何帮助吗?
注意:设置r.encoding = "utf-8"
仍然返回上面显示的奇怪符号。
注意:设置r.encoding = None
也是一样的
Python中的响应标头:
{'Date': 'Sun, 31 Mar 2019 13:39:00 GMT', 'Content-Type': 'application/json', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Set-Cookie': '__cfduid=df9ec0142c171b8601e97a3045d4dc5ca1554039538; expires=Mon, 30-Mar-20 13:38:58 GMT; path=/; domain=.runebet.com; HttpOnly; Secure', 'Vary': 'Accept-Encoding', 'Cache-Control': 'no-cache, private', 'X-RateLimit-Limit': '10', 'X-RateLimit-Remaining': '8', 'X-Frame-Options': 'SAMEORIGIN', 'X-XSS-Protection': '1; mode=block', 'X-Content-Type-Options': 'nosniff', 'Strict-Transport-Security': 'max-age=15552000; includeSubDomains; preload', 'Expect-CT': 'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"', 'Server': 'cloudflare', 'CF-RAY': '4c02ba0d5807977a-FRA', 'Content-Encoding': 'br'}
解决方案
我是同样的问题。您可以从标题中删除 Accept-Encoding 。
在这种情况下,Accapt-Encoding 告诉服务器返回的数据应该被压缩。因此,您可以解压缩数据或仅获取数据而不进行压缩。 有关接受编码的更多信息
推荐阅读
- javascript - 在页面内注入书签后,$ 不是函数错误
- javascript - 从 JavaScript 中的另一个函数访问一个函数内部的 var
- python-3.x - 查找列表中的任何值是否存在于数据框中的任何位置
- python - 在熊猫数据框中处理不一致的数据
- python - Pandas Grouper 计算事件之间经过的时间
- swift - 问题过滤带有部分的 Swift UITableView - 未显示结果
- c - 请求重放模式中的 ZeroMQ 只允许来自特定 IP 的请求
- python - AttributeError:“datetime.timedelta”对象没有属性“strftime”
- python - PyCharm 中某些 Cerberus 库类的未解决参考
- prometheus - 如何在节点导出器中设置 --collector.textfile.directory