为什么在运行pider时出现错误?

时间:2020-02-18 20:22:47

标签: python csv scrapy

我目前正在练习,将特定产品的亚马逊评论放入csv文件中。我已经整理了代码以提取数据,但是当我进入runspider放入csv时遇到语法错误。我直接从正在查看的练习模块中复制了这一部分,所以我不太确定问题可能是什么。我在runspider上发现的所有资源都表明代码应该正确,但显然我在这里做错了。

这是我的代码。在最后一行出现错误:

import scrapy

# Implementing Spider
class ReviewspiderSpider(scrapy.Spider):

    # Name of Spider
    name = 'reviewspider'

    allowed_domains = ["amazon.com"]
    start_urls = ['https://www.amazon.com/product-reviews/B07N49F51N/ref=cm_cr_arp_d_viewpnt_lft?pageNumber=']


    def parse(self, response):
        names = response.xpath('//span[@class="a-profile-name"]/text()').extract()
        reviewTitles = response.xpath('//a[@data-hook="review-title"]/span/text()').extract()
        starRatings = response.xpath('//span[@class="a-icon-alt"]/text()').extract()
        reviews = response.xpath('//span[@data-hook="review-body"]/span/text()').extract()
        noOfComments = response.xpath('//span[@class="a-size-base"]/text()').extract()
        for (name, title, rating, review, comments) in zip(names, reviewTitles, starRatings, reviews, noOfComments):
            yield {'Name': name, 'Title': title, 'Rating': rating, 'Review': review, 'No of Comments': comments }

scrapy runspider spiders/reviewspider.py -t csv -o - > amazonreviews.csv

这是错误消息:

  File "<ipython-input-35-6e8796e727d9>", line 22
    scrapy runspider <reviewspider.py> -t csv -o - > amazonreviews.csv
                   ^
SyntaxError: invalid syntax

我在这里想念什么?我是Python的新手,网络爬虫和抓痒,因此任何故障/洞察都是有用的。

1 个答案:

答案 0 :(得分:0)

scrapy runspider spiders/reviewspider.py -t csv -o - > amazonreviews.csv

不属于您的代码。只是命令如何运行蜘蛛。

通过cmd或anaconda提示符转到您的项目位置。并尝试

scrapy runspider reviewspider.py -t csv -o amazonreviews.csv