如何获得这个资源?

阅读 1.6k
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)