【问题标题】:Scrapy neither shows any error nor fetches any dataScrapy 既不显示任何错误也不获取任何数据
【发布时间】:2017-04-18 22:53:30
【问题描述】:

尝试使用 scrapy 从网站解析产品名称和价格。但是,当我运行我的scrapy代码时,它既不显示任何错误也不获取任何数据。我做错了什么超出了我的能力范围。希望有人来看看。

“items.py”包括:

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

名为“sephorasp.py”的蜘蛛文件包含:

from scrapy.contrib.spiders import CrawlSpider, Rule 
from scrapy.linkextractors import LinkExtractor

class SephoraspSpider(CrawlSpider):
    name = "sephorasp"
    allowed_domains = ['sephora.ae']
    start_urls = ["https://www.sephora.ae/en/stores/"]
    rules = [
            Rule(LinkExtractor(restrict_xpaths='//li[@class="level0 nav-1 active first touch-dd  parent"]')),
            Rule(LinkExtractor(restrict_xpaths='//li[@class="level2 nav-1-1-1 active first"]'),
            callback="parse_item")
    ]

    def parse_item(self, response):
        page = response.xpath('//div[@class="product-info"]')
        for titles in page:
            Product = titles.xpath('.//a[@title]/text()').extract()
            Rate = titles.xpath('.//span[@class="price"]/text()').extract()
            yield {'Name':Product,'Price':Rate}

这是日志的链接: "https://www.dropbox.com/s/8xktgh7lvj4uhbh/output.log?dl=0"

当我使用 BaseSpider 时它可以工作:

from scrapy.spider import BaseSpider
from scrapy.http.request import Request

class SephoraspSpider(BaseSpider):
    name = "sephorasp"
    allowed_domains = ['sephora.ae']
    start_urls = [
                    "https://www.sephora.ae/en/travel-size/make-up",
                    "https://www.sephora.ae/en/perfume/women-perfume",
                    "https://www.sephora.ae/en/makeup/eye/eyeshadow",
                    "https://www.sephora.ae/en/skincare/moisturizers",
                    "https://www.sephora.ae/en/gifts/palettes"

    ]

    def pro(self, response):
        item_links = response.xpath('//a[contains(@class,"level0")]/@href').extract()
        for a in item_links:
            yield Request(a, callback = self.end)

    def end(self, response):
        item_link = response.xpath('//a[@class="level2"]/@href').extract()
        for b in item_link:
            yield Request(b, callback = self.parse)

    def parse(self, response):
        page = response.xpath('//div[@class="product-info"]')
        for titles in page:
            Product= titles.xpath('.//a[@title]/text()').extract()
            Rate= titles.xpath('.//span[@class="price"]/text()').extract()
            yield {'Name':Product,'Price':Rate}

【问题讨论】:

  • 你能发布爬取日志吗?您可以通过scrapy crawl spider -s LOG_FILE=output.logscrapy crawl spider &> output.log 命令执行此操作。
  • 感谢 Granitosaurus 先生的回复。我已经添加了您要查找的内容。不过,我无法以可搜索的格式上传。

标签: python web-scraping scrapy


【解决方案1】:

您的 xpath 存在严重缺陷。

Rule(LinkExtractor(restrict_xpaths='//li[@class="level0 nav-1 active first touch-dd  parent"]')),
Rule(LinkExtractor(restrict_xpaths='//li[@class="level2 nav-1-1-1 active first"]'),

您正在匹配可以随时更改的整个班级范围,并且顺序可能在 scrapy 中有所不同。只需选择一个类,它很可能足够独特:

Rule(LinkExtractor(restrict_xpaths='//li[contains(@class,"level0")]')),
Rule(LinkExtractor(restrict_xpaths='//li[contains(@class,"level2")]')),

【讨论】:

  • 尊敬的 Granitosaurus 先生,我使用您的修正 xpath 运行了我的代码,但在我上传的控制台中出现了新的错误。您可以在上面看到的图像是更新后的图像。谢谢。
  • 尊敬的 Granitosaurus 先生,我已按照您的建议捕获了日志,但无法上传。它超大。你能告诉我我该怎么做吗?
  • @SMth80 使用某种 pastebin,例如codepad.org 上传您的日志并分享链接。
  • 好的,先生,我会这样做。顺便说一句,如果我使用 Basespider 编写代码,那么它或多或少地工作。已经更新了上面的工作。不过,我想在不提供任何 url 的情况下进行解析。
猜你喜欢
  • 1970-01-01
  • 2021-08-17
  • 1970-01-01
  • 1970-01-01
  • 2016-07-04
  • 2012-12-22
  • 2016-06-28
  • 2014-01-31
  • 2016-11-21
相关资源
最近更新 更多