跳过错误,在从Pandas获取价格数据的循环中获得3个错误后继续运行

2024-06-16 09:48:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个循环来迭代函数。这个功能只是通过一系列的股票代码从雅虎财经获取数据。然而,有些股票没有数据在雅虎财务,有时有错误,所以我需要重新运行的功能,每当我得到这个错误。你知道吗

基本上,重新运行可以解决这个bug,但是如果数据库中没有数据,就无能为力了。所以,我想使用一个循环来定义如果有错误,那么重新运行,但是如果错误出现了3次,那么跳过该代码。你知道吗

我想我做了一些错误的循环,它没有通过,并继续运行,即使它已经得到错误的股票超过3倍。我能知道怎么解决吗?你知道吗

谢谢!你知道吗

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pickle
import datetime as dt
import os
import pandas as pd
import pandas_datareader.data as web

def save_hsci_tickers():
    driver = webdriver.Chrome(r"C:/Users/kman/Downloads/chromedriver_win32/chromedriver.exe")
    wait = WebDriverWait(driver, 10)
    driver.get("https://www.hsi.com.hk/HSI-Net/HSI-Net?cmd=tab&pageId=en.indexes.hscis.hsci.constituents&expire=false&lang=en&tabs.current=en.indexes.hscis.hsci.overview_des%5Een.indexes.hscis.hsci.constituents&retry=false")
    tickers = []
    for name in wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "table.greygeneraltxt td.greygeneraltxt,td.lightbluebg"))):
        data = str(name.get_attribute('textContent'))
        tickers.append(data)
    edit = [x for x in tickers if x != '']
    final = edit[::2]
    driver.quit()

    def yahoo_ticker(data):
        if len(data) <= 4:
            return data.zfill(4) + '.HK'
        else:
            return data[0:] + '.HK'
    yahoo_tickers = [yahoo_ticker(data) for data in final]
    with open("hscitickers.pickle","wb") as f:
        pickle.dump(yahoo_tickers, f)

    print(yahoo_tickers)
    return yahoo_tickers

save_hsci_tickers()

def get_data_from_yahoo (reload_hscitickers=False):
    if reload_hscitickers:
        tickers = save_hsci_tickers()
    else:
        with open("hscitickers.pickle","rb") as f:
            tickers = pickle.load(f)

    if not os.path.exists('stock_dfs'):
        os.makedirs('stock_dfs')

    start = dt.datetime(2009,6,30)
    end = dt.datetime(2017,6,30)

    for ticker in tickers:
        print(ticker)
        if not os.path.exists('stock_dfs/{}.csv'.format(ticker)):
            df =web.DataReader(ticker,'yahoo',start,end)
            df.to_csv('stock_dfs/{}.csv'.format(ticker))
        else:
            print('Already have {}'.format(ticker))

attempts = 0
while True:
    try:
        get_data_from_yahoo()
    except:
        if attempts < 3:
            attempts += 1
            continue
        if attempts >= 3:
            pass
    else:
        break

Tags: fromimportdataifosasdriverselenium