Python爬取10jqka.com.cn业绩公告数据并去重写入CSV文件
以下是Python代码实现:
import requests
import csv
url = 'http://data.10jqka.com.cn/ajax/yjgg/date/2022-12-31/board/ALL/field/DECLAREDATE/order/desc/page/{}/ajax/1/free/1/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299'
}
data_set = set()
for i in range(1, 101):
response = requests.get(url.format(i), headers=headers)
if response.status_code == 200:
data = response.json()
for item in data['data']:
if item['code'] not in data_set:
data_set.add(item['code'])
with open('yjgg.csv', 'a+', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
writer.writerow([item['code'], item['name'], item['declaredate'], item['eps'], item['profit'], item['report_date']])
print('第{}页写入完成'.format(i))
else:
print('第{}页请求失败'.format(i))
以上代码通过循环访问每一页数据,将每一页中不重复的数据写入到yjgg.csv文件中。其中,使用了set来避免重复数据的写入。每写入完一次一页的数据后,输出一条提示信息,方便查看进度。
原文地址: https://www.cveoy.top/t/topic/mxXX 著作权归作者所有。请勿转载和采集!