akfamily / akshare

AKShare is an elegant and simple financial data interface library for Python, built for human beings! 开源财经数据接口库
https://akshare.akfamily.xyz
MIT License
9.33k stars 1.88k forks source link

bond_china_close_return()报错 #4299

Closed qiuning79 closed 11 months ago

qiuning79 commented 11 months ago

python:3.11.6 akshare:1.11.79

import akshare as ak ak.bond_china_close_return(symbol="国债", period="1", start_date="20231101", end_date="20231101")

raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 实际原因是网站返回403,禁止访问。 1.11.58之前版本出过同样现象的问题,1.11.60版本通过在网站注册的方式修复。但从昨日开始1.11.79最新版本也重新出现这一问题了,可能是网站改变了认证策略? 同样,通过手工访问chinamoney主页,再调用函数的方式可以临时规避这一问题。

LittleWorm1 commented 11 months ago

单步了一下,是网站注册流程有了修改,还需要在以下地址提交注册: https://www.chinamoney.com.cn/lss/rest/cm-s-account/getSessionUser 我在def __bond_register_service() -> requests.Session:函数的 session.post(url="https://www.chinamoney.com.cn/dqs/rest/cm-u-rbt/apply", data=data, headers=headers) 语句后增加了以下代码: cookies_dict = session.cookies.get_dict() cookies_str = '; '.join(f'{k}={v}' for k, v in cookies_dict.items()) headers = { "Accept": "application/json, text/javascript, /; q=0.01", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "en", "Connection": "keep-alive", "Content-Length": "0", 'Cookie': cookies_str, "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", "Host": "www.chinamoney.com.cn", "Origin": "https://www.chinamoney.com.cn", "Referer": "https://www.chinamoney.com.cn/chinese/bkcurvclosedyhis/?bondType=CYCC000&reference=1", "Sec-Fetch-Dest": "empty", "Sec-Fetch-Mode": "cors", "Sec-Fetch-Site": "same-origin", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36", "X-Requested-With": "XMLHttpRequest" } session.post(url="https://www.chinamoney.com.cn/lss/rest/cm-s-account/getSessionUser", headers=headers) 增加上述代码之后,调用就可以正常返回收益率曲线数据,不再报错了。 供参考

albertandking commented 11 months ago

python:3.11.6 akshare:1.11.79

import akshare as ak ak.bond_china_close_return(symbol="国债", period="1", start_date="20231101", end_date="20231101")

raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 实际原因是网站返回403,禁止访问。 1.11.58之前版本出过同样现象的问题,1.11.60版本通过在网站注册的方式修复。但从昨日开始1.11.79最新版本也重新出现这一问题了,可能是网站改变了认证策略? 同样,通过手工访问chinamoney主页,再调用函数的方式可以临时规避这一问题。

感谢反馈问题及提供解决方案,目前已经修复,升级到 AKShare 1.11.80 使用

albertandking commented 11 months ago

单步了一下,是网站注册流程有了修改,还需要在以下地址提交注册: https://www.chinamoney.com.cn/lss/rest/cm-s-account/getSessionUser 我在def __bond_register_service() -> requests.Session:函数的 session.post(url="https://www.chinamoney.com.cn/dqs/rest/cm-u-rbt/apply", data=data, headers=headers) 语句后增加了以下代码: cookies_dict = session.cookies.get_dict() cookies_str = '; '.join(f'{k}={v}' for k, v in cookiesdict.items()) headers = { "Accept": "application/json, text/javascript, /_; q=0.01", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "en", "Connection": "keep-alive", "Content-Length": "0", 'Cookie': cookies_str, "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", "Host": "www.chinamoney.com.cn", "Origin": "https://www.chinamoney.com.cn", "Referer": "https://www.chinamoney.com.cn/chinese/bkcurvclosedyhis/?bondType=CYCC000&reference=1", "Sec-Fetch-Dest": "empty", "Sec-Fetch-Mode": "cors", "Sec-Fetch-Site": "same-origin", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36", "X-Requested-With": "XMLHttpRequest" } session.post(url="https://www.chinamoney.com.cn/lss/rest/cm-s-account/getSessionUser", headers=headers) 增加上述代码之后,调用就可以正常返回收益率曲线数据,不再报错了。 供参考

感谢反馈问题及提供解决方案,目前已经修复,升级到 AKShare 1.11.80 使用