如何将两个CSV文件合并为一个?

时间:2018-07-19 14:11:10

标签: python csv beautifulsoup screen-scraping

这是我的代码,用于将数据保存到文件“ index1.csv”。

我想将产品联系人和类别抓取到一个CSV文件中。我已经在BeautifulSoup中编写了代码,但它制作了单独的文件。我不知道如何将其制成单个CSV文件。

from bs4 import BeautifulSoup
import requests
import csv

source = requests.get('https://dir.dummysite.com/search.mp?ss=engines').text    
soup = BeautifulSoup(source, 'lxml')    

with open('index1.csv', 'w') as csv_file:
    writer = csv.writer(csv_file)
    for soup in soup.find_all('div',class_='nes'):
        articles = soup.span.text,soup.div.text
        writer.writerow(articles)

代码段2将数据保存到index2.csv

from bs4 import BeautifulSoup
import requests
import csv

source = requests.get('https://dir.dummysite.com/search.mp?ss=%engines').text    
soup = BeautifulSoup(source, 'lxml')    

with open('index2.csv', 'w') as csv_file:
    for soup in soup.find_all('div', class_ = 'clz'):
        writer = csv.writer(csv_file)
        articles = soup.p
        for every in articles.find_all('a', class_= 'pnm ldf cur'):
            articles = every.text
            writer.writerow([articles])

现在,我想将这两个CSV文件合并为一个。关于我该怎么做的任何想法?

0 个答案:

没有答案