简体   繁体   English

Python。 在网络上收集数据后如何导出到 csv 文件

[英]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).我用 selenium 进行了一些网页抓取,并且能够生成一个带有我需要导出的数据的表格(见下图)。 How can now export it to csv (place a .csv in a specific folder with todays date).现在如何将其导出到 csv(将 .csv 放在具有今天日期的特定文件夹中)。 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从技术上讲,只要你有它,就在表的每一行上调用 f.write 方法,但将 'w' 参数更改为 'a' 以附加到文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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