无法从雅虎财经中提取 table

Can't extract table from yahoo finance

我尝试 运行 下面的代码,但仍然无法在 Excel 和 headers 中获得正确的输出。请帮忙。

!pip install selenium
!apt-get update # to update ubuntu to correctly run apt install
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
import sys
sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
wd = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
lists = ["FBRX", "GNLN", "TISI"]

result=[] 

for list in lists:
  url = "https://finance.yahoo.com/quote/{list}/profile?p={list}"
  wd.get(url.format(list=list))
  add = wd.find_element_by_xpath('//*[@id="Col1-0-Profile-Proxy"]/section/section[1]/table/tbody').text

  print(list,add)
  result.append([list,add])

预期的格式将在下图中。

如果 Excel 电子表格不是强制性的,那么您的“结果”变量可以保存为 txt 文件,在行和列方面采用相同的格式,方法是在之后包含以下代码行:

import numpy as np
import pandas as pd
# Mout Drive;
from google.colab import drive
myGoogleDrive = drive.mount('/content/drive', force_remount = True)
%cd "/content/drive/My Drive/Colab Notebooks"
fileName = 'InsertNameForFile' + '.txt'
np.savetxt(fileName, result, delimiter = ", ", fmt = '%s')

稍后阅读保存的 txt 文件:

savedResultsFile = pd.read_csv(fileName, header = None).copy()