1

我想将网页中的抓取数据更新为已经存在的 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
4

1 回答 1

1

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

with open('EE_AppendTesting.csv', 'a') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=',',lineterminator='\n')
于 2012-12-26T08:38:05.817 回答