声明:本程序不存在任何恶意攻击意图,仅供学习练习使用
感谢巨潮资讯提供的信息:http://www.cninfo.com.cn/
教程开始
1. 环境
使用 pip安装 request 1.0.117
模块 os、json、time模块均为python自带环境 Python 3.8.12
代码
2. List item
import os
import requests
import json
import time
print("年报爬取,股票代码,开始时间、结束时间\n退出程序在输入股票代码处输入exit退出\n一次最多100条")
url = "http://www.cninfo.com.cn/new/hisAnnouncement/query"
headers = {
"Accept": "application/json, text/javascript, */*; q=0.01",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Host": "www.cninfo.com.cn",
"Origin": "http://www.cninfo.com.cn",
"Referer": "http://www.cninfo.com.cn/new/disclosure/stock?stockCode=000501&orgId=gssz0000501",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36 Edg/101.0.1210.39",
"X-Requested-With": "XMLHttpRequest"
}
while True:
number = str(input("请输入代号:"))
if number.__eq__("exit"):
print("退出程序")
break
begin = str(input("请输入开始年份:")) + "-01-01~"
end = str(input("请输入结束年份:")) + "-12-31"
data = {
"stock": number + ",gssz0" + number,
"tabName": "fulltext",
"pageSize": 100,
"pageNum": 1,
"column": "szse",
"category": "category_ndbg_szsh;",
"plate": "sz",
"seDate": begin + end,
"searchkey": "",
"secid": "",
"sortName": "",
"sortType": "",
"isHLtitle": "true"
}
print(data)
res = requests.post(url, data)
content = res.content
djson = json.loads(content)
if not os.path.exists("./pdf/" + number):
os.mkdir("./pdf/" + number)
if djson.get("announcements") is None:
print("你查找的股票在该范围内无公告,请检查股票编号和时间范围")
else:
for j in djson.get("announcements"):
url = str(j.get("adjunctUrl"))
name = j.get("announcementTitle")
url = "http://www.cninfo.com.cn/new/announcement/download?" \
+ "bulletinId=" + url[21:-4] + "&announceTime=" + url[10:20]
print(name + ":\t" + url)
response = requests.get(url)
content = response.content
file = open("./pdf/" + number + "/" + name + ".pdf", "wb+")
file.write(content)
file.close()
# time.sleep(1) # 防止被屏蔽
print(number + "任务完成")
结果图