[英]Cannot export python script output into the same csv file
#!/usr/bin/python
import requests
from bs4 import BeautifulSoup
import csv
class GetFeeds(object):
def main(self):
self.malc0de()
self.malwaredomainlist()
def malc0de(self):
url=requests.get('http://malc0de.com/rss/')
feed=url.content
soup=BeautifulSoup(feed,'html.parser')
with open("feeds_123.csv", "w") as f:
writer = csv.writer(f, delimiter=";")
for link in soup.find_all('item'):
desc = link.find('description').contents
formatted_desc = desc[0].split(",")
formatted_desc_contents = [cont.split(":")[1] for cont in formatted_desc]
print formatted_desc_contents
writer.writerow(formatted_desc_contents)
def malwaredomainlist(self):
url=requests.get('http://www.malwaredomainlist.com/hostslist/mdl.xml')
feed2=url.content
soup=BeautifulSoup(feed2,'html.parser')
##print soup.prettify()
with open("feeds_123.csv", "w") as b:
writer = csv.writer(b, delimiter=";")
for link in soup.find_all('item'):
desc = link.find('description').contents
formatted_desc = desc[0].split(",")
formatted_desc_contents = [cont.split(":")[1] for cont in formatted_desc]
print formatted_desc_contents
writer.writerow(formatted_desc_contents)
if __name__ == "__main__":
o = GetFeeds()
o.main()
Currently, I am trying to export the information from both mac0de and malwaredomainlist to the same file called feeds_123.csv however, the csv file only shows malwaredomainlist items instead of showing both. 目前,我正在尝试将信息从mac0de和恶意软件域名列表导出到名为feeds_123.csv的同一文件中,但是,csv文件仅显示恶意软件域名列表项,而不同时显示两者。 I tried extracting it into 2 different files it works.
我尝试将其提取到2个不同的文件中,它可以正常工作。 May I know how can I solve this error and extract into the same file?
我可以知道如何解决此错误并提取到同一文件中吗?
You need to open the file in "a" mode instead of "w" ,"a" means append and will add new content to the previous. 您需要以“ a”模式而不是“ w”模式打开文件,“ a”表示追加并将新内容添加到上一个。 "w" will clean the file and write over whatever was there.
“ w”将清除文件并覆盖其中的所有内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.