Scrapy callback doesn't work at all in my script

时间:2017-08-05 12:01:29

标签: python python-3.x web-scraping scrapy scrapy-spider

I've written a script in python scrapy to parse name and prices of different items available in a webpage. I tried to implement logic in my script the way I've learnt so far. However, when I execute it, I get the following error. I suppose I can't make the callback method work properly. Here is the script I've tried with:

The spider names "sth.py" contains:

from scrapy.contrib.spiders import CrawlSpider
from scrapy.http.request import Request

class SephoraSpider(CrawlSpider):

    name = "sephorasp"

    def start_requests(self):
        yield Request(url = "https://www.sephora.ae/en/stores/", callback = self.parse_pages)


    def parse_pages(self, response):
        for link in response.xpath('//ul[@class="nav-primary"]//a[contains(@class,"level0")]/@href').extract():
            yield Request(url = link, callback = self.parse_inner_pages)

    def parse_inner_pages(self, response):
        for links in response.xpath('//li[contains(@class,"amshopby-cat")]/a/@href').extract():
            yield Request(url = links, callback = self.target_page)

    def target_page(self, response):
        for titles in response.xpath('//div[@class="product-info"]'):
            product = titles.xpath('.//div[contains(@class,"product-name")]/a/text()').extract_first()
            rate = titles.xpath('.//span[@class="price"]/text()').extract_first()
            yield {'Name':product,'Price':rate}

"items.py" contains:

import scrapy
class SephoraItem(scrapy.Item):
    Name = scrapy.Field()
    Price = scrapy.Field()

Partial error looks like:

    if cookie.secure and request.type != "https":
AttributeError: 'WrappedRequest' object has no attribute 'type'

Here is the total error log: "https://www.dropbox.com/s/kguw8174ye6p3q9/output.log?dl=0"

1 个答案:

答案 0 :(得分:1)

当前版本为v1.4时,您似乎正在运行scrapy v1.1。据我记得,有一些关于早期1.something版本和用于处理cookie的WrappedRequest对象的错误。

尝试升级到v1.4:

pip install scrapy --upgrade