简体   繁体   中英

python. how to export to csv file after gather data on web

I did some web scraping with selenium and was able to generate to print a table with the data I need to export (see image below). How can now export it to csv (place a .csv in a specific folder with todays date). Thanks a lot for your inputs.

from selenium import webdriver
from selenium.webdriver.support.ui import Select



path_to_chromedriver = 'C:\python34\chromedriver\chromedriver.exe' # change path as needed
browser = webdriver.Chrome(executable_path = path_to_chromedriver)
url = 'http://test.com'
browser.get(url)

browser.find_element_by_xpath("//select[@name='query_choice']/option[text()='60 days']").click() # working to select DLP

browser.find_element_by_css_selector('input[type=\"submit\"]').click() # working to press submit

for tag in browser.find_elements_by_xpath('//*[@id="page1"]/table'):
    print (tag.text)

在此处输入图片说明

Assuming your table is a list

with open('saveTo\Directory\file.csv','w') as f:
    f.write(','.join(tableData))

Where you can technically, call the f.write method on each line of the table as soon as you have it, but change the 'w' parameter to 'a' to append to the file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM