1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
Codelist_path = r"" Output_path = r""
Stocklist_path = r""
Category_All = "category_ndbg_szsh;category_sjdbg_szsh;category_dshgg_szsh;category_rcjy_szsh;category_sf_szsh;category_pg_szsh;category_kzzq_szsh;category_bcgz_szsh;category_tbclts_szsh;category_tszlq_szsh;category_cqdq_szsh;category_qtrz_szsh;category_jj_szsh;category_zf_szsh;category_gszl_szsh;category_jshgg_szsh;category_yjygjxz_szsh;category_bndbg_szsh;category_yjdbg_szsh;category_qyfpxzcs_szsh;category_gddh_szsh;category_zj_szsh;category_gqjl_szsh;category_gszq_szsh;category_gqbd_szsh;category_fxts_szsh;"
Category_A = "category_rcjy_szsh"
SeDate_A = '2019-01-01~2023-01-01'
Search_key = '会计师事务所'
import requests
from urllib.request import urlretrieve
import re
import math
import json
import xlwt
import xlrd
import os
import pandas as pd
url = "http://www.cninfo.com.cn/new/data/szse_stock.json" ret = requests.get(url=url) ret = ret.content stock_list = json.loads(ret)["stockList"]
len(stock_list)
print(stock_list[:2])
def export_excel(export): pf = pd.DataFrame(export) order = ['orgId', 'category', 'code', 'pinyin', 'zwjc'] pf = pf[order] columns_map = { 'orgId': 'orgId(原始ID)', 'category': 'category(股市类型)', 'code': 'code(代码)', 'pinyin': 'pinyin(拼音)', 'zwjc': 'zwjc()' } pf.rename(columns=columns_map, inplace=True) file_path = pd.ExcelWriter(Stocklist_path) pf.fillna(' ', inplace=True) pf.to_excel(file_path, encoding='utf-8', index=False) file_path.save()
if __name__ == '__main__': export_excel(stock_list)
code_dic = {(it['code']): it['orgId'] for it in stock_list}
print("\n正在进行服务器数据库检索,共找到 {} 家上市公司!\n\n --------------------------------服务器数据比对工作完成,即将开始载入数据进行匹配!--------------------------------\n".format(len(code_dic)))
print("\n --------------------------数据匹配完成,共生成 {} 家上市公司的“code-orgId”匹配数据!--------------------------\n".format(len(code_dic)))
class excel_read: def __init__(self, excel_path=Codelist_path, encoding='utf-8', index=0): self.data = xlrd.open_workbook(excel_path)
self.table = self.data.sheets()[index]
self.rows = self.table.nrows
def get_data(self): result = [] for i in range(self.rows): col = self.table.row_values(i) result.append(col) print("待爬取企业已从xls文件中加载完毕,结果如下:\n {}\n".format(result)) return result
code_list = [] code_list.extend(excel_read().get_data())
def get_and_download_pdf_flie(pageNum, stock, searchkey='', category='', seDate=''): url = 'http://www.cninfo.com.cn/new/hisAnnouncement/query' pageNum = int(pageNum)
data = {'pageNum': pageNum, 'pageSize': 30, 'column': '', 'tabName': 'fulltext', 'plate': '', 'stock': stock, 'searchkey': searchkey, 'secid': '', 'category': category, 'trade': '', 'seDate': seDate, 'sortName': '', 'sortType': '', 'isHLtitle': 'true'}
headers = {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Connection': 'keep-alive', 'Content-Length': '181', '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/commonUrl/pageOfSearch?url=disclosure/list/search', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36', 'X-Requested-With': 'XMLHttpRequest'}
r = requests.post(url, data=data, headers=headers)
result = r.json()['announcements']
for i in result: if re.search('摘要', i['announcementTitle']): pass else: title = i['announcementTitle']
secName = i['secName']
secCode = i['secCode']
adjunctUrl = i['adjunctUrl'] down_url = 'http://static.cninfo.com.cn/'+adjunctUrl
filename = f'{secCode}{secName}{title}.pdf'
filename = re.sub(r'[(*)(<)(#)(\\)(|)(\")(?)(:)(/)(<em#)(</em#)]', '', filename)
filepath = saving_path+'\\'+filename
r = requests.get(down_url)
with open(filepath, 'wb') as f: f.write(r.content)
print(f'{filename}下载完毕')
saving_path = Output_path
Num = len(code_list)
print("待爬取企业总数量为:{}\n\n ------------------------------------已加载待爬取企业匹配信息,即将开始爬取数据!------------------------------------\n".format(Num))
for i in range(0, Num): code = code_list[i][0] orgId = code_dic[code]
stock = '{},{}'.format(code, orgId) print("\n即将爬取 {} :".format(stock))
searchkey_A = Search_key
category = Category_A
seDate = SeDate_A
for pageNum in range(1, 3): try: get_and_download_pdf_flie( pageNum, stock, searchkey_A, category, seDate,) except: pass
print("-------------------------------------------------程序执行完成!-------------------------------------------------")
|