东方财富历史资金流获取4000多个网页只要1个小时

# http://data.eastmoney.com/zjlx/300670.html
# http://data.eastmoney.com/zjlx/002157.html
# 下载数据的链接
import pandas as pd
from lxml import etree
df=pd.read_excel('东方财富数据.xlsx',converters={'股票代码':str})
stockcode=df['股票代码'].tolist()
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_experimental_option("excludeSwitches", ['enable-automation'])
prefs = {
            'profile.default_content_setting_values': {
                'images': 2,
            }
        }
options.add_experimental_option('prefs', prefs)
import re,time,random
driver = webdriver.Chrome('C:\Program Files\Google\Chrome\Application\chromedriver.exe',options = options)

#股票代码
code_list=[]
date_list = []
# 收盘价
ClosingPrice_list =[]
# 涨跌幅
Quotechange_list =[]
# 主力净流入净额
Mainnetinflow_list = []
# 主力净流入占比
Pmainnetinflow_list = []
# 超大单净流入净额
superlargeflow_list = []
# 超大单净流入占比
Psuperlargeflow_list =[]
# 大单净流入净额
largeflow_list =[]
# 大单净流入占比
Plargeflow_list = []
# 中单净流入净额
midflow_list =[]
# 中单净流入占比
Pmidflow_list =[]
# 小单净流入净额
smallflow_list =[]
# 小单净流入占比
Psmallflow_list = []




for code in stockcode:
    req=driver.get('http://data.eastmoney.com/zjlx/{}.html'.format(code))
    time.sleep(0.3 + random.randint(1, 10) / 10)
    html = etree.HTML(driver.page_source)
    items = html.xpath('//*[@id="table_ls"]/table/tbody')[0]
        # print(items)
    # try:
    #     for item in items:
    # 日期
    datelist=items.xpath('./tr/td[1]')
    # 收盘价
    ClosingPricelist = items.xpath('./tr/td[2]')
    # 涨跌幅
    Quotechangelist =items.xpath('./tr/td[3]')
    # 主力净流入净额
    Mainnetinflowlist=items.xpath('./tr/td[4]')
    # 主力净流入占比
    Pmainnetinflowlist=items.xpath('./tr/td[5]')
    # 超大单净流入净额
    superlargeflowlist=items.xpath('./tr/td[6]')
    # 超大单净流入占比
    Psuperlargeflowlist=items.xpath('./tr/td[7]')
    # 大单净流入净额
    largeflowlist=items.xpath('./tr/td[8]')
    # 大单净流入占比
    Plargeflowlist=items.xpath('./tr/td[9]')
    # 中单净流入净额
    midflowlist=items.xpath('./tr/td[10]')
    # 中单净流入占比
    Pmidflowlist=items.xpath('./tr/td[11]')
    # 小单净流入净额
    smallflowlist=items.xpath('./tr/td[12]')
    # 小单净流入占比
    Psmallflowlist=items.xpath('./tr/td[13]')
    if len(datelist)==0:
        print(code)
    else:
        date_text=[x.xpath('.//text()')[0] for x in datelist]
        # 输入code
        code_text=['{}'.format(code)]*len(date_text)
        ClosingPrice_text = [x.xpath('.//text()')[0] for x in ClosingPricelist]
        Quotechange_text = [x.xpath('.//text()')[0] for x in Quotechangelist]
        Mainnetinflowlist_text = [x.xpath('.//text()')[0] for x in Mainnetinflowlist]
        Pmainnetinflowlist_text = [x.xpath('.//text()')[0] for x in Pmainnetinflowlist]
        superlargeflowlist_text = [x.xpath('.//text()')[0] for x in superlargeflowlist]
        Psuperlargeflowlist_text = [x.xpath('.//text()')[0] for x in Psuperlargeflowlist]
        largeflowlist_text = [x.xpath('.//text()')[0] for x in largeflowlist]
        Plargeflowlist_text = [x.xpath('.//text()')[0] for x in Plargeflowlist]
        midflowlist_text = [x.xpath('.//text()')[0] for x in midflowlist]
        Pmidflowlist_text = [x.xpath('.//text()')[0] for x in Pmidflowlist]
        smallflowlist_text = [x.xpath('.//text()')[0] for x in smallflowlist]
        Psmallflowlist_text = [x.xpath('.//text()')[0] for x in Psmallflowlist]
        # print(date_text)
        # print(ClosingPrice_text)
        # print(code+'****')
        # 股票代码
        code_list+=code_text
        date_list+=date_text
        # 收盘价
        ClosingPrice_list+=ClosingPrice_text
        # 涨跌幅
        Quotechange_list+=Quotechange_text
        # 主力净流入净额
        Mainnetinflow_list+=midflowlist_text
        # 主力净流入占比
        Pmainnetinflow_list+=Pmainnetinflowlist_text
        # 超大单净流入净额
        superlargeflow_list+=superlargeflowlist_text
        # 超大单净流入占比
        Psuperlargeflow_list+=Psuperlargeflowlist_text
        # 大单净流入净额
        largeflow_list+=largeflowlist_text
        # 大单净流入占比
        Plargeflow_list+=Plargeflowlist_text
        # 中单净流入净额
        midflow_list+=midflowlist_text
        # 中单净流入占比
        Pmidflow_list+=Pmidflowlist_text
        # 小单净流入净额
        smallflow_list+=smallflowlist_text
        # 小单净流入占比
        Psmallflow_list+=Psmallflowlist_text
        print(code)
driver.close()

    # print(req.status_code)
dict_data = {
#股票代码
'股票代码':
code_list,
'日期':
date_list ,
# 收盘价
'收盘价':
ClosingPrice_list,
# 涨跌幅
'涨跌幅':
Quotechange_list,
# 主力净流入净额
'主力净流入净额':
Mainnetinflow_list,
# 主力净流入占比
'主力净流入占比':
Pmainnetinflow_list,
# 超大单净流入净额
'超大单净流入净额':
superlargeflow_list,
# 超大单净流入占比
    '超大单净流入占比':
Psuperlargeflow_list,
# 大单净流入净额
    '大单净流入净额':
largeflow_list,
# 大单净流入占比
    '大单净流入占比':
Plargeflow_list,
# 大单净流入净额
    '中单净流入净额':
midflow_list,
# 中单净流入占比
    '中单净流入占比':
Pmidflow_list,
# 小单净流入净额
    '小单净流入净额':
smallflow_list,
# 小单净流入净额
    '小单净流入占比':
Psmallflow_list
}
data = pd.DataFrame(dict_data)
data.to_excel('资金流向.xlsx', index=False)
#存入数据库
from sqlalchemy import create_engine
engine= create_engine('sqlite:///choice.db')

df.to_sql('Cflows', engine)

东方财富

https://gitee.com/skykaiyuan/oriental-fortune-python/tree/ff0f2711e7870d53eac2dd7a1b3617083d0cfe9a/

有疑问可以给博主留言

猜你喜欢

转载自blog.csdn.net/weixin_45631815/article/details/115003014