【问题标题】:spider scraped items from a web page but there is nothing in output蜘蛛从网页中抓取项目,但输出中没有任何内容
【发布时间】:2020-04-02 02:57:13
【问题描述】:

我正在尝试从这个web page 中抓取产品名称及其价格,我在蜘蛛之后写了这篇文章

import scrapy
from ..items import MenDataItem
class MenCollectionSpider(scrapy.Spider):
    name = 'men_collection'
    allowed_domains = ['www.exportleftovers.com']
    start_urls = ['https://www.exportleftovers.com/collections/men']

    def parse(self, response):

        items = MenDataItem()

        for product in response.xpath("//div[@class = 'product-list collection-matrix clearfix equal-columns--clear equal-columns--outside-trim']/div/div/a/div"):

            title =  product.xpath(".//a[@class='product-info__caption ']/div[@class='product-details']/span[@class = 'title']/text()").get()
            price = product.xpath(".//a[@class='product-info__caption ']/div[@class='product-details']/span[@class = 'price ']/span[@class='current_price']/span[@class='money']/text()").get()

            items['title'] = title
            items['price'] = price

            yield items

以下是items.py

import scrapy


class MenDataItem(scrapy.Item):
    # define the fields for your item here like:
    title = scrapy.Field()
    price = scrapy.Field()

它提供以下输出

2020-04-01 19:25:07 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2020-04-01 19:25:08 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.exportleftovers.com/robots.txt> (referer: None)
2020-04-01 19:25:09 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.exportleftovers.com/collections/men> (referer: None)
2020-04-01 19:25:09 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.exportleftovers.com/collections/men>
{'title': None, 'price': None}
2020-04-01 19:25:09 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.exportleftovers.com/collections/men>
{'title': None, 'price': None}
2020-04-01 19:25:09 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.exportleftovers.com/collections/men>
{'title': None, 'price': None}
2020-04-01 19:25:09 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.exportleftovers.com/collections/men>
{'title': None, 'price': None}
2020-04-01 19:25:09 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.exportleftovers.com/collections/men>
{'title': None, 'price': None}
2020-04-01 19:25:09 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.exportleftovers.com/collections/men>
{'title': None, 'price': None}
2020-04-01 19:25:09 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.exportleftovers.com/collections/men>
{'title': None, 'price': None}
2020-04-01 19:25:09 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.exportleftovers.com/collections/men>
{'title': None, 'price': None}
2020-04-01 19:25:09 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.exportleftovers.com/collections/men>
{'title': None, 'price': None}
2020-04-01 19:25:09 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.exportleftovers.com/collections/men>
{'title': None, 'price': None}
2020-04-01 19:25:09 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.exportleftovers.com/collections/men>
{'title': None, 'price': None}
2020-04-01 19:25:09 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.exportleftovers.com/collections/men>
{'title': None, 'price': None}
2020-04-01 19:25:09 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.exportleftovers.com/collections/men>
{'title': None, 'price': None}
2020-04-01 19:25:09 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.exportleftovers.com/collections/men>
{'title': None, 'price': None}
2020-04-01 19:25:09 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.exportleftovers.com/collections/men>
{'title': None, 'price': None}
2020-04-01 19:25:09 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.exportleftovers.com/collections/men>
{'title': None, 'price': None}
2020-04-01 19:25:09 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.exportleftovers.com/collections/men>
{'title': None, 'price': None}
2020-04-01 19:25:09 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.exportleftovers.com/collections/men>
{'title': None, 'price': None}
2020-04-01 19:25:09 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.exportleftovers.com/collections/men>
{'title': None, 'price': None}
2020-04-01 19:25:09 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.exportleftovers.com/collections/men>
{'title': None, 'price': None}
2020-04-01 19:25:09 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.exportleftovers.com/collections/men>
{'title': None, 'price': None}
2020-04-01 19:25:09 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.exportleftovers.com/collections/men>
{'title': None, 'price': None}
2020-04-01 19:25:09 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.exportleftovers.com/collections/men>
{'title': None, 'price': None}
2020-04-01 19:25:09 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.exportleftovers.com/collections/men>
{'title': None, 'price': None}
2020-04-01 19:25:09 [scrapy.core.engine] INFO: Closing spider (finished)
2020-04-01 19:25:09 [scrapy.extensions.feedexport] INFO: Stored csv feed (24 items) in: data.csv
2020-04-01 19:25:09 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 531,
 'downloader/request_count': 2,
 'downloader/request_method_count/GET': 2,
 'downloader/response_bytes': 184287,
 'downloader/response_count': 2,
 'downloader/response_status_count/200': 2,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2020, 4, 2, 2, 25, 9, 457430),
 'item_scraped_count': 24,
 'log_count/DEBUG': 26,
 'log_count/INFO': 10,
 'response_received_count': 2,
 'robotstxt/request_count': 1,
 'robotstxt/response_count': 1,
 'robotstxt/response_status_count/200': 1,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'start_time': datetime.datetime(2020, 4, 2, 2, 25, 7, 590785)}
2020-04-01 19:25:09 [scrapy.core.engine] INFO: Spider closed (finished)

每次都为两个变量提供“无”值时,可以在此处看到,xpath 值可以很好地搜索此页面上的 24 个项目,因为我将输出存储在 data.csv 文件中,可以在记录 24 个项目存储在 csv 文件中,输出文件中除了标题名称外没有任何内容。谁能帮忙

【问题讨论】:

  • 我相信是因为您必须将items.py 中的字典键定义为Item.Field() 对象。然后你需要将你的项目函数导入你的蜘蛛,最后将字典解析为项目函数的输入。 yield YourItemFunction(**yourDictionary)
  • 试过了,同样的输出
  • 你能发布你的items.py 并更新你的蜘蛛吗?
  • 另外,您需要检查您的 ITEMS 管道是否在您的 settings.py 中的 on
  • 我已经更新了有问题的蜘蛛,请看一下,即使在从 settings.py 文件中的 ITEM_PIPELINES 中删除 cmets 后,也没有任何变化

标签: python web-scraping scrapy web-crawler


【解决方案1】:

我相信根据您所拥有的其他方法可以做到这一点:

import scrapy
from ..items import MenDataItem
class MenCollectionSpider(scrapy.Spider):
    name = 'men_collection'
    allowed_domains = ['www.exportleftovers.com']
    start_urls = ['https://www.exportleftovers.com/collections/men']

    def parse(self, response):

        items = MenDataItem()

        titles = response.xpath("*//a[@class='product-info__caption ']/div[@class='product-details']/span[@class = 'title']/text()").getall()
        prices = response.xpath("*//a[@class='product-info__caption ']/div[@class='product-details']/span[@class = 'price ']/span[@class='current_price']/span[@class='money']/text()").getall()

        for i in range(len(titles)):
            items['title'] = titles[i]
            items['price'] = prices[i]

            yield items

我想知道这是否能解决问题。让我知道是否有! :D

【讨论】:

  • 很抱歉回复得太晚了。此代码生成TypeError: 'NoneType' object is not subscriptable 错误。我也试着寻找答案,每个人都说同样的话,但第一个答案是这个 thread 专门关于网络抓取,解释这里发生的事情是“如果你正在寻找的不是这个,你会遇到这个错误那里”。这有意义吗?
  • 确实如此,但我检查了您发送的网站并尝试了此 XPath,它们都返回了正确的结果。可能,当您使用 scrapy 连接到网站时,您会收到不同的响应。
猜你喜欢
  • 2017-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-18
  • 1970-01-01
  • 2012-01-23
  • 1970-01-01
相关资源
最近更新 更多