【问题标题】:I need to export this data to excel [duplicate]我需要将此数据导出到 excel [重复]
【发布时间】:2021-07-13 19:18:39
【问题描述】:

我在 python 中使用 selenium 获取了这些数据,我想将其导出到 excel。

我想我可以使用 pandas,但我不知道该怎么做。感谢您的帮助。

这是我的代码:

import random
from time import sleep
from selenium import webdriver


driver = webdriver.Chrome('./chromedriver.exe')

driver.get('https://es.investing.com/crypto/bitcoin/btc-usd-historical-data?cid=1035793')

boton = driver.find_element_by_xpath('//button[@id="onetrust-accept-btn-handler"]')
boton.click()

sleep(random.uniform(5.0, 10.0))

lista = driver.find_element_by_xpath('//*[@id="data_interval"]/option[3]')
lista.click()

sleep(random.uniform(10.0, 20.0))

registro = driver.find_element_by_xpath('//*[@id="PromoteSignUpPopUp"]/div[2]/i')
registro.click()

sleep(random.uniform(8.0, 10.0))

tablas = driver.find_elements_by_xpath('//*[@id="curr_table"]')

rows = len(driver.find_elements_by_xpath('//*[@id="curr_table"]/tbody/tr'))
cols = len(driver.find_elements_by_xpath('//*[@id="curr_table"]/tbody/tr[1]/td'))

print(rows)
print(cols)

for n in range(2, rows+1):
    for b in range(1, cols-4):
        fec_pre = driver.find_element_by_xpath('//*[@id="curr_table"]/tbody/tr['+str(n)+']/td['+str(b)+']').text
        print(fec_pre, end='                                                                        ')
    print()

【问题讨论】:

  • 可以写成 CSV,然后在 excel 中打开

标签: python excel pandas selenium


【解决方案1】:

假设您的数据是一个数组,使用 Pandas 导出的一般语法如下:

import pandas as pd
df=pd.DataFrame(data=your_data,index=list_of_index,columns=list_of_columns)
df.to_excel(filename)

您可以参考文档了解更多详情:
pd.DataFrame
to_excel function

【讨论】:

    猜你喜欢
    • 2012-09-14
    • 2020-05-15
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多