python - Scrapy 管道以正确的格式导出 csv 文件

标签 python csv scrapy pipeline

我根据下面alexce的建议做了改进。我需要的是如下图。但是,每一行/行应该是一条评论:包含日期、评级、评论文本和链接。

我需要让项目处理器处理每个页面的每个评论。
目前 TakeFirst() 只接受页面的第一次审查。所以 10 页,我只有 10 行/行,如下图所示。

enter image description here

爬虫代码如下:

import scrapy
from amazon.items import AmazonItem

class AmazonSpider(scrapy.Spider):
   name = "amazon"
   allowed_domains = ['amazon.co.uk']
   start_urls = [
    'http://www.amazon.co.uk/product-reviews/B0042EU3A2/'.format(page) for      page in xrange(1,114)

]

def parse(self, response):
    for sel in response.xpath('//*[@id="productReviews"]//tr/td[1]'):
        item = AmazonItem()
        item['rating'] = sel.xpath('div/div[2]/span[1]/span/@title').extract()
        item['date'] = sel.xpath('div/div[2]/span[2]/nobr/text()').extract()
        item['review'] = sel.xpath('div/div[6]/text()').extract()
        item['link'] = sel.xpath('div/div[7]/div[2]/div/div[1]/span[3]/a/@href').extract()

        yield item

最佳答案

我从头开始,下面的蜘蛛应该运行

scrapy crawl amazon -t csv -o Amazon.csv --loglevel=INFO

以便为我显示用电子表格打开 CSV 文件

enter image description here

希望这有帮助:-)

import scrapy

class AmazonItem(scrapy.Item):
    rating = scrapy.Field()
    date = scrapy.Field()
    review = scrapy.Field()
    link = scrapy.Field()

class AmazonSpider(scrapy.Spider):

    name = "amazon"
    allowed_domains = ['amazon.co.uk']
    start_urls = ['http://www.amazon.co.uk/product-reviews/B0042EU3A2/' ]

    def parse(self, response):

        for sel in response.xpath('//table[@id="productReviews"]//tr/td/div'):

            item = AmazonItem()
            item['rating'] = sel.xpath('./div/span/span/span/text()').extract()
            item['date'] = sel.xpath('./div/span/nobr/text()').extract()
            item['review'] = sel.xpath('./div[@class="reviewText"]/text()').extract()
            item['link'] = sel.xpath('.//a[contains(.,"Permalink")]/@href').extract()
            yield item

        xpath_Next_Page = './/table[@id="productReviews"]/following::*//span[@class="paging"]/a[contains(.,"Next")]/@href'
        if response.xpath(xpath_Next_Page):
            url_Next_Page = response.xpath(xpath_Next_Page).extract()[0]
            request = scrapy.Request(url_Next_Page, callback=self.parse)
            yield request

关于python - Scrapy 管道以正确的格式导出 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29943075/

相关文章:

Python 磁盘镜像

python - 正则表达式提取数字维度

php - 使用 MySQL,当我从数据库中检索哈希用户密码时,结果为 1。

sql - PostgreSQL 删除等于以逗号分隔的字符串的行

python - 如何在Scrapy spider中获取pipeline对象

python - Scrapy 登录适用于某些网站,但不适用于其他网站

python - Eigen + MKL 或 OpenBLAS 比 Numpy/Scipy + OpenBLAS 慢

python - 编写一个装饰器,为一个类的所有方法应用另一个带有参数的装饰器

python - 从经过训练的 keras 模型中获取训练超参数

javascript - 无法使用 Scrapy 从下拉列表中抓取