无法将 Colab 的输出存储到 Excel

Can't store output from Colab into Excel

我写了下面的代码,但无法将其保存到 Excel。

!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"]

for list in lists:
  url = "https://finance.yahoo.com/quote/{list}?p={list}"
  wd.get(url.format(list=list))
  EPS = wd.find_element_by_xpath('//*[@id="quote-summary"]/div[2]/table/tbody/tr[4]/td[2]/span').text
  AV = wd.find_element_by_xpath('//*[@id="quote-summary"]/div[1]/table/tbody/tr[8]/td[2]/span').text
  OYT = wd.find_element_by_xpath('//*[@id="quote-summary"]/div[2]/table/tbody/tr[8]/td[2]/span').text
  print(list,EPS,AV,OYT)

它会输出下面的table。但是在那之后,我不能把下面变成Excel。我尝试了很多方法,但仍然失败。我该如何解决这个问题?

FBRX -1.6060 2,031,998 3.25
GNLN -1.0530 827,585 5.40
TISI -2.4640 545,536 10.00

这应该可以完成工作,替换您发布的代码的第二部分:

lists = ["FBRX", "GNLN", "TISI"]

import pandas as pd # Import Pandas
df = pd.DataFrame(columns=range(len(lists)), index=lists) # Create empty DataFrame

for i, list in enumerate(lists):
  url = "https://finance.yahoo.com/quote/{list}?p={list}"
  wd.get(url.format(list=list))
  EPS = wd.find_element_by_xpath('//*[@id="quote-summary"]/div[2]/table/tbody/tr[4]/td[2]/span').text
  AV = wd.find_element_by_xpath('//*[@id="quote-summary"]/div[1]/table/tbody/tr[8]/td[2]/span').text
  OYT = wd.find_element_by_xpath('//*[@id="quote-summary"]/div[2]/table/tbody/tr[8]/td[2]/span').text
  print(list,EPS,AV,OYT)
  df[i][0]=EPS # Fill line i-th, column 0
  df[i][1]=AV # Fill line i-th, column 1
  df[i][2]=OYT # Fill line i-th, column 2

df.to_excel("output.xlsx") # Save to excel file

基本上,您创建一个 DataFrame, which is like an empty table, by using Pandas(“一种快速、强大、灵活且易于使用的开源数据分析和操作工具,构建在 Python 编程语言之上”)。然后,对于每个循环,您填充一行 DataFrame。 最后,调用 to_excel() 方法将文件存储为 output.xlsx。您可以在 Google Colab 的 content 文件夹中找到它。

在 for 循环中构建结果列表,使用 Pandas 制作数据框,并由此创建电子表格。

lists = ["FBRX", "GNLN", "TISI"]
result=[]  # empty list to start
for list in lists:
  url = f"https://finance.yahoo.com/quote/{list}?p={list}" # use an f string to format
  wd.get(url.format(list=list))
  EPS = wd.find_element_by_xpath('//*[@id="quote-summary"]/div[2]/table/tbody/tr[4]/td[2]/span').text
  AV = wd.find_element_by_xpath('//*[@id="quote-summary"]/div[1]/table/tbody/tr[8]/td[2]/span').text
  OYT = wd.find_element_by_xpath('//*[@id="quote-summary"]/div[2]/table/tbody/tr[8]/td[2]/span').text
  print(list,EPS,AV,OYT)
  result.append([list,EPS,AV,OYT])   # add the row to the results

result

#[['FBRX', '-1.6060', '2,031,998', '3.25'],
# ['GNLN', '-1.0530', '827,585', '5.40'],
# ['TISI', '-2.4640', '545,536', '10.00']]

import pandas as pd
df = pd.DataFrame(result, columns=['List','EPS','AV','OYT'])
df.to_excel('result.xlsx')

请注意,我必须让 url 代使用 f 字符串才能使 url 正确。