我是新手, 我试图从丝芙兰那里获得产品信息。 理想情况下,尽管我在第一阶段遇到困难,但我还是希望通过所有护肤。 我试图弄清楚为什么它没有收集任何信息? 分别在scrapy shell中的响应参数为我提供了我需要的数据。
# -*- coding: utf-8 -*-
import scrapy
class sephora_try_mousturizer(scrapy.Spider):
name = 'sephora_m'
allowed_domains = ['www.sephora.com']
start_urls = ['https://www.sephora.com/product/the-true-cream-aqua-bomb-P394639?icid2=top-rated:p394639:product//']
def parse(self, response):
brand = response.css(".css-15zphjk::text").extract()
title = response.css('css-r4ddnb::text').extract()
print(title)
price = response.css(".css-n8yjg7::text").extract()
ingridinets = response.css(".css-192qj50::text").extract()
for item in zip(brand,title,price,ingridinets):
scraped_info = {
'brand' : item[0],
'title' : item[1],
'price' : item[2],
'ingridinets' : item[3],
}
#print (scraped_info)
#yield or give the scraped info to scrapy
yield scraped_info
我得到的答复如下。
scrapy crawl sephora_m
2018-11-23 13:00:05 [scrapy.utils.log] INFO: Scrapy 1.5.1 started (bot: sephora_try_mousturizer)
2018-11-23 13:00:05 [scrapy.utils.log] INFO: Versions: lxml 4.2.5.0, libxml2 2.9.8, cssselect 1.0.3, parsel 1.5.1, w3lib 1.19.0, Twisted 18.9.0, Python 3.6.7 |Anaconda, Inc.| (default, Oct 23 2018, 14:01:38) - [GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)], pyOpenSSL 18.0.0 (OpenSSL 1.1.1a 20 Nov 2018), cryptography 2.4.1, Platform Darwin-18.0.0-x86_64-i386-64bit
2018-11-23 13:00:05 [scrapy.crawler] INFO: Overridden settings: {'BOT_NAME': 'sephora_try_mousturizer', 'FEED_FORMAT': 'csv', 'FEED_URI': 'sephora_m.csv', 'NEWSPIDER_MODULE': 'sephora_try_mousturizer.spiders', 'ROBOTSTXT_OBEY': True, 'SPIDER_MODULES': ['sephora_try_mousturizer.spiders']}
2018-11-23 13:00:05 [scrapy.middleware] INFO: Enabled extensions:
['scrapy.extensions.corestats.CoreStats',
'scrapy.extensions.telnet.TelnetConsole',
'scrapy.extensions.memusage.MemoryUsage',
'scrapy.extensions.feedexport.FeedExporter',
'scrapy.extensions.logstats.LogStats']
2018-11-23 13:00:05 [scrapy.middleware] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
'scrapy.downloadermiddlewares.retry.RetryMiddleware',
'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
'scrapy.downloadermiddlewares.stats.DownloaderStats']
2018-11-23 13:00:05 [scrapy.middleware] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
'scrapy.spidermiddlewares.referer.RefererMiddleware',
'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
'scrapy.spidermiddlewares.depth.DepthMiddleware']
2018-11-23 13:00:05 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2018-11-23 13:00:05 [scrapy.core.engine] INFO: Spider opened
2018-11-23 13:00:05 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2018-11-23 13:00:05 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6024
2018-11-23 13:00:06 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.sephora.com/robots.txt> (referer: None)
2018-11-23 13:00:06 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://www.sephora.com/product/the-true-cream-aqua-bomb-P394639?icid2=top-rated:p394639:product//> (referer: None)
[]
2018-11-23 13:00:06 [scrapy.core.engine] INFO: Closing spider (finished)
2018-11-23 13:00:06 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 550,
'downloader/request_count': 2,
'downloader/request_method_count/GET': 2,
'downloader/response_bytes': 62260,
'downloader/response_count': 2,
'downloader/response_status_count/200': 2,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2018, 11, 23, 11, 0, 6, 638657),
'log_count/DEBUG': 3,
'log_count/INFO': 7,
'memusage/max': 51142656,
'memusage/startup': 51142656,
'response_received_count': 2,
'scheduler/dequeued': 1,
'scheduler/dequeued/memory': 1,
'scheduler/enqueued': 1,
'scheduler/enqueued/memory': 1,
'start_time': datetime.datetime(2018, 11, 23, 11, 0, 5, 901156)}
2018-11-23 13:00:06 [scrapy.core.engine] INFO: Spider closed (finished)
但是输出文件仍然为空。
答案 0 :(得分:1)
您在title
的css选择器中缺少一个点。
这会导致title
是一个空列表,而zip()
却是空列表。
答案 1 :(得分:0)
zip()
适用于给定的可迭代对象,如果任何可迭代对象为空列表,它将返回空值。您尚未在标题选择器.
中添加css-r4ddnb::text
从而导致列表为空。