时间:2023-05-04 12:00:02 | 来源:网站运营
时间:2023-05-04 12:00:02 来源:网站运营
上市公司国企数量分析:前段时间看到有人在问中国上市公司中,国企数量有多少。当时感觉这个问题并不复杂,应该有现成的结论,结果百度、谷歌之后,发现并没有一个明确的答案。本篇文章就是带领大家了解下获取上市公司中国企数量的方法。# -*- coding: utf-8 -*-import requestsimport xlwtimport pandas as pdimport timeimport jsonheaders = { 'Accept': 'application/json, text/plain, */*', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Connection': 'keep-alive', 'Cookie': 'xq_a_token=48575b79f8efa6d34166cc7bdc5abb09fd83ce63; xqat=48575b79f8efa6d34166cc7bdc5abb09fd83ce63; xq_r_token=7dcc6339975b01fbc2c14240ce55a3a20bdb7873; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOi0xLCJpc3MiOiJ1YyIsImV4cCI6MTU4OTY4MjczMCwiY3RtIjoxNTg4NjI3ODM4MjM4LCJjaWQiOiJkOWQwbjRBWnVwIn0.T8Kn6v1mJhaShx0AnQ-J68Vsq2rKbk10SnvLoIineGTbEacSsFhRmF8H49wekqFHxzW_JOnjHG-l7cocC65CdLTn_ji3WLfIqdyiibGvs1Pvox0y_uMvduLod8aSwc-KVmoXq6B4k-nCc_1TcqtOIBCw7M9PQrbHXS2P_VROcbNWCHxgcAOF00n4eEPQrWDahmU9lQBlijnV4_hIJE8bf31YKDEhedyXwvSg4P5wjnIDmg0AL-xov0MvtQOFpOKD0dXSTy3xkBQaP1HWzRjznqNnZU8hXUltXUlpjc8Owi3XHjzGBJPiQMR9JfVcIkcLl4OlO3L-fO6zXSKuC6yFHQ; u=331588627844878; device_id=24700f9f1986800ab4fcc880530dd0ed; s=cv110w8aqt', 'Host': 'stock.xueqiu.com', 'Origin': '雪球 - 聪明的投资者都在这里', 'Referer': '雪球-聪明的投资者都在这里', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Site': 'same-site', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36' }#从baostock获取的all_stock.csv上市公司列表,通过pandas解析,获得公司总数量#我这里把csv文件另存成了exceldf = pd.read_excel('all_stock.xlsx')height,width = df.shape#初始化创建一个上市公司类别的excel文件,用于存储结果workbook = xlwt.Workbook(encoding = 'utf-8')worksheet = workbook.add_sheet('company_SHA')# 遍历深交所A股所有公司列表,使用公司股票代码爬取公司基本信息# 如果遇到连接问题,记录并重复get4次,如果仍然无法连接,则跳过抓取下一个公司#y = heightfor x in range(0,height): if("sh" in df.loc[x,"code"]): company_id = "SH"+df.loc[x,"code"][3:] url = "https://stock.xueqiu.com/v5/stock/f10/cn/company.json?symbol="+company_id elif("sz" in df.loc[x,"code"]): company_id = "SZ"+df.loc[x,"code"][3:] url = "https://stock.xueqiu.com/v5/stock/f10/cn/company.json?symbol="+company_id i = 1 while i<=4: try: res = requests.get(url, headers = headers) try: res_dict = json.loads(res.text) break except: print("Json 解析异常") except requests.exceptions.ConnectionError: print('ConnectionError -- please wait 3 seconds') time.sleep(3) except requests.exceptions.ChunkedEncodingError: print('ChunkedEncodingError -- please wait 3 seconds') time.sleep(3) except: print('未知错误 重新连接') time.sleep(3) i+=1 if(i == 4): print(company_id+":该公司无法获取") company_type = "无此公司" controler = "无实际控制人" else: if(res_dict['data']['company'] is None or res_dict['data']['company']['classi_name'] is None or res_dict['data']['company']['actual_controller'] is None): company_type = "公司无类型" controler = "无实际控制人" else: company_type = res_dict['data']['company']['classi_name'] controler = res_dict['data']['company']['actual_controller'] print("剩余:"+str(y)+"个: "+df.loc[x,"code"]+":"+df.loc[x,"code_name"]+":"+company_type+" "+controler) #将结果写入excel worksheet.write(x,0,df.loc[x,"code"]) worksheet.write(x,1,df.loc[x,"code_name"]) worksheet.write(x,2,company_type) worksheet.write(x,3,controler) workbook.save('xueqiu2.xls') time.sleep(1) y-=1
关键词:分析