我想,日志没有显示出严重的问题,但没有刮伤任何元素。所以,我想问题可能是因为XPath表达式。但是,我仔细检查了它们,并尽可能地简化它们。因此,我真的需要帮助找到这里的bug
这是我得到的日志:
2020-11-19 12:57:07 [scrapy.core.engine] INFO: Spider opened
2020-11-19 12:57:07 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2020-11-19 12:57:07 [scrapy.extensions.httpcache] DEBUG: Using filesystem cache storage in C:\Users\shima\projects\apple_podcasts\.scrapy\httpcache
2020-11-19 12:57:07 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2020-11-19 12:57:07 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://podcasts.apple.com/robots.txt> (referer: None) ['cached']
2020-11-19 12:57:07 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://podcasts.apple.com/us/genre/podcasts-arts-books/id1482> (referer: None) ['cached']
2020-11-19 12:57:07 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://podcasts.apple.com/us/genre/podcasts-arts-books/id1482?letter=A> (referer: https://podcasts.apple.com/us/genre/podcasts-arts-books/id1482) ['cached']
2020-11-19 12:57:08 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://podcasts.apple.com/us/podcast/about-myself/id1494962309> (referer: https://podcasts.apple.com/us/genre/podcasts-arts-books/id1482?letter=A) ['cached']
2020-11-19 12:57:08 [scrapy.core.engine] INFO: Closing spider (finished)
2020-11-19 12:57:08 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 1415,
'downloader/request_count': 4,
'downloader/request_method_count/GET': 4,
'downloader/response_bytes': 75168,
'downloader/response_count': 4,
'downloader/response_status_count/200': 4,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2020, 11, 19, 9, 27, 8, 185767),
'httpcache/hit': 4,
'log_count/DEBUG': 5,
'log_count/INFO': 9,
'request_depth_max': 2,
'response_received_count': 4,
'robotstxt/request_count': 1,
'robotstxt/response_count': 1,
'robotstxt/response_status_count/200': 1,
'scheduler/dequeued': 3,
'scheduler/dequeued/memory': 3,
'scheduler/enqueued': 3,
'scheduler/enqueued/memory': 3,
'start_time': datetime.datetime(2020, 11, 19, 9, 27, 7, 723909)}
2020-11-19 12:57:08 [scrapy.core.engine] INFO: Spider closed (finished)
为了解释这个爬行器的功能,让人更容易理解,因为乍一看它可能看起来很庞大,但事实并非如此,我告诉我的爬行器通过几个链接,每次都会产生一些元素。这是我的蜘蛛:
import scrapy
class ApplePodcastSpider(scrapy.Spider):
name = 'apple_podcast'
allowed_domains = ['podcasts.apple.com']
def start_requests(self):
yield scrapy.Request(url='https://podcasts.apple.com/us/genre/podcasts-arts/id1301',
callback= self.parse_categories,dont_filter = True)
def parse_categories(self, response):
categories=response.xpath("//ul[@class='list column first']")
for category in categories:
category_name=category.xpath(".//li/a/text()").get()
category_link=category.xpath(".//li/a/@href").get()
yield response.follow(url=category_link, callback=self.parse_sub_categories, meta={'category_name': category_name}, dont_filter=True)
def parse_sub_categories(self, response):
category_name=response.request.meta['category_name']
sub_categories=response.xpath("//ul[@class='list column first']/li")
for sub_category in sub_categories:
sub_category_name=sub_category.xpath(".//ul[@class='list top-level-subgenres']/li/a/text()").get()
sub_category_link=sub_category.xpath(".//ul[@class='list top-level-subgenres']/li/a/@href").get()
if sub_category_link is None:
pass
else:
yield response.follow(url=sub_category_link, callback=self.parse_alphabet, meta={'category_name': category_name,'sub_category_name': sub_category_name}, dont_filter=True)
def parse_alphabet(self,response):
category_name=response.request.meta['category_name']
sub_category_name=response.request.meta['sub_category_name']
for alphabet_link in response.xpath("//ul[@class='list alpha']/li/a/@href").getall():
yield response.follow(url=alphabet_link, callback=self.parse_podcasts_link, meta={'category_name': category_name, 'sub_category_name': sub_category_name},dont_filter = True)
def parse_podcasts_link(self, response):
category_name=response.request.meta['category_name']
sub_category_name=response.request.meta['sub_category_name']
for podcast_node in response.xpath("//div[@class='column first']/ul/li/a"):
podcast_link = podcast_node.xpath('.//@href').get()
for podcast_node in response.xpath("//div[@class='column']/ul/li/a"):
podcast_link = podcast_node.xpath('.//@href').get()
for podcast_node in response.xpath("//div[@class='column last']/ul/li/a"):
podcast_link = podcast_node.xpath('.//@href').get()
yield response.follow(url=podcast_link, callback=self.parse_podcasts, meta={'category_name':category_name,'sub_category_name':sub_category_name},dont_filter = True)
def parse_podcasts(self, response):
category_name = response.request.meta['category_name']
sub_category_name = response.request.meta['sub_category_name']
name = response.xpath('normalize-space(//span[@data-test-podcast-name])').get()
author = response.xpath('normalize-space(//span[@data-test-artist-name])').get()
podcasts = response.xpath("//div[@id='ember146']/section/div")
for podcast in podcasts:
if podcast.xpath(".//div[@class='l-column small-12 medium-7 large-8 small-valign-top']/div/div[2]/header/ul/li[2]/ul/li/figure/figcaption/text()[1]"):
rate = podcast.xpath(".//div[@class='l-column small-12 medium-7 large-8 small-valign-top']/div/div[2]/header/ul/li[2]/ul/li/figure/figcaption/text()[1]").get()
else:
rate=''
if podcast.xpath(".//div[@class='l-column small-12 medium-7 large-8 small-valign-top']/div/div[2]/header/ul/li[2]/ul/li/figure/figcaption/text()[3]"):
rating_number = podcast.xpath(".//div[@class='l-column small-12 medium-7 large-8 small-valign-top']/div/div[2]/header/ul/li[2]/ul/li/figure/figcaption/text()[3]").get()
else:
rating_number=''
if podcast.xpath(".//section[@id='ember147']/div/div/div[1]/p/text()"):
episodes = podcast.xpath(".//section[@id='ember147']/div/div/div[1]/p/text()").get()
else:
episodes=''
if podcast.xpath(".//section[@id='ember393']/div/div[1]/div[2]/section/div/div/p/text()[1]"):
description_in_short= podcast.xpath(".//section[@id='ember393']/div/div[1]/div[2]/section/div/div/p/text()[1]").get()
else:
description_in_short=''
if podcast.xpath(".//section[@id='ember393']/div/div[1]/div[2]/section/div/div/p/text()[2]"):
description_the_rest=podcast.xpath(".//section[@id='ember393']/div/div[1]/div[2]/section/div/div/p/text()[2]").get()
else:
description_the_rest=''
yield{
'category_name':category_name,
'sub_category_name':sub_category_name,
'name':name,
'author':author,
'rate':rate,
'rating_number':rating_number,
'episodes':episodes,
'description':f'{description_in_short}+{description_the_rest}'
}
next_page=response.xpath("//div[@id='content']/div[@class='padder']/div[@id='selectedgenre']/ul[@class='list paginate'][1]/li/a/@href").get()
if next_page:
yield scrapy.Request(url=next_page, callback=self.parse_podcasts,dont_filter = True)
谢谢你的帮助
---编辑--
这是我在gangabass得到的日志:
2020-11-22 11:18:52 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://podcasts.apple.com/us/podcast/bake-sale-by-nora-voutas/id1526021229> (referer: https://podcasts.apple.com/us/genre/podcasts-arts-books/id1482?letter=B) ['cached']
2020-11-22 11:18:52 [scrapy.core.engine] INFO: Closing spider (finished)
2020-11-22 11:18:52 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 875349,
'downloader/request_count': 2100,
'downloader/request_method_count/GET': 2100,
'downloader/response_bytes': 75683564,
'downloader/response_count': 2100,
'downloader/response_status_count/200': 2100,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2020, 11, 22, 7, 48, 52, 286711),
'httpcache/hit': 2100,
'log_count/DEBUG': 2101,
'log_count/INFO': 10,
'request_depth_max': 4,
'response_received_count': 2100,
'robotstxt/request_count': 1,
'robotstxt/response_count': 1,
'robotstxt/response_status_count/200': 1,
'scheduler/dequeued': 2099,
'scheduler/dequeued/memory': 2099,
'scheduler/enqueued': 2099,
'scheduler/enqueued/memory': 2099,
'start_time': datetime.datetime(2020, 11, 22, 7, 47, 46, 330411)}
2020-11-22 11:18:52 [scrapy.core.engine] INFO: Spider closed (finished)
我建议对
parse_podcast
使用以下表达式:对于您的
parse_alphabet
(正如@renatodvc所指出的):对于
parse_podcasts_link
:相关问题 更多 >
编程相关推荐