我的蜘蛛包含以下代码,我刚刚开始使用scrapy 我要绑定到动漫列表前50位的动漫中并对其进行处理 :
import scrapy
class AnimeSpider(scrapy.Spider):
name="animelist"
start_urls=[
'https://myanimelist.net/topanime.php'
]
def parse(self,response):
for anime in response.css('tr.rankinglist'):
yield {
'name':anime.css("div.di-ib clearfix >a::text").extract_first(),
'score':anime.css(".js-top-ranking-score-col di-ib al > span.text::text").extract_first(),
}
next_page=response.css('a.link-blue-box next::attr("href")').extract_first()
if next_page is not None:
yield response.follow(next_page, self.parse)
当我运行我的代码时,所有这些事情都会发生,并且我不明白这些都是诚实的:
berseker@berseker-Inspiron-15-3567:~/python$ scrapy runspider anime_spider.py -o anime.json
2018-11-09 12:02:20 [scrapy.utils.log] INFO: Scrapy 1.5.1 started (bot: scrapybot)
2018-11-09 12:02:20 [scrapy.utils.log] INFO: Versions: lxml 4.2.1.0, libxml2 2.9.8, cssselect 1.0.3, parsel 1.5.1, w3lib 1.19.0, Twisted 18.9.0, Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) - [GCC 7.2.0], pyOpenSSL 18.0.0 (OpenSSL 1.0.2o 27 Mar 2018), cryptography 2.2.2, Platform Linux-4.15.0-38-generic-x86_64-with-debian-stretch-sid
2018-11-09 12:02:20 [scrapy.crawler] INFO: Overridden settings: {'FEED_FORMAT': 'json', 'FEED_URI': 'anime.json', 'SPIDER_LOADER_WARN_ONLY': True}
2018-11-09 12:02:20 [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-09 12:02:20 [scrapy.middleware] INFO: Enabled downloader middlewares:
['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-09 12:02:20 [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-09 12:02:20 [scrapy.middleware] INFO: Enabled item pipelines:
[]
2018-11-09 12:02:20 [scrapy.core.engine] INFO: Spider opened
2018-11-09 12:02:20 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2018-11-09 12:02:20 [scrapy.extensions.telnet] DEBUG: Telnet console listening on 127.0.0.1:6023
2018-11-09 12:02:23 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://myanimelist.net/topanime.php> (referer: None)
2018-11-09 12:02:23 [scrapy.core.engine] INFO: Closing spider (finished)
2018-11-09 12:02:23 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 226,
'downloader/request_count': 1,
'downloader/request_method_count/GET': 1,
'downloader/response_bytes': 16558,
'downloader/response_count': 1,
'downloader/response_status_count/200': 1,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2018, 11, 9, 6, 32, 23, 224266),
'log_count/DEBUG': 2,
'log_count/INFO': 7,
'memusage/max': 53026816,
'memusage/startup': 53026816,
'response_received_count': 1,
'scheduler/dequeued': 1,
'scheduler/dequeued/memory': 1,
'scheduler/enqueued': 1,
'scheduler/enqueued/memory': 1,
'start_time': datetime.datetime(2018, 11, 9, 6, 32, 20, 685482)}
2018-11-09 12:02:23 [scrapy.core.engine] INFO: Spider closed (finished)
所有这些之后,我的文件anime.json保持为空,为什么会发生 我在做什么错了?
答案 0 :(得分:1)
您的选择器
response.css('tr.rankinglist')
是错误的,它返回空列表,因此您没有任何项目。
类为ranking-list
。选择器应该是
response.css('tr.ranking-list')
或
response.xpath("//tr[@class='ranking-list'])