【发布时间】: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