如何获得这个资源?

阅读 1.5k
1 个回答

网页中是通过向 https://www.csindex.com.cn/csindex-home/exportExcel/security-industry-search-excel/CH 发送 POST 请求来获取 xlsx 文件的内容并下载。

所以可以这么写(需要安装 requests 库):

import requests
from urllib.parse import unquote

res = requests.post(
    "https://www.csindex.com.cn/csindex-home/exportExcel/security-industry-search-excel/CH",
    headers={"content-type": "application/json;charset=UTF-8"},
    json={
        "searchInput": "", # 输入的搜索内容
        "pageNum": 1, # 页码
        "pageSize": 10, # 每页条数
        "sortField": None, # 按哪个类别排序
        "sortOrder": None, # 排序方式
    },
)

print(res.status_code, res.url)
with open(
    unquote(res.headers["Content-disposition"].lstrip("attachment;filename=")), "wb"
) as xlsx:
    xlsx.write(res.content)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进