首页 > 解决方案 > 无法将 python 脚本输出导出到同一个 csv 文件中

问题描述

#!/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()

目前,我正在尝试将信息从 mac0de 和malwaredomainlist 导出到同一个名为 feeds_123.csv 的文件中,但是 csv 文件只显示malwaredomainlist 项目,而不是同时显示两者。我尝试将其提取到 2 个不同的文件中。我可以知道如何解决此错误并提取到同一个文件中吗?

标签: pythonweb-scrapingbeautifulsoupexport-to-csv

解决方案


您需要以“a”模式打开文件,而不是“w”,“a”表示追加,并将新内容添加到以前的内容。“w” 将清理文件并覆盖那里的任何内容。


推荐阅读