获取XXXX个股前十大股东数据-03
# !/usr/bin/env python # -*- coding:utf-8 -*- """ # File : XXXX.py # Time :2023/8/18 11:38 # Author :lrtao2010 # version :python 3.10.1 # Description:获取XXXX个股前十大股东 """ #导入模块 import requests #下载网页 import bs4 #beautifulsoup4 解析网页 import time import random #自定义请求头 my_headers = { 'Accept': 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive', 'Host': 'XXXX.XXXX.com', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36' } #生成股票代码列表 my_code_list = [] with open("./shuju/XXXX.txt",encoding='utf-8')as f: for i in f.readlines(): my_code_list.append(i.strip()) #print(my_code_list) #主程序 for my_list in my_code_list: print(my_list) page_obj = requests.get(f"http://XXXX.XXXX.com//?XXXX?XXXX={my_list}",headers=my_headers,timeout=(30,30)) #print(page_obj.text) bs4_obj = bs4.BeautifulSoup(page_obj.text,"lxml") shareholder_eles = bs4_obj.find_all("tr",attrs={"height":"25"}) shareholder_num = 1 shareholder_list = [] shareholder_set = () for eles in shareholder_eles: if shareholder_num <= 10: shareholder = eles.find("a", attrs={"class": "anews2"}) if shareholder != None: shareholder_list.append(shareholder.text[0:2]) shareholder_num += 1 shareholder_set = set(shareholder_list) set_list_diff = len(shareholder_list) - len(shareholder_set) if set_list_diff >= 3: with open("./shuju/shareholder-4.txt", 'a+',encoding='utf-8')as s_f: s_f.writelines(f"{my_list}" + "\n") elif set_list_diff >= 2: with open("./shuju/shareholder-3.txt", 'a+',encoding='utf-8')as s_f: s_f.writelines(f"{my_list}" + "\n") elif set_list_diff >= 1: with open("./shuju/shareholder-2.txt", 'a+',encoding='utf-8')as s_f: s_f.writelines(f"{my_list}" + "\n") else : with open("./shuju/shareholder.txt", 'a+',encoding='utf-8')as s_f: s_f.writelines(f"{my_list}" + "\n") time.sleep(random.randint(1,2))