【问题标题】:Can't store output from Colab into Excel无法将 Colab 的输出存储到 Excel 中
【发布时间】:2021-11-22 19:45:09
【问题描述】:

我编写了下面的代码,但无法将其保存到 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)

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

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

【问题讨论】:

    标签: python excel pandas dataframe google-colaboratory


    【解决方案1】:

    在 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。

    【讨论】:

      【解决方案2】:

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

      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,它就像一个空表,使用Pandas(“一个快速、强大、灵活且易于使用的开源数据分析和操作工具,构建在Python之上编程语言”)。然后,对于每个循环,您填充 DataFrame 的一行。 最后,调用to_excel() 方法将文件存储为output.xlsx。您可以在 Google Colab 的 content 文件夹中找到它。

      【讨论】:

        猜你喜欢
        • 2019-03-10
        • 1970-01-01
        • 2022-07-29
        • 1970-01-01
        • 2014-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多