【发布时间】: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