[英]Not saving lines when writing to CSV file using for-loop
所以我正在尝试运行一个网站。 刮刀运行得很好。 但是,每当我尝试将抓取的信息/行写入 csv 文件时,它都会删除前一行。 我最终只是在最后的文件中获得了最后的刮擦结果。 我确定这只是一个缩进错误? 我还是 Python 的新手,所以任何帮助将不胜感激!
代码:
# create general field names
fields = ['name', 'about', 'job_title', 'location','company',
'education','accomplishments','linkedin_url']
with open('ScrapeResults.csv', 'w') as f:
# using csv.writer method from CSV package
write = csv.writer(f)
write.writerow(fields)
f.close()
# Loop-through urls to scrape multiple pages at once
for individual,link in contact_dict.items():
## assign ##
the_name = individual
the_link = link
# scrape peoples url:
person = Person(the_link, driver=driver, close_on_complete=False)
# rows to be written
rows = [[person.name, person.about, person.job_title, person.location, person.company,
person.educations, person.accomplishments, person.linkedin_url]]
# write
with open('ScrapeResults.csv', 'w') as f:
# using csv.writer method from CSV package
write = csv.writer(f)
write.writerows(rows)
f.close()
您需要在append模式下打开文件。
改变
with open('ScrapeResults.csv', 'w') as f:
至
with open('ScrapeResults.csv', 'a') as f:
我认为您正在覆盖文件而不是附加它。 for 循环中的 Append 模式应该可以解决问题
with open('ScrapeResults.csv', "a")
请参考 python IO 文档: https://docs.Z23EEEB4347BDD26BFC6B7EE3/tutorial-filesoutput.html#Z-and-tutorial-filesoutput.html#Z.org
您必须以append 模式打开文件。
代替
with open('ScrapeResults.csv', 'w') as f:
至
with open('ScrapeResults.csv', 'a') as f:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.