首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将数据从网页保存到excel文件中存在问题

将数据从网页保存到excel文件中存在问题
EN

Stack Overflow用户
提问于 2019-03-12 15:05:22
回答 1查看 184关注 0票数 0

我刚开始使用Python进行抓取。在使用了大量有用的资源之后,我能够抓取页面的内容。但是,我无法将这些数据保存到.csv文件中。

Python:

代码语言:javascript
复制
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文件?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-12 15:22:15

当前,您的代码将打开文件,编写一行,关闭它,然后在下一行上再次打开它并覆盖该行。请考虑以下代码片段:

代码语言:javascript
复制
# 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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55124722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档