IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> python通过selenium绕过反扒系统跨网页批量获取股票财务信息 -> 正文阅读

[开发测试]python通过selenium绕过反扒系统跨网页批量获取股票财务信息

完整代码:

import xlwings as xw
import requests
from bs4 import BeautifulSoup
from datetime import datetime
import json
import xlwt
import xlwings as xw
from selenium import webdriver
import time
import pandas as pd
import csv
import re
from selenium.webdriver import Chrome, ChromeOptions


# item_list=[]
df = pd.DataFrame()
def data_a(html):  # 获取基础信息1
    # with open('rrBand.html', 'r', encoding='utf-8') as f:
    # html = BeautifulSoup(f, 'lxml')
    # html.list = html.find_all('div', attrs={'class': 'container-sm float-left stock__main'})
    # print(html.list)
    df = pd.DataFrame()
    # print(html)
    for i, item in enumerate(html):
        # print(item)
        # print(html.list_a)
        try:
            bandNanme = item.find_all('div', attrs={'class', 'stock-name'})[0].text.strip('()')[0:].strip(')')
            df['序号'] = '',
            # df['股票'] = item.find_all('div', attrs={'class', 'stock-name'})[0].text.strip('()')[0:bandNanme.find('(')].strip(
            #             ')'),
            # df['代码'] = item.find_all('div', attrs={'class', 'stock-name'})[0].text.strip('')[5:].strip(')').replace(':',''),
                    # print(df[['股票','代码']])
            df['股价'] = item.find_all('div', attrs={'class', 'stock-current'})[0].text.strip('¥'),
            # print(df['股价'])
            html.list_a = item.find_all('table', attrs={'class', 'quote-info'})
            for i,item_a in enumerate(html.list_a):
                # print(item_a.find_all('span'))
                # for i in range(60):
                #     print(item_a.find_all('span')[i].text,i,sep=',')
                df['总市值(亿)'] = item_a.find_all('span')[19].text.strip('亿'),
                df['总股本(亿)'] = '',
                df['营市比'] = '',
                # print(item_a.find_all('span')[i].text,i,sep=',')#_a.find_all('span')[18].text.strip('亿'),i,sep=','),
                df['PE市盈率'] = item_a.find_all('span')[10].text.strip(''),
                df['PB市净率'] = item_a.find_all('span')[10].text.strip(''),#str((float(df['股价']) / float(item_a.find_all('span')[20].text.strip(''))))[0:4],
                # print(df['PB市净率'])
                print(str(i),"第一模块写入正常")
        except:
            print(str(i), "第一模块写入异常")
            #     # continue
    return df
# df.to_csv('fundWebd.csv', index=None, encoding='utf-8-sig',sep=',')#mode='a', header=None,index=None,
# print(df[['股价','总市值','EPS每股收益']])

def data_b(html):  # 获取基础信息2
# url='https://xueqiu.com/snowman/S/SH600282/detail#/ZYCWZB'
# print('12')
#     with open('Band.html', 'r', encoding='utf-8') as f:
#         html = BeautifulSoup(f, 'lxml')
#         html.list_b = html.find_all('tbody')
        df = pd.DataFrame()
        bandIncome=[]
        for i, item in enumerate(html):
            # print(item)
            html.list_b_a=item.find_all('tr')
            for i,item in enumerate(html.list_b_a):
                html.list_b_a_a = item.find_all('td')
                # print(item.find_all('td'))
                for i, item in enumerate(html.list_b_a_a):
                    try:
                        html.list = item.find_all('p')[0].contents[0]
                        bandIncome.append(html.list)
                        # print(bandIncome,i,sep=',')
                        # for i, item in enumerate(html.list):
                        # print(item)
                        html.list_b = item.find_all('table', attrs={'class', 'quote-info'})
                        # print(html.list_a
                        # bandNanme = item.find_all('div', attrs={'class', 'stock-name'})[0].text.strip('()')[0:].strip(')')
                        # df['17年利润(亿)'] = '',
                        # df['20年利润(亿)'] = '',
                        print(str(i), "第二模块写入正常")
                    except:
                        # continue
                        print(str(i), "第二模块写入异常")
        df['营业额'] =bandIncome[0].strip('亿'),
        df['EPS每股收益'] =bandIncome[30].strip(''),#item_a.find_all('span')[16].text.strip(''),
        df['负债率'] =bandIncome[85].strip(''),
        df['经营现金流'] =bandIncome[50].strip(''),
        df['17年利润(亿)'] =bandIncome[13].strip('亿'),
        df['20年利润(亿)'] = bandIncome[10].strip('亿'),
        df['未分配利润'] = bandIncome[45].strip(''),
        df['公积金'] = bandIncome[40].strip(''),
        df['毛利率'] = bandIncome[75].strip(''),
        df['净利率'] = bandIncome[80].strip(''),
        df['ROA总报酬率'] = bandIncome[65].strip(''),
        df['ROE净收益率'] = bandIncome[60].strip(''),
        df['账款周期'] = bandIncome[120].strip(''),
        df['存货周转'] = bandIncome[150].strip(''),
        df['总资产周转率'] = bandIncome[145].strip(''),
        print(df)
        for i in range(len(bandIncome)):
            print(bandIncome[i],i,sep=',')
        return df
def data_c(html,html1):
    # with open('Band.html','r',encoding='utf-8') as f:
    #     # url='https://xueqiu.com/snowman/S/SH601991/detail#/FHPS'
    #     html=BeautifulSoup(f,'lxml')
    #     html.list=html.find_all('tbody')
    df = pd.DataFrame()
    for i,item in enumerate(html1):
        # print(item.text)
        bandNanme=item.text
        # print(item.text.strip('')[bandNanme.find('('):].strip(')'))
        df['股票'] = item.text.strip('')[0:bandNanme.find('(')].strip(')'),
        df['代码'] = item.text.strip('')[bandNanme.find('(')+1:].strip(')'),
        # print(df)
        # print(df['股票'].valuse, df['代码'].values)
    # df['股票'] = item.find_all('div', attrs={'class', 'stock-name'})[0].text.strip('()')[0:bandNanme.find('(')].strip(
    #             ')'),
    # df['代码'] = item.find_all('div', attrs={'class', 'stock-name'})[0].text.strip('')[5:].strip(')').replace(':',''),
    # print(df[['股票','代码']])
    for i,item in enumerate(html):
        try:
            cut_a=item.find_all('td')[1].text.strip().find('派')
            cut_b = item.find_all('td')[1].text.strip().find('元')
            # print(cut_a,cut_b)
            # print(item)
            # print(item.find_all('td')[1].text.strip()[cut_a+1:cut_b])
            df['分红率'] = '',
            df['分红']=item.find_all('td')[1].text.strip()[cut_a+1:cut_b],
            print(df)
            print(str(i),"第三模块写入正常")
        except:
            print(str(i), "第三模块写入异常")
    return df
def data_d(html):
# with open('Band.html','r',encoding='utf-8') as f:
#     # url='https://xueqiu.com/snowman/S/SH600282/detail#/ZCFZB'
#     html=BeautifulSoup(f,'lxml')
#     html.list=html.find_all('tbody')
    df = pd.DataFrame()
    # print(html)
    for i, item in enumerate(html):
        # try:
        cut_a = item.find_all('td')[7].text.strip('').find('亿')
        # print(cut_a)
        # print(item.find_all('td')[7].text.strip('')[0: cut_a-1])
        # print(item.find_all('td')[61].text.strip('')[0: cut_a-1])
        df['货币资金']=item.find_all('td')[7].text.strip('')[0: cut_a],
        df['存货']=item.find_all('td')[61].text.strip('')[0: cut_a],
        # for i in range(300):
        #     print(item.find_all('p')[i],i,sep=',')
        print(df)
        print(str(i), "第四模块写入正常")
        # except:
        #     print(str(i), "第四模块写入正常")
    return df
def data_e(html):
# with open('Band.html','r',encoding='utf-8') as f:
#             # url='https://xueqiu.com/snowman/S/SH601600/detail#/GSLRB'
#             html=BeautifulSoup(f,'lxml')
#             html.list=html.find_all('tbody')
        df = pd.DataFrame()
        # print(html)
        for i, item in enumerate(html):
            try:
                cut_a = item.find_all('td')[43].text.strip('').find('+')
                cut_b = item.find_all('td')[55].text.strip('').find('+')
                cut_c = item.find_all('td')[61].text.strip('').find('+')
                df['研发费用']=item.find_all('td')[43].text.strip(''),#[0:cut_a],
                df['利息费用'] =item.find_all('td')[55].text.strip(''),#[0:cut_b],
                df['利息收入'] = item.find_all('td')[61].text.strip(''),#[0:cut_c],
                # print(item.find_all('td')[43].text.strip('')[0:cut_a-1])
                # print(df['研发费用'].values, df['利息费用'].values, df['利息收入'].values ,sep=',')
                print(df)

                # for i in range(100):
                #     print(item.find_all('td')[i].text,i,sep=',')
                print(str(i), "第五模块写入正常")
            except:
                print(str(i), "第五模块写入异常")
        return df

# 写入csv中
if __name__ == "__main__":
    # 创建一个workbook

    app = xw.App(visible=False, add_book=False)
    wb = app.books.open('fundWebd.xlsx')
    # 创建一个worksheet
    sh = wb.sheets['worksheet']
    rng = [i for i in sh.range("c:c").value if i != None]#单元格内容
    j = sh.range('a1').expand('table').rows.count#序号
    app.display_alerts = False
    app.screen_updating = False
    # rng = sh.range('a1').expand('table')
    # nrows = rng.rows.count
    # a = sh.range(f'a1:a{nrows}').value
    # a = [ i for i in sht.range(a:a).value if i != None]

    # 打开网页
    opt = ChromeOptions()            # 创建Chrome参数对象
    opt.headless =False #True#              # 把Chrome设置成可视化无界面模式,
    driver = Chrome(options=opt)
    # driver = webdriver.Chrome()
    df_a=[]
    df_b=[]
    df_c = []
    df_d = []
    df_e = []
    for i in range(len(rng)-1):
        print(str(i),rng[i],'第'+str(i+1)+'只股票开始写入')#rng[i+1]
        try:
            bandcode=rng[i+1]#'SH601600'
            xueqiu_url='https://xueqiu.com/S/'+bandcode#雪球网基础数据'https://c.runoob.com/'#很好的ide工具
            xueqiu_url_a='https://xueqiu.com/snowman/S/'+bandcode+'/detail#/ZYCWZB'#主要指标
            xueqiu_url_c= 'https://xueqiu.com/snowman/S/'+bandcode+'/detail#/FHPS'#分红
            xueqiu_url_d = 'https://xueqiu.com/snowman/S/'+bandcode+'/detail#/ZCFZB'  # 存货
            xueqiu_url_e ='https://xueqiu.com/snowman/S/'+bandcode+'/detail#/GSLRB'#研发、利息收入

            # DFCF_url='http://emweb.eastmoney.com/PC_HSF10/OperationsRequired/Index?type=web&code=SH601600'

            #基础数据1加载
            driver.get(xueqiu_url)#加载网址
            time.sleep(1)  # 休眠1秒
            source =driver.page_source#获取网页内容
            html=BeautifulSoup(source,'html.parser')#获取网页内容
            time.sleep(1)  # 休眠1秒
            html.list = html.find_all('div', attrs={'class': 'container-sm float-left stock__main'})
            df_a = data_a(html.list)  # 执行语句块
            time.sleep(1)  # 休眠1秒

            #基础数据2加载主要指教
            driver.back()  # 加载网址
            time.sleep(2)
            driver.get(xueqiu_url_a)  # 加载网址
            # driver.find_elements_by_class_name('btn active').click()
            # driver.find_element_by_xpath(".//*[@id='header']/div[1]/div/form/input[2]").click()
            time.sleep(4)
            driver.find_element_by_xpath(".//div[contains(@class,'stock-info-btn-list')]/span[2]").click()
            # print(driver.find_element_by_xpath(".//div[contains(@class,'stock-info-btn-list')]/span[2]").text)  # /span[contains(@class,'btn')]
            time.sleep(3)  # 休眠4秒
            source = driver.page_source  # 获取网页内容
            html = BeautifulSoup(source, 'html.parser')  # 获取网页内容
            # print(html)
            time.sleep(2)  # 休眠1秒
            html.list_b = html.find_all('tbody')
            df_b = data_b(html.list_b)  # 执行语句块
            time.sleep(2)

            #基础数据三加载分红
            driver.back()  # 加载网址
            # time.sleep(1)
            driver.get(xueqiu_url_c)  # 加载网址
            time.sleep(3)
            source = driver.page_source  # 获取网页内容
            html = BeautifulSoup(source, 'html.parser')  # 获取网页内容
            # print(html)
            time.sleep(1)  # 休眠1秒
            html.list_c = html.find_all('tbody')
            html.list_c2 = html.find_all('div',attrs={'stock-info-name'})
            # print(html.list_c2)
            df_c= data_c(html.list_c,html.list_c2)  # 执行语句块
            # print(html)
            time.sleep(1)

            # 基础数据四加载资产负债表
            driver.get(xueqiu_url)  # 加载网址
            time.sleep(2)
            driver.get(xueqiu_url_d)  # 加载网址
            time.sleep(4)
            driver.find_element_by_xpath(".//div[contains(@class,'stock-info-btn-list')]/span[2]").click()
            time.sleep(4)
            source = driver.page_source  # 获取网页内容
            html = BeautifulSoup(source, 'html.parser')  # 获取网页内容
            # print(html)
            time.sleep(1)  # 休眠1秒
            html.list_d=html.find_all('tbody')
            df_d= data_d(html.list_d)  # 执行语句块
            df_d.to_json('fundWebdTest.json', orient='records', force_ascii=False)  # ,orient="values")
            time.sleep(2)

            # 基础数据五加载利润表
            driver.back()
            time.sleep(2)
            driver.get(xueqiu_url_e )#xueqiu_url_e
            time.sleep(4)
            driver.find_element_by_xpath(".//div[contains(@class,'stock-info-btn-list')]/span[2]").click()
            time.sleep(2)
            source=driver.page_source# 获取网页内容
            html=BeautifulSoup(source,'html.parser')# 获取网页内容
            # print(html)
            time.sleep(1)  # 休眠1秒
            html.list_e = html.find_all('tbody')
            df_e = data_e(html.list_e)  # 执行语句块
            # df_e.to_json('fundWebdTest.json', orient='records', force_ascii=False)  # ,orient="values")
            time.sleep(3)
            # with open('rrBand.html','w',encoding='utf-8') as f:#写入网页
            #     f.write(source)

        except:
            # continue
            print(str(i),"当页数据操作失败")

        # with open('rrBand.html', 'r', encoding='utf-8') as f:
        #     html = BeautifulSoup(f, 'lxml')
        #     html.list = html.find_all('div', attrs={'class': 'container-sm float-left stock__main'})
        #     df_a = data_a(html.list)  # 执行语句块
        # with open('Band.html', 'r', encoding='utf-8') as f:
        #     html = BeautifulSoup(f, 'lxml')
        #     html.list_b = html.find_all('tbody')
        #     df_b = data_b(html.list_b)  # 执行语句块
        # with open('Band.html', 'r', encoding='utf-8') as f:
        #      html = BeautifulSoup(f, 'lxml')
        #      html.list_b = html.find_all('tbody')
        # with open('Band.html','r',encoding='utf-8') as f:
        #     # url='https://xueqiu.com/snowman/S/SH600282/detail#/ZCFZB'
        #     html=BeautifulSoup(f,'lxml')
        #     html.list=html.find_all('tbody')
        # with open('Band.html','r',encoding='utf-8') as f:
        # # url='https://xueqiu.com/snowman/S/SH601600/detail#/GSLRB'
        #html=BeautifulSoup(f,'lxml')
        #html.list=html.find_all('tbody')

        #以下为写入模板
        # df=pd.concat([df_a,df_b],axis=1)#列合并,axis=0表示按行合并df = df_a.append(df_b)
        # print(df_c,'测试')
        df1=pd.concat([df_a,df_b],axis=1)#按列合并
        # print(df1)
        df2=pd.concat([df1,df_c],axis=1)#按列合并
        # print(df2)
        df3 = pd.concat([df2, df_d], axis=1)  # 按列合并
        # print(df3)
        df4 = pd.concat([df3, df_e], axis=1)  # 按列合并
        print(df4)
        df=pd.concat([df4,df],axis=0)#按行合并,这里很重要
        print(df)
        df.to_csv("fundWebd.csv", mode="a+", header=None, index=None, encoding="utf-8-sig",sep=',')#提前写入vsv文件
        # item_list.append(df)
        # print(item_list)

        df.to_json('fundWebd.json',orient ='records', force_ascii=False)#,orient="values")
        # with open('fundWebd.json','r',encoding='utf-8') as f:
        #     data = json.load(f)
        # item_list.append(data)
        # with open('.fund.json', 'w', encoding='utf-8')as f:
        #     json.dump(item_list,f, indent=1, ensure_ascii=False)
        with open('fundWebd.json', 'r', encoding='utf-8') as f:
            data = json.load(f)
            # print(data[0]['股票'])
        bandN = ['序号', '股票', '代码', '股价', '总市值(亿)', '总股本(亿)', '营业额', 'EPS每股收益', '分红', '分红率', '营市比', 'PE市盈率',\
                      'PB市净率', '负债率','经营现金流','货币资金','存货','利息费/收','17年利润(亿)','20年利润(亿)','利润复增率','营业额复合增长率',\
                      '季度增长率','现金收入比','PEG','未分配利润','公积金','毛利率','净利率','ROA总报酬率','ROE净收益率','账款周期','存货周转','总资产周转率']
        for i in range(len(data)):#写入数据
            try:
                print(len(data))
                sh.cells[i+1,0].value=i+1
                sh.cells[i+1, 1].value=data[i][bandN[1]]
                sh.cells[i+1, 2].value=data[i][bandN[2]]
                sh.cells[i+1, 3].value = data[i][bandN[3]]
                sh.cells[i+1, 4].value = data[i][bandN[4]]
                sh.cells[i+1, 5].value = data[i][bandN[5]]
                sh.cells[i+1, 6].value = data[i][bandN[6]]#营业额
                sh.cells[i+1, 7].value = data[i][bandN[7]]
                sh.cells[i+1, 8].value = data[i][bandN[8]]#分红
                # sh.cells[i+1, 9].value = data[i][bandN[9]]#分红率
                # sh.cells[i+1, 10].value = data[i][bandN[10]]#营市比
                sh.cells[i+1, 11].value = data[i][bandN[11]]
                sh.cells[i+1, 12].value = data[i][bandN[12]]
                sh.cells[i+1, 13].value = data[i][bandN[13]]#负债率
                sh.cells[i + 1, 14].value = data[i][bandN[14]]  # 经营现金流
                sh.cells[i + 1, 15].value = data[i][bandN[15]]  # 货币资金
                sh.cells[i + 1, 16].value = data[i][bandN[16]]  # 存货
                # sh.cells[i + 1, 17].value = round(float(data[i]['利息费用'])/float(data[i]['利息收入']),2)  # 利息费/收
                sh.cells[i + 1, 17].value = data[i]['利息费用']+' /'+ data[i]['利息收入']  # 利息费/收
                # sh.cells[i + 20, 17].value =data[i]['利息费用']
                # sh.cells[i + 20, 18].value =data[i]['利息收入']

                sh.cells[i + 1, 18].value = data[i][bandN[18]]  # 17年利润(亿)
                sh.cells[i + 1, 19].value = data[i][bandN[19]]  # 20年利润(亿)
                # sh.cells[i + 1, 20].value = data[i][bandN[20]]  # 利润复增率
                # sh.cells[i + 1, 21].value = data[i][bandN[21]]  # 营业额复合增长率
                # sh.cells[i + 1, 22].value = data[i][bandN[22]]  # 季度增长率
                # sh.cells[i + 1, 23].value = data[i][bandN[23]]  # 现金收入比
                # sh.cells[i + 1, 24].value = data[i][bandN[24]]  # PEG
                sh.cells[i + 1, 25].value = data[i][bandN[25]]  # 未分配利润
                sh.cells[i + 1, 26].value = data[i][bandN[26]]  # 公积金
                sh.cells[i + 1, 27].value = data[i][bandN[27]]  # 毛利率
                sh.cells[i + 1, 28].value = data[i][bandN[28]]  # 净利率
                sh.cells[i + 1, 29].value = data[i][bandN[29]]  # ROA总报酬率
                sh.cells[i + 1, 30].value = data[i][bandN[30]]  # ROE净收益率
                sh.cells[i + 1, 31].value = data[i][bandN[31]]  # 账款周期
                sh.cells[i + 1, 32].value = data[i][bandN[32]]  # 存货周转
                sh.cells[i + 1, 33].value = data[i][bandN[33]]  # 总资产周转率
                # sh.cells[i + 1, 32].value = data[i][bandN[32]]  # 存货周转
                # sh.cells[i + 1, 32].value = data[i][bandN[32]]  # 存货周转
                # sh.cells[i + 1, 32].value = data[i][bandN[32]]  # 存货周转
                # sh.cells[i + 1, 43].value =round(float(data[i]['研发费用'])/float(data[i][bandN[6]]),4)  # 研发/收入比
                sh.cells[i + 1, 34].value = data[i]['研发费用']+'/'+ data[i][bandN[6]]# 研发/收入比
                # sh.cells[i + 1, 45].value = float(data[i][bandN[6]])  # 研发/收入比
                    # print(i)
            except:
                # continue
                print(str(i),'excel写入错误')

    try:
        wb.save('fundWebd.xlsx')
        app.quit()
        # 获得当前窗口句柄
        sreach_windows = driver.current_window_handle
        driver.quit()
        # 获得当前所有打开的窗口的句柄
        all_handles = driver.window_handles
        for handle in all_handles:
            driver.switch_to.window(handle)
            driver.close()
            time.sleep(2)
        # driver.close()
        # driver.quit()
    except:
        print('有错误代码')








完整代码:重新调整股票代码和股票名称

import xlwings as xw
import requests
from bs4 import BeautifulSoup
from datetime import datetime
import json
import xlwt
import xlwings as xw
from selenium import webdriver
import time
import pandas as pd
import csv
import re
from selenium.webdriver import Chrome, ChromeOptions


# item_list=[]
df = pd.DataFrame()
def data_a(html):  # 获取基础信息1
    # with open('rrBand.html', 'r', encoding='utf-8') as f:
    # html = BeautifulSoup(f, 'lxml')
    # html.list = html.find_all('div', attrs={'class': 'container-sm float-left stock__main'})
    # print(html.list)
    df = pd.DataFrame()
    # print(html)
    for i, item in enumerate(html):
        # print(item)
        # print(html.list_a)
        try:
            bandNanme = item.find_all('div', attrs={'class', 'stock-name'})[0].text.strip('()')[0:].strip(')')
            df['序号'] = '',
            df['股票'] = item.find_all('div', attrs={'class', 'stock-name'})[0].text.strip('()')[0:bandNanme.find('(')].strip(
                        ')'),
            df['代码'] = item.find_all('div', attrs={'class', 'stock-name'})[0].text.strip('')[5:].strip(')').replace(':',''),
                    # print(df[['股票','代码']])
            df['股价'] = item.find_all('div', attrs={'class', 'stock-current'})[0].text.strip('¥'),
            print(df['股价'])
            html.list_a = item.find_all('table', attrs={'class', 'quote-info'})

            for i,item_a in enumerate(html.list_a):
                # print(item_a.find_all('span'))
                # for i in range(21):
                df['总市值(亿)'] = item_a.find_all('span')[19].text.strip('亿'),
                df['总股本(亿)'] = '',
                # df['营业额'] = '',
                df['EPS每股收益'] = item_a.find_all('span')[16].text.strip(''),
                # df['分红'] = '',
                df['分红率'] = '',
                df['营市比'] = '',
                # print(item_a.find_all('span')[i].text,i,sep=',')#_a.find_all('span')[18].text.strip('亿'),i,sep=','),
                df['PE市盈率'] = item_a.find_all('span')[10].text.strip(''),
                df['PB市净率'] = str((float(df['股价']) / float(item_a.find_all('span')[20].text.strip(''))))[0:4],
                # df['负债率'] = '',
                # print(df['PB市净率'])
                print(str(i),"第一模块写入正常")
        except:
            print(str(i), "第一模块写入异常")
            #     # continue
    return df
# df.to_csv('fundWebd.csv', index=None, encoding='utf-8-sig',sep=',')#mode='a', header=None,index=None,
# print(df[['股价','总市值','EPS每股收益']])

def data_b(html):  # 获取基础信息2
# url='https://xueqiu.com/snowman/S/SH600282/detail#/ZYCWZB'
# print('12')
#     with open('Band.html', 'r', encoding='utf-8') as f:
#         html = BeautifulSoup(f, 'lxml')
#         html.list_b = html.find_all('tbody')
        df = pd.DataFrame()
        bandIncome=[]
        for i, item in enumerate(html):
            # print(item)
            html.list_b_a=item.find_all('tr')
            for i,item in enumerate(html.list_b_a):
                html.list_b_a_a = item.find_all('td')
                # print(item.find_all('td'))
                for i, item in enumerate(html.list_b_a_a):
                    try:
                        html.list = item.find_all('p')[0].contents[0]
                        bandIncome.append(html.list)
                        # print(bandIncome,i,sep=',')
                        # for i, item in enumerate(html.list):
                        # print(item)
                        html.list_b = item.find_all('table', attrs={'class', 'quote-info'})
                        # print(html.list_a
                        # bandNanme = item.find_all('div', attrs={'class', 'stock-name'})[0].text.strip('()')[0:].strip(')')
                        df['17年利润(亿)'] = '',
                        df['20年利润(亿)'] = '',
                        print(str(i), "第二模块写入正常")
                    except:
                        # continue
                        print(str(i), "第二模块写入异常")
        df['营业额'] =bandIncome[0].strip('亿'),
        df['负债率'] =bandIncome[85].strip(''),
        df['经营现金流'] =bandIncome[50].strip(''),
        df['17年利润(亿)'] =bandIncome[13].strip('亿'),
        df['20年利润(亿)'] = bandIncome[10].strip('亿'),
        df['未分配利润'] = bandIncome[45].strip(''),
        df['公积金'] = bandIncome[40].strip(''),
        df['毛利率'] = bandIncome[75].strip(''),
        df['净利率'] = bandIncome[80].strip(''),
        df['ROA总报酬率'] = bandIncome[65].strip(''),
        df['ROE净收益率'] = bandIncome[60].strip(''),
        df['账款周期'] = bandIncome[120].strip(''),
        df['存货周转'] = bandIncome[150].strip(''),
        df['总资产周转率'] = bandIncome[145].strip(''),
        print(df)
        # for i in range(len(bandIncome)):
        #     print(bandIncome[i],i,sep=',')
        return df
def data_c(html):
    # with open('Band.html','r',encoding='utf-8') as f:
    #     # url='https://xueqiu.com/snowman/S/SH601991/detail#/FHPS'
    #     html=BeautifulSoup(f,'lxml')
    #     html.list=html.find_all('tbody')
    df = pd.DataFrame()
    for i,item in enumerate(html):
        try:
            cut_a=item.find_all('td')[1].text.strip().find('派')
            cut_b = item.find_all('td')[1].text.strip().find('元')
            # print(cut_a,cut_b)
            # print(item)
            print(item.find_all('td')[1].text.strip()[cut_a+1:cut_b])
            df['分红']=item.find_all('td')[1].text.strip()[cut_a+1:cut_b],
            print(df)
            print(str(i),"第三模块写入正常")
        except:
            print(str(i), "第三模块写入异常")
    return df
def data_d(html):
# with open('Band.html','r',encoding='utf-8') as f:
#     # url='https://xueqiu.com/snowman/S/SH600282/detail#/ZCFZB'
#     html=BeautifulSoup(f,'lxml')
#     html.list=html.find_all('tbody')
    df = pd.DataFrame()
    # print(html)
    for i, item in enumerate(html):
        # try:
        cut_a = item.find_all('td')[7].text.strip('').find('亿')
        # print(cut_a)
        # print(item.find_all('td')[7].text.strip('')[0: cut_a-1])
        # print(item.find_all('td')[61].text.strip('')[0: cut_a-1])
        df['货币资金']=item.find_all('td')[7].text.strip('')[0: cut_a-1],
        df['存货']=item.find_all('td')[61].text.strip('')[0: cut_a-1],
        # for i in range(300):
        #     print(item.find_all('p')[i],i,sep=',')
        print(df)
        print(str(i), "第四模块写入正常")
        # except:
        #     print(str(i), "第四模块写入正常")
    return df
def data_e(html):
# with open('Band.html','r',encoding='utf-8') as f:
#             # url='https://xueqiu.com/snowman/S/SH601600/detail#/GSLRB'
#             html=BeautifulSoup(f,'lxml')
#             html.list=html.find_all('tbody')
        df = pd.DataFrame()
        # print(html)
        for i, item in enumerate(html):
            try:
                cut_a = item.find_all('td')[43].text.strip('').find('亿')
                cut_b = item.find_all('td')[55].text.strip('').find('亿')
                cut_c = item.find_all('td')[61].text.strip('').find('亿')
                df['研发费用']=item.find_all('td')[43].text.strip('')[0:cut_a],
                df['利息费用'] =item.find_all('td')[55].text.strip('')[0:cut_b],
                df['利息收入'] = item.find_all('td')[61].text.strip('')[0:cut_c],
                # print(item.find_all('td')[43].text.strip('')[0:cut_a-1])
                # print(df['研发费用'].values, df['利息费用'].values, df['利息收入'].values ,sep=',')
                print(df)

                # for i in range(100):
                #     print(item.find_all('td')[i].text,i,sep=',')
                print(str(i), "第五模块写入正常")
            except:
                print(str(i), "第五模块写入异常")
        return df

# 写入csv中
if __name__ == "__main__":
    # 创建一个workbook

    app = xw.App(visible=False, add_book=False)
    wb = app.books.open('fundWebd.xlsx')
    # 创建一个worksheet
    sh = wb.sheets['worksheet']
    rng = [i for i in sh.range("c:c").value if i != None]#单元格内容
    j = sh.range('a1').expand('table').rows.count#序号
    app.display_alerts = False
    app.screen_updating = False
    # rng = sh.range('a1').expand('table')
    # nrows = rng.rows.count
    # a = sh.range(f'a1:a{nrows}').value
    # a = [ i for i in sht.range(a:a).value if i != None]

    # 打开网页
    opt = ChromeOptions()            # 创建Chrome参数对象
    opt.headless =False #True#              # 把Chrome设置成可视化无界面模式,
    driver = Chrome(options=opt)
    # driver = webdriver.Chrome()
    df_a=[]
    df_b=[]
    df_c = []
    df_d = []
    for i in range(len(rng)-1):
        print(str(i),rng[i],'第'+str(i+1)+'只股票开始写入')#rng[i+1]
        try:
            bandcode=rng[i+1]#'SH601600'
            xueqiu_url='https://xueqiu.com/S/'+bandcode#雪球网基础数据'https://c.runoob.com/'#很好的ide工具
            xueqiu_url_a='https://xueqiu.com/snowman/S/'+bandcode+'/detail#/ZYCWZB'#主要指标
            xueqiu_url_c= 'https://xueqiu.com/snowman/S/'+bandcode+'/detail#/FHPS'#分红
            xueqiu_url_d = 'https://xueqiu.com/snowman/S/'+bandcode+'/detail#/ZCFZB'  # 存货
            xueqiu_url_e ='https://xueqiu.com/snowman/S/'+bandcode+'/detail#/GSLRB'#研发、利息收入

            # DFCF_url='http://emweb.eastmoney.com/PC_HSF10/OperationsRequired/Index?type=web&code=SH601600'

            #基础数据1加载
            driver.get(xueqiu_url)#加载网址
            time.sleep(3)  # 休眠1秒
            source =driver.page_source#获取网页内容
            html=BeautifulSoup(source,'html.parser')#获取网页内容
            time.sleep(2)  # 休眠1秒
            html.list = html.find_all('div', attrs={'class': 'container-sm float-left stock__main'})
            df_a = data_a(html.list)  # 执行语句块
            time.sleep(3)  # 休眠1秒

            #基础数据2加载
            driver.back()  # 加载网址
            time.sleep(2)
            driver.get(xueqiu_url_a)  # 加载网址
            # driver.find_elements_by_class_name('btn active').click()
            # driver.find_element_by_xpath(".//*[@id='header']/div[1]/div/form/input[2]").click()
            time.sleep(4)
            driver.find_element_by_xpath(".//div[contains(@class,'stock-info-btn-list')]/span[2]").click()
            # print(driver.find_element_by_xpath(".//div[contains(@class,'stock-info-btn-list')]/span[2]").text)  # /span[contains(@class,'btn')]
            time.sleep(5)  # 休眠4秒
            source = driver.page_source  # 获取网页内容
            html = BeautifulSoup(source, 'html.parser')  # 获取网页内容
            # print(html)
            time.sleep(2)  # 休眠1秒
            html.list_b = html.find_all('tbody')
            df_b = data_b(html.list_b)  # 执行语句块
            time.sleep(2)

            #基础数据三加载
            driver.back()  # 加载网址
            time.sleep(4)
            driver.get(xueqiu_url_c)  # 加载网址
            time.sleep(5)
            source = driver.page_source  # 获取网页内容
            html = BeautifulSoup(source, 'html.parser')  # 获取网页内容
            # print(html)
            time.sleep(3)  # 休眠1秒
            html.list_c = html.find_all('tbody')
            df_c= data_c(html.list_c)  # 执行语句块
            # print(html)
            time.sleep(3)

            # 基础数据四加载
            driver.get(xueqiu_url)  # 加载网址
            time.sleep(2)
            driver.get(xueqiu_url_d)  # 加载网址
            time.sleep(4)
            driver.find_element_by_xpath(".//div[contains(@class,'stock-info-btn-list')]/span[2]").click()
            time.sleep(5)
            source = driver.page_source  # 获取网页内容
            html = BeautifulSoup(source, 'html.parser')  # 获取网页内容
            # print(html)
            time.sleep(1)  # 休眠1秒
            html.list_d=html.find_all('tbody')
            df_d= data_d(html.list_d)  # 执行语句块
            df_d.to_json('fundWebdTest.json', orient='records', force_ascii=False)  # ,orient="values")
            time.sleep(1)

            # 基础数据五加载
            driver.back()
            time.sleep(2)
            driver.get(xueqiu_url_e )#xueqiu_url_e
            time.sleep(4)
            driver.find_element_by_xpath(".//div[contains(@class,'stock-info-btn-list')]/span[2]").click()
            time.sleep(2)
            source=driver.page_source# 获取网页内容
            html=BeautifulSoup(source,'html.parser')# 获取网页内容
            # print(html)
            time.sleep(1)  # 休眠1秒
            html.list_e = html.find_all('tbody')
            df_e = data_e(html.list_e)  # 执行语句块
            df_e.to_json('fundWebdTest.json', orient='records', force_ascii=False)  # ,orient="values")
            time.sleep(1)
            # with open('rrBand.html','w',encoding='utf-8') as f:#写入网页
            #     f.write(source)

        except:
            # continue
            print(str(i),"当页数据操作失败")

        # with open('rrBand.html', 'r', encoding='utf-8') as f:
        #     html = BeautifulSoup(f, 'lxml')
        #     html.list = html.find_all('div', attrs={'class': 'container-sm float-left stock__main'})
        #     df_a = data_a(html.list)  # 执行语句块
        # with open('Band.html', 'r', encoding='utf-8') as f:
        #     html = BeautifulSoup(f, 'lxml')
        #     html.list_b = html.find_all('tbody')
        #     df_b = data_b(html.list_b)  # 执行语句块
        # with open('Band.html', 'r', encoding='utf-8') as f:
        #      html = BeautifulSoup(f, 'lxml')
        #      html.list_b = html.find_all('tbody')
        # with open('Band.html','r',encoding='utf-8') as f:
        #     # url='https://xueqiu.com/snowman/S/SH600282/detail#/ZCFZB'
        #     html=BeautifulSoup(f,'lxml')
        #     html.list=html.find_all('tbody')
        # with open('Band.html','r',encoding='utf-8') as f:
        # # url='https://xueqiu.com/snowman/S/SH601600/detail#/GSLRB'
        #html=BeautifulSoup(f,'lxml')
        #html.list=html.find_all('tbody')
        

        #以下为写入模板
        # df=pd.concat([df_a,df_b],axis=1)#列合并,axis=0表示按行合并df = df_a.append(df_b)
        # print(df_c,'测试')
        df1=pd.concat([df_a,df_b],axis=1)#按列合并
        # print(df1)
        df2=pd.concat([df1,df_c],axis=1)#按列合并
        # print(df2)
        df3 = pd.concat([df2, df_d], axis=1)  # 按列合并
        # print(df3)
        df4 = pd.concat([df3, df_e], axis=1)  # 按列合并
        print(df4)
        df=pd.concat([df4,df],axis=0)#按行合并,这里很重要
        print(df)
        df.to_csv("fundWebd.csv", mode="a+", header=None, index=None, encoding="utf-8-sig",sep=',')#提前写入vsv文件
        # item_list.append(df)
        # print(item_list)

        df.to_json('fundWebd.json',orient ='records', force_ascii=False)#,orient="values")
        # with open('fundWebd.json','r',encoding='utf-8') as f:
        #     data = json.load(f)
        # item_list.append(data)
        # with open('.fund.json', 'w', encoding='utf-8')as f:
        #     json.dump(item_list,f, indent=1, ensure_ascii=False)
        with open('fundWebd.json', 'r', encoding='utf-8') as f:
            data = json.load(f)
            # print(data[0]['股票'])
        bandN = ['序号', '股票', '代码', '股价', '总市值(亿)', '总股本(亿)', '营业额', 'EPS每股收益', '分红', '分红率', '营市比', 'PE市盈率',\
                      'PB市净率', '负债率','经营现金流','货币资金','存货','利息费/收','17年利润(亿)','20年利润(亿)','利润复增率','营业额复合增长率',\
                      '季度增长率','现金收入比','PEG','未分配利润','公积金','毛利率','净利率','ROA总报酬率','ROE净收益率','账款周期','存货周转','总资产周转率']
        for i in range(len(data)):#写入数据
            try:
                print(len(data))
                sh.cells[i+1,0].value=i+1
                sh.cells[i+1, 1].value=data[i][bandN[1]]
                sh.cells[i+1, 2].value=data[i][bandN[2]]
                sh.cells[i+1, 3].value = data[i][bandN[3]]
                sh.cells[i+1, 4].value = data[i][bandN[4]]
                sh.cells[i+1, 5].value = data[i][bandN[5]]
                sh.cells[i+1, 6].value = data[i][bandN[6]]#营业额
                sh.cells[i+1, 7].value = data[i][bandN[7]]
                sh.cells[i+1, 8].value = data[i][bandN[8]]#分红
                # sh.cells[i+1, 9].value = data[i][bandN[9]]#分红率
                # sh.cells[i+1, 10].value = data[i][bandN[10]]#营市比
                sh.cells[i+1, 11].value = data[i][bandN[11]]
                sh.cells[i+1, 12].value = data[i][bandN[12]]
                sh.cells[i+1, 13].value = data[i][bandN[13]]#负债率
                sh.cells[i + 1, 14].value = data[i][bandN[14]]  # 经营现金流
                sh.cells[i + 1, 15].value = data[i][bandN[15]]  # 货币资金
                sh.cells[i + 1, 16].value = data[i][bandN[16]]  # 存货

                sh.cells[i + 1, 17].value = round(float(data[i]['利息费用'])/float(data[i]['利息收入']),2)  # 利息费/收
                sh.cells[i + 1, 17].value = data[i]['利息费用']+' /'+ data[i]['利息收入']  # 利息费/收
                # sh.cells[i + 20, 17].value =data[i]['利息费用']
                # sh.cells[i + 20, 18].value =data[i]['利息收入']

                sh.cells[i + 1, 18].value = data[i][bandN[18]]  # 17年利润(亿)
                sh.cells[i + 1, 19].value = data[i][bandN[19]]  # 20年利润(亿)
                # sh.cells[i + 1, 20].value = data[i][bandN[20]]  # 利润复增率
                # sh.cells[i + 1, 21].value = data[i][bandN[21]]  # 营业额复合增长率
                # sh.cells[i + 1, 22].value = data[i][bandN[22]]  # 季度增长率
                # sh.cells[i + 1, 23].value = data[i][bandN[23]]  # 现金收入比
                # sh.cells[i + 1, 24].value = data[i][bandN[24]]  # PEG
                sh.cells[i + 1, 25].value = data[i][bandN[25]]  # 未分配利润
                sh.cells[i + 1, 26].value = data[i][bandN[26]]  # 公积金
                sh.cells[i + 1, 27].value = data[i][bandN[27]]  # 毛利率
                sh.cells[i + 1, 28].value = data[i][bandN[28]]  # 净利率
                sh.cells[i + 1, 29].value = data[i][bandN[29]]  # ROA总报酬率
                sh.cells[i + 1, 30].value = data[i][bandN[30]]  # ROE净收益率
                sh.cells[i + 1, 31].value = data[i][bandN[31]]  # 账款周期
                sh.cells[i + 1, 32].value = data[i][bandN[32]]  # 存货周转
                sh.cells[i + 1, 33].value = data[i][bandN[33]]  # 总资产周转率
                # sh.cells[i + 1, 32].value = data[i][bandN[32]]  # 存货周转
                # sh.cells[i + 1, 32].value = data[i][bandN[32]]  # 存货周转
                # sh.cells[i + 1, 32].value = data[i][bandN[32]]  # 存货周转
                # sh.cells[i + 1, 43].value =round(float(data[i]['研发费用'])/float(data[i][bandN[6]]),4)  # 研发/收入比
                sh.cells[i + 1, 34].value = data[i]['研发费用']+'/'+ data[i][bandN[6]]# 研发/收入比
                # sh.cells[i + 1, 45].value = float(data[i][bandN[6]])  # 研发/收入比
                    # print(i)
            except:
                # continue
                print('excel写入错误')

    try:
        wb.save('fundWebd.xlsx')
        app.quit()
        # 获得当前窗口句柄
        sreach_windows = driver.current_window_handle
        driver.quit()
        # 获得当前所有打开的窗口的句柄
        all_handles = driver.window_handles
        for handle in all_handles:
            driver.switch_to.window(handle)
            driver.close()
            time.sleep(2)
        # driver.close()
        # driver.quit()
    except:
        print('有错误代码')








完整代码:3,重新调整市盈率、市净率

import xlwings as xw
import requests
from bs4 import BeautifulSoup
from datetime import datetime
import json
import xlwt
import xlwings as xw
from selenium import webdriver
import time
import pandas as pd
import csv
import re
from selenium.webdriver import Chrome, ChromeOptions


# item_list=[]
df = pd.DataFrame()
def data_a(html,numcode):  # 获取基础信息1
    # with open('rrBand.html', 'r', encoding='utf-8') as f:
    # html = BeautifulSoup(f, 'lxml')
    # html.list = html.find_all('div', attrs={'class': 'container-sm float-left stock__main'})
    # print(html.list)
    # numcode=''
    df = pd.DataFrame()
    # print(numcode,'一')
    # print(html)
    for i, item in enumerate(html):
        # print(item)
        # print(html.list_a)
        try:
            bandNanme = item.find_all('div', attrs={'class', 'stock-name'})[0].text.strip('()')[0:].strip(')')
            df['序号'] = '',
            # df['股票'] = item.find_all('div', attrs={'class', 'stock-name'})[0].text.strip('()')[0:bandNanme.find('(')].strip(
            #             ')'),
            # df['代码'] = item.find_all('div', attrs={'class', 'stock-name'})[0].text.strip('')[5:].strip(')').replace(':',''),
                    # print(df[['股票','代码']])
            df['股价'] = item.find_all('div', attrs={'class', 'stock-current'})[0].text.strip('¥'),
            # print(df['股价'])
            html.list_a = item.find_all('table', attrs={'class', 'quote-info'})
            for i,item_a in enumerate(html.list_a):
                # print(item_a.find_all('span'))
                # for i in range(25):
                #     print(item_a.find_all('span')[i].text,i,sep=',')
                df['总股本(亿)'] = '',
                df['营市比'] = '',
                # print(item_a.find_all('span')[i].text,i,sep=',')#_a.find_all('span')[18].text.strip('亿'),i,sep=','),
                # print(item_a.find_all('span')[19].text.strip('亿'))
                # df['总市值(亿)'] = item_a.find_all('span')[19].text.strip('亿'),
                # df['PE市盈率'] = item_a.find_all('span')[10].text.strip(''),
                # df['PB市净率'] = item_a.find_all('span')[15].text.strip(''),
                # print( df['总市值(亿)'])
                if numcode=='SH':
                    print(numcode,'上海')
                    df['总市值(亿)'] =item_a.find_all('span')[19].text.strip('亿'),
                    df['PE市盈率'] = item_a.find_all('span')[10].text.strip(''),
                    df['PB市净率'] = item_a.find_all('span')[15].text.strip(''),
                elif numcode=='SZ':
                    print(numcode,'深圳')
                    df['总市值(亿)'] = item_a.find_all('span')[11].text.strip('亿'),
                    df['PE市盈率'] = item_a.find_all('span')[16].text.strip(''),
                    df['PB市净率'] = item_a.find_all('span')[21].text.strip(''),
                elif numcode=='':
                    df['总市值(亿)'] = item_a.find_all('span')[19].text.strip('亿'),
                    df['PE市盈率'] = item_a.find_all('span')[10].text.strip(''),
                    df['PB市净率'] = item_a.find_all('span')[15].text.strip(''),
                else:
                    print('错误')
                # print(df['PB市净率'])
                print(df)
                print(str(i),"第一模块写入正常")
        except:
            print(str(i), "第一模块写入异常")
            #     # continue
    return df




# df.to_csv('fundWebd.csv', index=None, encoding='utf-8-sig',sep=',')#mode='a', header=None,index=None,
# print(df[['股价','总市值','EPS每股收益']])

def data_b(html):  # 获取基础信息2
# url='https://xueqiu.com/snowman/S/SH600282/detail#/ZYCWZB'
# print('12')
#     with open('Band.html', 'r', encoding='utf-8') as f:
#         html = BeautifulSoup(f, 'lxml')
#         html.list_b = html.find_all('tbody')
        df = pd.DataFrame()
        bandIncome=[]
        for i, item in enumerate(html):
            # print(item)
            html.list_b_a=item.find_all('tr')
            for i,item in enumerate(html.list_b_a):
                html.list_b_a_a = item.find_all('td')
                # print(item.find_all('td'))
                for i, item in enumerate(html.list_b_a_a):
                    try:
                        html.list = item.find_all('p')[0].contents[0]
                        bandIncome.append(html.list)
                        # print(bandIncome,i,sep=',')
                        # for i, item in enumerate(html.list):
                        # print(item)
                        html.list_b = item.find_all('table', attrs={'class', 'quote-info'})
                        # print(html.list_a
                        # bandNanme = item.find_all('div', attrs={'class', 'stock-name'})[0].text.strip('()')[0:].strip(')')
                        # df['17年利润(亿)'] = '',
                        # df['20年利润(亿)'] = '',
                        print(str(i), "第二模块写入正常")
                    except:
                        # continue
                        print(str(i), "第二模块写入异常")
        df['营业额'] =bandIncome[0].strip('亿'),
        df['EPS每股收益'] =bandIncome[30].strip(''),#item_a.find_all('span')[16].text.strip(''),
        df['负债率'] =bandIncome[85].strip(''),
        df['经营现金流'] =bandIncome[50].strip(''),
        df['17年利润(亿)'] =bandIncome[13].strip('亿'),
        df['20年利润(亿)'] = bandIncome[10].strip('亿'),
        df['未分配利润'] = bandIncome[45].strip(''),
        df['公积金'] = bandIncome[40].strip(''),
        df['毛利率'] = bandIncome[75].strip(''),
        df['净利率'] = bandIncome[80].strip(''),
        df['ROA总报酬率'] = bandIncome[65].strip(''),
        df['ROE净收益率'] = bandIncome[55].strip(''),
        df['账款周期'] = bandIncome[125].strip(''),
        df['存货周转'] = bandIncome[120].strip(''),
        df['总资产周转率'] = bandIncome[145].strip(''),
        print(df)
        # for i in range(len(bandIncome)):
        #     print(bandIncome[i],i,sep=',')
        return df
def data_c(html,html1):
    # with open('Band.html','r',encoding='utf-8') as f:
    #     # url='https://xueqiu.com/snowman/S/SH601991/detail#/FHPS'
    #     html=BeautifulSoup(f,'lxml')
    #     html.list=html.find_all('tbody')
    df = pd.DataFrame()
    for i,item in enumerate(html1):
        # print(item.text)
        bandNanme=item.text
        # print(item.text.strip('')[bandNanme.find('('):].strip(')'))
        df['股票'] = item.text.strip('')[0:bandNanme.find('(')].strip(')'),
        df['代码'] = item.text.strip('')[bandNanme.find('(')+1:].strip(')'),
        # print(df)
        # print(df['股票'].valuse, df['代码'].values)
    # df['股票'] = item.find_all('div', attrs={'class', 'stock-name'})[0].text.strip('()')[0:bandNanme.find('(')].strip(
    #             ')'),
    # df['代码'] = item.find_all('div', attrs={'class', 'stock-name'})[0].text.strip('')[5:].strip(')').replace(':',''),
    # print(df[['股票','代码']])
    for i,item in enumerate(html):
        try:
            cut_a=item.find_all('td')[1].text.strip().find('派')
            cut_b = item.find_all('td')[1].text.strip().find('元')
            # print(cut_a,cut_b)
            # print(item)
            # print(item.find_all('td')[1].text.strip()[cut_a+1:cut_b])
            df['分红率'] = '',
            df['分红']=item.find_all('td')[1].text.strip()[cut_a+1:cut_b],
            print(df)
            print(str(i),"第三模块写入正常")
        except:
            print(str(i), "第三模块写入异常")
    return df
def data_d(html):
# with open('Band.html','r',encoding='utf-8') as f:
#     # url='https://xueqiu.com/snowman/S/SH600282/detail#/ZCFZB'
#     html=BeautifulSoup(f,'lxml')
#     html.list=html.find_all('tbody')
    df = pd.DataFrame()
    # print(html)
    for i, item in enumerate(html):
        # try:
        cut_a = item.find_all('td')[7].text.strip('').find('亿')
        # print(cut_a)
        # print(item.find_all('td')[7].text.strip('')[0: cut_a-1])
        # print(item.find_all('td')[61].text.strip('')[0: cut_a-1])
        df['货币资金']=item.find_all('td')[7].text.strip('')[0: cut_a],
        df['存货']=item.find_all('td')[61].text.strip('')[0: cut_a],
        # for i in range(300):
        #     print(item.find_all('p')[i],i,sep=',')
        print(df)
        print(str(i), "第四模块写入正常")
        # except:
        #     print(str(i), "第四模块写入正常")
    return df
def data_e(html):
# with open('Band.html','r',encoding='utf-8') as f:
#             # url='https://xueqiu.com/snowman/S/SH601600/detail#/GSLRB'
#             html=BeautifulSoup(f,'lxml')
#             html.list=html.find_all('tbody')
        df = pd.DataFrame()
        # print(html)
        for i, item in enumerate(html):
            try:
                cut_a = item.find_all('td')[43].text.strip('').find('+')
                cut_b = item.find_all('td')[55].text.strip('').find('+')
                cut_c = item.find_all('td')[61].text.strip('').find('+')
                df['研发费用']=item.find_all('td')[43].text.strip(''),#[0:cut_a],
                df['利息费用'] =item.find_all('td')[55].text.strip(''),#[0:cut_b],
                df['利息收入'] = item.find_all('td')[61].text.strip(''),#[0:cut_c],
                # print(item.find_all('td')[43].text.strip('')[0:cut_a-1])
                # print(df['研发费用'].values, df['利息费用'].values, df['利息收入'].values ,sep=',')
                print(df)

                # for i in range(100):
                #     print(item.find_all('td')[i].text,i,sep=',')
                print(str(i), "第五模块写入正常")
            except:
                print(str(i), "第五模块写入异常")
        return df

# 写入csv中
if __name__ == "__main__":
    # 创建一个workbook

    app = xw.App(visible=False, add_book=False)
    wb = app.books.open('fundWebd.xlsx')
    # 创建一个worksheet
    sh = wb.sheets['worksheet']
    rng = [i for i in sh.range("c:c").value if i != None]#单元格内容
    j = sh.range('a1').expand('table').rows.count#序号
    app.display_alerts = False
    app.screen_updating = False
    # rng = sh.range('a1').expand('table')
    # nrows = rng.rows.count
    # a = sh.range(f'a1:a{nrows}').value
    # a = [ i for i in sht.range(a:a).value if i != None]

    # 打开网页
    opt = ChromeOptions()            # 创建Chrome参数对象
    opt.headless =False #True#              # 把Chrome设置成可视化无界面模式,
    driver = Chrome(options=opt)
    # driver = webdriver.Chrome()
    df_a=[]
    df_b=[]
    df_c = []
    df_d = []
    df_e = []
    for i in range(len(rng)-1):
        print(str(i),rng[i+1],'第'+str(i+1)+'只股票开始写入')#rng[i+1]
        try:
            bandcode=rng[i+1]#'SH601600'
            xueqiu_url='https://xueqiu.com/S/'+bandcode#雪球网基础数据'https://c.runoob.com/'#很好的ide工具
            xueqiu_url_a='https://xueqiu.com/snowman/S/'+bandcode+'/detail#/ZYCWZB'#主要指标
            xueqiu_url_c= 'https://xueqiu.com/snowman/S/'+bandcode+'/detail#/FHPS'#分红
            xueqiu_url_d = 'https://xueqiu.com/snowman/S/'+bandcode+'/detail#/ZCFZB'  # 存货
            xueqiu_url_e ='https://xueqiu.com/snowman/S/'+bandcode+'/detail#/GSLRB'#研发、利息收入

            # DFCF_url='http://emweb.eastmoney.com/PC_HSF10/OperationsRequired/Index?type=web&code=SH601600'

            #基础数据1加载
            driver.get(xueqiu_url)#加载网址
            time.sleep(1)  # 休眠1秒
            source =driver.page_source#获取网页内容
            html=BeautifulSoup(source,'html.parser')#获取网页内容
            time.sleep(1)  # 休眠1秒
            html.list = html.find_all('div', attrs={'class': 'container-sm float-left stock__main'})
            numcode=''#rng[i+1].strip('')[0:2]
            # print(numcode)
            df_a = data_a(html.list,numcode)  # 执行语句块
            df_a.to_json('fundWebdTest.json', orient='records', force_ascii=False)  # ,orient="values")
            # time.sleep(1)  # 休眠1秒

            #基础数据2加载主要指教
            driver.back()  # 加载网址
            # time.sleep(1)
            driver.get(xueqiu_url_a)  # 加载网址
            # driver.find_elements_by_class_name('btn active').click()
            # driver.find_element_by_xpath(".//*[@id='header']/div[1]/div/form/input[2]").click()
            time.sleep(2)
            driver.find_element_by_xpath(".//div[contains(@class,'stock-info-btn-list')]/span[2]").click()
            # print(driver.find_element_by_xpath(".//div[contains(@class,'stock-info-btn-list')]/span[2]").text)  # /span[contains(@class,'btn')]
            time.sleep(3)  # 休眠4秒
            source = driver.page_source  # 获取网页内容
            html = BeautifulSoup(source, 'html.parser')  # 获取网页内容
            # print(html)
            time.sleep(1)  # 休眠1秒
            html.list_b = html.find_all('tbody')
            df_b = data_b(html.list_b)  # 执行语句块
            time.sleep(1)

            #基础数据三加载分红
            driver.back()  # 加载网址
            # time.sleep(1)
            driver.get(xueqiu_url_c)  # 加载网址
            time.sleep(2)
            source = driver.page_source  # 获取网页内容
            html = BeautifulSoup(source, 'html.parser')  # 获取网页内容
            # print(html)
            time.sleep(1)  # 休眠1秒
            html.list_c = html.find_all('tbody')
            html.list_c2 = html.find_all('div',attrs={'stock-info-name'})
            # print(html.list_c2)
            df_c= data_c(html.list_c,html.list_c2)  # 执行语句块
            # print(html)
            time.sleep(1)

            # 基础数据四加载资产负债表
            driver.get(xueqiu_url)  # 加载网址
            time.sleep(1)
            driver.get(xueqiu_url_d)  # 加载网址
            time.sleep(2)
            driver.find_element_by_xpath(".//div[contains(@class,'stock-info-btn-list')]/span[2]").click()
            time.sleep(3)
            source = driver.page_source  # 获取网页内容
            html = BeautifulSoup(source, 'html.parser')  # 获取网页内容
            # print(html)
            time.sleep(1)  # 休眠1秒
            html.list_d=html.find_all('tbody')
            df_d= data_d(html.list_d)  # 执行语句块
            df_d.to_json('fundWebdTest.json', orient='records', force_ascii=False)  # ,orient="values")
            time.sleep(1)

            # 基础数据五加载利润表
            driver.back()
            time.sleep(1)
            driver.get(xueqiu_url_e )#xueqiu_url_e
            time.sleep(2)
            driver.find_element_by_xpath(".//div[contains(@class,'stock-info-btn-list')]/span[2]").click()
            time.sleep(3)
            source=driver.page_source# 获取网页内容
            html=BeautifulSoup(source,'html.parser')# 获取网页内容
            # print(html)
            time.sleep(1)  # 休眠1秒
            html.list_e = html.find_all('tbody')
            df_e = data_e(html.list_e)  # 执行语句块
            # df_e.to_json('fundWebdTest.json', orient='records', force_ascii=False)  # ,orient="values")
            time.sleep(2)
            # with open('rrBand.html','w',encoding='utf-8') as f:#写入网页
            # f.write(source)
        except:
            # continue
            print(str(i),"当页数据操作失败")

            # with open('rrBand.html', 'r', encoding='utf-8') as f:
            #     html = BeautifulSoup(f, 'lxml')
            #     html.list = html.find_all('div', attrs={'class': 'container-sm float-left stock__main'})
            #     df_a = data_a(html.list)  # 执行语句块
            # with open('Band.html', 'r', encoding='utf-8') as f:
            #     html = BeautifulSoup(f, 'lxml')
            #     html.list_b = html.find_all('tbody')
            #     df_b = data_b(html.list_b)  # 执行语句块
            # with open('Band.html', 'r', encoding='utf-8') as f:
            #      html = BeautifulSoup(f, 'lxml')
            #      html.list_b = html.find_all('tbody')
            # with open('Band.html','r',encoding='utf-8') as f:
            #     # url='https://xueqiu.com/snowman/S/SH600282/detail#/ZCFZB'
            #     html=BeautifulSoup(f,'lxml')
            #     html.list=html.find_all('tbody')
            # with open('Band.html','r',encoding='utf-8') as f:
            # # url='https://xueqiu.com/snowman/S/SH601600/detail#/GSLRB'
            #html=BeautifulSoup(f,'lxml')
            #html.list=html.find_all('tbody')

        #以下为写入模板
        # df=pd.concat([df_a,df_b],axis=1)#列合并,axis=0表示按行合并df = df_a.append(df_b)
        # print(df_c,'测试')
        df1=pd.concat([df_a,df_b],axis=1)#按列合并
        # print(df1)
        df2=pd.concat([df1,df_c],axis=1)#按列合并
        # print(df2)
        df3 = pd.concat([df2, df_d], axis=1)  # 按列合并
        # print(df3)
        df4 = pd.concat([df3, df_e], axis=1)  # 按列合并
        print(df4)
        df=pd.concat([df4,df],axis=0)#按行合并,这里很重要
        print(df)
        df.to_csv("fundWebd.csv", mode="a+", header=None, index=None, encoding="utf-8-sig",sep=',')#提前写入vsv文件
        # item_list.append(df)
        # print(item_list)

        df.to_json('fundWebd.json',orient ='records', force_ascii=False)#,orient="values")
        # with open('fundWebd.json','r',encoding='utf-8') as f:
        #     data = json.load(f)
        # item_list.append(data)
        # with open('.fund.json', 'w', encoding='utf-8')as f:
        #     json.dump(item_list,f, indent=1, ensure_ascii=False)
        with open('fundWebd.json', 'r', encoding='utf-8') as f:
            data = json.load(f)
            # print(data[0]['股票'])
        bandN = ['序号', '股票', '代码', '股价', '总市值(亿)', '总股本(亿)', '营业额', 'EPS每股收益', '分红', '分红率', '营市比', 'PE市盈率',\
                      'PB市净率', '负债率','经营现金流','货币资金','存货','利息费/收','17年利润(亿)','20年利润(亿)','利润复增率','营业额复合增长率',\
                      '季度增长率','现金收入比','PEG','未分配利润','公积金','毛利率','净利率','ROA总报酬率','ROE净收益率','账款周期','存货周转','总资产周转率']
        for i in range(len(data)):#写入数据
            try:
                print(len(data))
                sh.cells[i+1,0].value=i+1
                sh.cells[i+1, 1].value=data[i][bandN[1]]
                sh.cells[i+1, 2].value=data[i][bandN[2]]
                sh.cells[i+1, 3].value = data[i][bandN[3]]
                sh.cells[i+1, 4].value = data[i][bandN[4]]
                sh.cells[i+1, 5].value = round(float(data[i]['总市值(亿)'])/float(data[i]['股价']),2)
                sh.cells[i+1, 6].value = data[i][bandN[6]]#营业额
                sh.cells[i+1, 7].value = data[i][bandN[7]]
                sh.cells[i+1, 8].value = data[i][bandN[8]]#分红
                # sh.cells[i+1, 9].value = data[i][bandN[9]]#分红率
                # sh.cells[i+1, 10].value = data[i][bandN[10]]#营市比
                sh.cells[i+1, 11].value = data[i][bandN[11]]
                sh.cells[i+1, 12].value = data[i][bandN[12]]
                sh.cells[i+1, 13].value = data[i][bandN[13]]+'%'#负债率
                sh.cells[i + 1, 14].value = data[i][bandN[14]]  # 经营现金流
                sh.cells[i + 1, 15].value = data[i][bandN[15]]  # 货币资金
                sh.cells[i + 1, 16].value = data[i][bandN[16]]  # 存货
                # sh.cells[i + 1, 17].value = round(float(data[i]['利息费用'])/float(data[i]['利息收入']),2)  # 利息费/收
                sh.cells[i + 1, 17].value = data[i]['利息费用']+' /'+ data[i]['利息收入']  # 利息费/收
                # sh.cells[i + 20, 17].value =data[i]['利息费用']
                # sh.cells[i + 20, 18].value =data[i]['利息收入']

                sh.cells[i + 1, 18].value = data[i][bandN[18]]  # 17年利润(亿)
                sh.cells[i + 1, 19].value = data[i][bandN[19]]  # 20年利润(亿)
                # sh.cells[i + 1, 20].value = data[i][bandN[20]]  # 利润复增率
                # sh.cells[i + 1, 21].value = data[i][bandN[21]]  # 营业额复合增长率
                # sh.cells[i + 1, 22].value = data[i][bandN[22]]  # 季度增长率
                # sh.cells[i + 1, 23].value = data[i][bandN[23]]  # 现金收入比
                # sh.cells[i + 1, 24].value = data[i][bandN[24]]  # PEG
                sh.cells[i + 1, 25].value = data[i][bandN[25]]  # 未分配利润
                sh.cells[i + 1, 26].value = data[i][bandN[26]]  # 公积金
                sh.cells[i + 1, 27].value = data[i][bandN[27]]+'%' # 毛利率
                sh.cells[i + 1, 28].value = data[i][bandN[28]]+'%'  # 净利率
                sh.cells[i + 1, 29].value = data[i][bandN[29]]+'%' # ROA总报酬率
                sh.cells[i + 1, 30].value = data[i][bandN[30]]+'%' # ROE净收益率
                sh.cells[i + 1, 31].value = data[i][bandN[31]]  # 账款周期
                sh.cells[i + 1, 32].value = data[i][bandN[32]]  # 存货周转
                sh.cells[i + 1, 33].value = data[i][bandN[33]]+'%' # 总资产周转率
                # sh.cells[i + 1, 32].value = data[i][bandN[32]]  # 存货周转
                # sh.cells[i + 1, 32].value = data[i][bandN[32]]  # 存货周转
                # sh.cells[i + 1, 32].value = data[i][bandN[32]]  # 存货周转
                # sh.cells[i + 1, 43].value =round(float(data[i]['研发费用'])/float(data[i][bandN[6]]),4)  # 研发/收入比
                sh.cells[i + 1, 34].value = data[i]['研发费用']+'/'+ data[i][bandN[6]]# 研发/收入比
                # sh.cells[i + 1, 45].value = float(data[i][bandN[6]])  # 研发/收入比
                    # print(i)
            except:
                # continue
                print(str(i),'excel写入错误')


    try:
        wb.save('fundWebd.xlsx')
        app.quit()
        # 获得当前窗口句柄
        sreach_windows = driver.current_window_handle
        driver.quit()
        # 获得当前所有打开的窗口的句柄
        all_handles = driver.window_handles
        for handle in all_handles:
            driver.switch_to.window(handle)
            driver.close()
            time.sleep(2)
        # driver.close()
        # driver.quit()
    except:
        print('有错误代码')








  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2021-09-14 13:39:31  更:2021-09-14 13:40:44 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/18 0:46:58-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码