无法从Yahoo finance抓取数据

2024-05-29 02:58:12 发布

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

我正在从雅虎财经频道抓取数据。我搜索了以下链接:

https://query1.finance.yahoo.com/v7/finance/download/BVH?period1=923729900&period2=1618039708&interval=1d&events=history&includeAdjustedClose=true

def createLink(symbol,table):
    s = "https://query1.finance.yahoo.com/v7/finance/download/BVH?period1=923729900&period2=1618039708&interval=1d&events=history&includeAdjustedClose=true"
    return s.replace("BVH",symbol).replace("history",table)

def getData(symbol,table):
    URL = createLink(symbol,table)
    web = requests.get(URL)
    if web.status_code == 200:
      reader = pd.read_csv(URL)
    else:
      reader = pd.DataFrame({"Data":[],"Dividends":[],"Stock Splits":[]})
    return reader
def history(symbol):
    history_close = getData(symbol,'history')
    if history_close.empty:
      return history_close
    divend = getData(symbol,'div')
    stock = getData(symbol,'split')
    x = pd.merge(divend,stock, how="outer", on="Date")
    data = pd.merge(history_close,x, how="outer", on="Date")    
    return data
df = pd.read_excel("/content/drive/MyDrive/Colab Notebooks/symbolNYSE.xlsx")
count = 0
count_fail = 0

for i in range(0,len(df["Symbol"])):
    try:
      count += 1
      print(df["Symbol"][i],count)
      a = history(df["Symbol"][i])
      if not a.empty:
        a.to_excel("/content/drive/MyDrive/ColabNotebooks/GetCloseYahoo/"+df["Symbol"][i]+".xlsx")
    except:
      count_fail+=1
      pass


print("success:", count)
print("fail:", count_fail)

我正在使用python、request和Jupiter上的熊猫来爬行它。 错误:

  • 标记数据时出错。C错误:saw 12第3行中应有2个字段
  • 关键错误

开始,我可以爬大约100-200个公司。然后,该程序将错误的任何符号公司。最后,我等一分钟,我可以运行重复它,程序没有错误

原因是什么?非常感谢你


Tags: dfclosereturndefcount错误tablesymbol

热门问题