Python代码:
[Python] 纯文本查看 复制代码 import requests
import csv
headers = {
# "referer": "https://q.stock.sohu.com/cn/000002/lshq.shtml",
"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"
}
url = "https://q.stock.sohu.com/hisHq"
params = {
"code": "cn_000002", # 股票代码
"start": "20221201", # 开始日期
"end": "20230112", # 结束日期
"stat": "1",
"order": "D",
"period": "d",
"r": "0.6705010102947877",
}
response = requests.get(url, headers=headers, params=params)
response.encoding = 'gbk'
data_list = response.json()[0]['hq']
with open('cn_000002.csv', 'w', encoding='gbk', newline='') as f: # 创建表格文件
csv_writer = csv.writer(f)
head = ['日期', '开盘', '收盘', '涨跌额', '涨跌幅', '最低', '最高', '成交量(手)', '成交额(万)', '换手率'] # 表头
csv_writer.writerow(head)
for i in data_list:
csv_writer.writerow(i) # 写入每行数据
print('保存成功') |