【发布时间】:2021-08-23 17:30:56
【问题描述】:
您好,我想将我的 python 请求 cookie 保存为 json 格式的文本文件,如下所示:
{"Proxy": "Cookies"}
目前我正在使用:
with open('data.txt') as f:
proxy_data = json.load(f)
r = requests.get(URL,proxies=proxies, cookies=proxy_data['127:000'])
proxy_data['127:000'] = dict(session.cookies)
with open('data.txt', 'w') as json_file:
json.dump(proxy_data, json_file)
我的data.txt文件写完后是这样的:
{"127:000": "{\"__cf_bm\": \"628f4c77a5139148cf28961a73f4edffed52f8c1-1622925414-1800-AaLguAyS9Hfl8TXCaQYUjjZ5i3sDck15kn/vcPJCWZBxi+nM9xU0zd/GfyKJu6/3R+GQEFdk/DGHEV4mANvpG1UMvcsXA9G9l/AFNEnIknTv\", \"cf_clearance\": \"84bffd070fbafcb91edcfa82f8cc3ba5a60a618d-1622925414-0-150\", \"cf_chl_seq_b116a013f9e2c44\": \"d3ac4406fc56d30\"}"}
但是我在这里遇到了一些错误。我该怎么做?
更新
我得到的错误
Python\Python37\lib\site-packages\requests\cookies.py",第 524 行,在 cookiejar_from_dict cookiejar.set_cookie(create_cookie(name, cookie_dict[name])) TypeError: string indices must be integers
【问题讨论】:
-
您遇到什么错误?您可以编辑您的答案以包含错误吗?
-
@user5305519 我上传了它们
-
@PhpCoder223,你能添加分配给
session的代码吗?是 requests.session 的实例吗? -
@SujanAdiga 是的
-
您的 data.txt 文件的值“127:000”已转义,这就是 requests.get 失败的原因。从您提供的代码和示例中,尚不清楚为什么要转义该值。
标签: python json python-3.x cookies python-requests