将python脚本的结果写入CSV

时间:2018-04-26 08:08:53

标签: python csv web-scraping

我正在使用我在Github上发现的亚马逊评论刮刀:https://github.com/philipperemy/amazon-reviews-scraper

        for review in reviews_list:
             rating = review.find(attrs={'data-hook': 'review-star-rating'}).attrs['class'][2].split('-')[-1]
             body = review.find(attrs={'data-hook': 'review-body'}).text
             date = review.find(attrs={'data-hook': 'review-date'}).text
             title = review.find(attrs={'data-hook': 'review-title'}).text

             logging.info('***********************************************')
             logging.info(title)
             logging.info(date)
             logging.info(rating)
             logging.info(body)
             logging.info('***********************************************\n')
             reviews.append({'title': title,
                             'rating': rating,
                             'body': body,
                             'product_id': product_id
                            })

              return reviews

我希望将这些结果发送到CSV文件。但我只是Python的初学者,所以我不完全确定。我在Windows上使用Python。

在Akshat的帮助下我得到了这个,但我不确定它是否正确:

        for review in reviews_list:
            rating = review.find(attrs={'data-hook': 'review-star-rating'}).attrs['class'][2].split('-')[-1]
            body = review.find(attrs={'data-hook': 'review-body'}).text
            date = review.find(attrs={'data-hook': 'review-date'}).text
            title = review.find(attrs={'data-hook': 'review-title'}).text

from csv import DictWriter

with open('results.csv', 'w') as csvfile:
    fieldnames = ['title', 'date', 'rating', 'body']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()

    for review in reviews: 
        writer.writerow(review)

2 个答案:

答案 0 :(得分:0)

你的问题在这里:

 List<int> imageBytes = await widget.fileData.readAsBytes();

writer.writerow(title, date, rating, body) 需要一个序列。这样做:

writerow

该行需要在 writer.writerow([title, date, rating, body]) 的循环内。

答案 1 :(得分:0)

@BoarGules指出的错误外。我建议您从CSV模块

中探索DictWriter
from csv import DictWriter

with open('results.csv', 'w') as csvfile:
    fieldnames = ['title', 'date', 'rating', 'body']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()

    for review in reviews: 
        writer.writerow(review)