网站首页
请求的URL
数据乱码
代码
import requests
import pandas as pd
url = "http://www.xinfadi.com.cn/getPriceData.html"
# 表单数据
payload=(
'limit=1000&' # 每页条数
'current=1&' #当前页数
'pubDateStartTime=2025/01/01'
'&pubDateEndTime=2025/01/28&'
'prodPcatid=&' # 一级分类
'prodCatid=&' # 二级分类
'prodName='
)
# 请求头
headers = {
'X-Requested-With': 'XMLHttpRequest',
'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
# 参数为表单形式
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Accept': '*/*',
'Host': 'www.xinfadi.com.cn',
'Connection': 'keep-alive'
}
# 发起请求
response = requests.request("POST", url, headers=headers, data=payload)
if response.status_code == 200:
# 解决乱码
response.encoding = "utf-8"
# 将结果转为DataFrame表格数据
df = pd.json_normalize(response.json()["list"])
df.to_excel("F:\Code\数据集\\xinfadi.xlsx", index=False)
else:
print("请求失败:%d" % (response.status_code))