我刚开始使用Python进行抓取。在使用了大量有用的资源之后,我能够抓取页面的内容。但是,我无法将这些数据保存到.csv文件中。
Python:
import mechanize
import time
import requests
import csv
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Firefox(executable_path=r'C:\Users\geckodriver.exe')
driver.get("myUrl.jsp")
username = driver.find_element_by_name('USER')
password = driver.find_element_by_name('PASSWORD')
username.send_keys("U")
password.send_keys("P")
main_frame = driver.find_element_by_xpath('//*[@id="Frame"]')
src = driver.switch_to_frame(main_frame)
table = driver.find_element_by_xpath("/html/body/div/div[2]/div[5]/form/div[7]/div[3]/table")
rows = table.find_elements(By.TAG_NAME, "tr")
for tr in rows:
outfile = open("C:/Users/Scripts/myfile.csv", "w")
with outfile:
writers = csv.writer(outfile)
writers.writerows(tr.text)问题:
只有一行被写入excel文件。但是,当我将tr.text打印到控制台时,所有必需的行都会显示出来。如何将tr元素中的所有文本写入excel文件?
发布于 2019-03-12 15:22:15
当前,您的代码将打开文件,编写一行,关闭它,然后在下一行上再次打开它并覆盖该行。请考虑以下代码片段:
# We use 'with' to open the file and auto close it when done
# syntax is best modified as follows
with open('C:/Users/Scripts/myfile.csv', 'w') as outfile:
writers = csv.writer(outfile)
# we only need to open the file once so we open it first
# then loop through each row to print everything into the open file
for tr in rows:
writers.writerows(tr.text)https://stackoverflow.com/questions/55124722
复制相似问题