从selenium到excel

从selenium到excel,excel,selenium,beautifulsoup,Excel,Selenium,Beautifulsoup,下面的代码使用selenium刮取一些数据并将结果打印到屏幕上。有人知道我如何将这些数据保存到excel文件中,甚至更好地保存到现有文件中的工作表中吗?谢谢 from time import sleep from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.web

下面的代码使用selenium刮取一些数据并将结果打印到屏幕上。有人知道我如何将这些数据保存到excel文件中,甚至更好地保存到现有文件中的工作表中吗?谢谢

from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

url = 'http://www.tradingview.com/screener'
driver = webdriver.Firefox()
driver.get(url)

try:

    selector = '.js-field-total.tv-screener-table__field-value--total'
    condition = EC.visibility_of_element_located((By.CSS_SELECTOR, selector))
    matches = WebDriverWait(driver, 10).until(condition)
    matches = int(matches.text.split()[0])

except (TimeoutException, Exception):
    print ('Problem finding matches, setting default...')
    matches = 4895 # Set default

# The page loads 150 rows at a time; divide matches by
# 150 to determine the number of times we need to scroll;
# add 5 extra scrolls just to be sure
num_loops = int(matches / 150 + 5)

for _ in range(num_loops):

    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    sleep(2) # Pause briefly to allow loading time

# will give a list of all tickers
tickers = driver.find_elements_by_css_selector('a.tv-screener__symbol') 

# will give a list of all EMPs
emps = driver.find_elements_by_xpath('//tbody/tr/td[10]')

# will give a list of all sectors
sectors = driver.find_elements_by_xpath('//tbody/tr/td[11]')

for index in range(len(tickers)):
   print("Row " + tickers[index].text + " " + emps[index].text + " " + sectors[index].text + " ")

您需要使用外部api/框架。对于java,我使用它将selenium数据保存到excel


我还没有用python来做这件事,但是有一些很好的资源。

您给了我们一些代码,这很好,但据我所知,这与您遇到的问题无关,即您希望将代码生成的数据写入excel电子表格。你试图用什么方法来解决这个问题?甚至不知道哪种方法是最好的,这都是非常令人困惑的。你推荐什么?我在Anaconda上安装了openpyxl。我一直在试图了解如何使用它,但它真的很残忍。有人能帮我开始吗?