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
| # -*- coding: UTF-8 -*- # !/usr/bin/python
# pip3 install requests # pip3 install xlrd # pip3 install pandas
import hashlib import json import os import sys import csv import time
import requests import xlrd
# 我的key appKey = '' # 我的密钥 secKey = '' encode = 'utf-8' # Http请求头设置 timespan = str(int(time.time())) token = appKey + timespan + secKey hl = hashlib.md5() hl.update(token.encode(encoding=encode)) token = hl.hexdigest().upper() file_path = 'data.csv'
# 数据获取 def get_data(company_name): url = 'http://api.qichacha.com/ECIPartner/GetList?pageIndex=1&pageSize=20&key=' + appKey + '&searchKey=' \ + company_name print('开始获取' + company_name + '的股东信息数据...') print("请求url为:" + url) headers = {'Token': token, 'Timespan': timespan} response = requests.get(url, headers=headers) result_json = json.dumps(str(response.content, encoding=encode)) result_json = result_json.encode(encode).decode("unicode-escape") data = json.loads(result_json[1:-1]) if data['Status'] != '200': print('数据获取失败!原因:' + data['Message']) print('程序执行结束。') sys.exit() print('数据获取成功!') print('源数据为:' + str(data)) return data
# 向csv文件内写数据 def write_data_2_csv(index, data, company_name): total = data['Paging']['TotalRecords'] items = data['Result'] print(company_name + "共获取到" + str(total) + '条股东信息数据') print('开始写入' + company_name + '的数据...') csv_file = open(file_path, 'a+', newline='') try: writer = csv.writer(csv_file) if index == 0: header = ('公司名称', '股东', '类型', '占比', '出资金额(万)', '出资时间') writer.writerow(header) for item in items: row = (company_name, item['StockName'], item['StockType'], item['StockPercent'], item['ShouldCapi'], item['ShoudDate']) writer.writerow(row) print('写入数据完毕。') finally: csv_file.close()
# 删除已存在的data.csv def pre_exec(): if os.path.exists(file_path): os.remove(file_path)
def main(): print('程序执行开始...') pre_exec() company = xlrd.open_workbook('companyName.xlsx').sheet_by_index(0) for i in range(company.nrows): company_name = company.row_values(i)[0] data = get_data(company_name) write_data_2_csv(i, data, company_name) print('程序执行结束。')
if __name__ == '__main__': main()
|