使用漂亮的汤更新已经存在的csv的数据

时间:2012-12-26 07:41:48

标签: csv python-2.7 screen-scraping beautifulsoup

我想将网页中的抓取数据更新为已经存在的csv,这是我第一次运行抓取代码时生成的。 我希望新数据附加到已经删除的行下面。

下面是我用来刮取数据并编写csv的代码:

import csv
import urllib2
import sys
import time
from bs4 import BeautifulSoup
from itertools import islice
page = urllib2.urlopen('http://shop.ee.co.uk/mobile-phones/pay-monthly/').read()
soup = BeautifulSoup(page)
soup.prettify()
with open('EE_AppendTesting.csv', 'a') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=',')
    #spamwriter.writerow(["Date","Month","Day of Week","Device Name","Price","Plan Monthly Price"])    
    items = soup.findAll('h2', {"class":"heading2 no-margin-top"})
    prices = soup.findAll('span', {"class": "price"})
    monthly = soup.findAll('div',{"class":"mrc-holder"})
    for item, price, monthly1 in zip(items, prices, monthly):
        textcontent = u' '.join(islice(monthly1.stripped_strings, 2, 3, 1))
        if textcontent:
            spamwriter.writerow([time.strftime("%Y-%m-%d"),time.strftime("%B"),time.strftime("%A") ,unicode(item.string).encode('utf8').strip(),unicode(price.string).encode('utf8').replace('£','£').strip(),unicode(textcontent).encode('utf8').replace('£','£').strip()])

现在,每次运行代码时,新数据都会在每个数据条目之间附加,空行。以下是附加的csv示例:

26-12-2012  December    Wednesday   Nokia Lumia 920 White   £ 19.99 £46.00
26-12-2012  December    Wednesday   iPhone 5 64GB Black £ 139.99    £56.00

26-12-2012  December    Wednesday   Nokia Lumia 820 FREE    £36.00

26-12-2012  December    Wednesday   iPhone 5 16GB White £ 19.99 £56.00

1 个答案:

答案 0 :(得分:1)

我在代码中使用了“lineterminator ='\ n'”方法来解决上述问题。 更新后的代码如下:

with open('EE_AppendTesting.csv', 'a') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=',',lineterminator='\n')