python-requests - How to get bond data from fundsupermart using post method?
问题描述
I am trying to get bond data from this page, "https://secure.fundsupermart.com/fsm/bonds/factsheet/XS1415758991/DEGREE-7-250-03Jun2021-Corp-USD".
I inspected the page, the data can be found in Network-XHR, name is XS1415758991. The name is the bond ISIN code. I then found the link behind the name. “<a href="https://secure.fundsupermart.com/fsm/rest/bond-info/get-bond-factsheet-data/XS1415758991" rel="nofollow noreferrer">https://secure.fundsupermart.com/fsm/rest/bond-info/get-bond-factsheet-data/XS1415758991”. In the Headers, I also found "x-xsrf-token: 38ccccd8-6cae-46a9-b916-2e80e607e107".
import requests
url_bond = r'https://secure.fundsupermart.com/fsm/rest/bond-info/get-bond-factsheet-data/XS1415758991'
headers = {'Accept-Encoding': 'gzip', }
data = {"x-xsrf-token":'38ccccd8-6cae-46a9-b916-2e80e607e107'}
r = requests.post(url_bond,headers=headers, data = data)
r.text
I tried following codes, but replied "'Expected CSRF token not found. Has your session expired?'". I expect to get the data in ","bondPriceJsonHashmap"--"THREE_YEAR"
解决方案
I solved the question myself. The solution is simple. 1) find the token in cookies 2) post to the url.
URL = 'https://secure.fundsupermart.com/fsm/rest/bond-info/get-bond-factsheet-data/JK8897874'
client = requests.session()
client.get(URL) # sets cookie
csrftoken = client.cookies['XSRF-TOKEN']
headers = {'x-xsrf-token':csrftoken}
r = client.post(URL, headers= headers)
r.content
推荐阅读
- android - Android-GridView 中带有复选框的项目选择
- ios - 使用 NSPersistentCloudKitContainer 同步旧数据
- javascript - 计算由 javascript 代码添加的多个输入字段的值,并在 Javascript 中为每一行显示
- python - 具有累积值的新 Pandas 列取决于上一行的条件
- reactjs - 隐藏下一个按钮,直到在 React 最终表单向导中填充了值
- python - 这是 pycuda 的 gpuarray.dot() 函数的正常行为吗?
- python-3.x - LightFM 中的损失函数
- r - 根据小标题中的特定值过滤小标题列表
- node.js - 如何将命令限制到调用命令的服务器?
- python - 使用 skimage 阅读器