python - Scrapy:无法理解有关 robots.txt 的日志
问题描述
我的问题是,如果这个日志意味着网站不能被抓取?我将用户代理更改为看起来像浏览器,但没有帮助。另外,我省略了“start_requests”中的“s”,但这也没有帮助。即使我在 seetings.py 中更改了“ROBOTSTXT_OBEY = False”,但也没有帮助。
这是我得到的日志:
2020-11-17 18:06:41 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2020-11-17 18:06:41 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2020-11-17 18:06:42 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://podcasts.apple.com/robots.txt> (referer: None)
2020-11-17 18:06:42 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://podcasts.apple.com/us/genre/podcasts-arts/id1301> (referer: None)
2020-11-17 18:06:43 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://podcasts.apple.com/us/genre/podcasts-arts/id1301> (referer: https://podcasts.apple.com/us/genre/podcasts-arts/id1301)
2020-11-17 18:06:43 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://podcasts.apple.com/us/genre/podcasts-arts-books/id1482> (referer: https://podcasts.apple.com/us/genre/podcasts-arts/id1301)
2020-11-18 17:29:49 [scrapy.core.engine] INFO: Closing spider (finished)
2020-11-18 17:29:49 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 1342,
'downloader/request_count': 4,
'downloader/request_method_count/GET': 4,
'downloader/response_bytes': 67297,
'downloader/response_count': 4,
'downloader/response_status_count/200': 4,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2020, 11, 18, 13, 59, 49, 133234),
'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, 18, 13, 59, 48, 758371)}
2020-11-18 17:29:49 [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']
alphabets=response.xpath("//div[@class='content']/div[@class='padder']/div[@class='selectedgenre']")
for alphabet in alphabets:
alphabet_link=alphabet.xpath(".//ul[@class='list alpha']/li/a[@class='selected']/@href").get()
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']
podcasts=response.xpath("//div[@class='grid3-column']")
for podcast in podcasts:
podcast_name=podcast.xpath(".//div[@class='column first']/ul/li/a/text()").get()
podcast_link=podcast.xpath(".//div[@class='column first']/ul/li/a/@href").get()
for podcast in podcasts:
podcast_name=podcast.xpath(".//div[@class='column']/ul/li/a/text()").get()
podcast_link=podcast.xpath(".//div[@class='column']/ul/li/a/@href").get()
for podcast in podcasts:
podcast_name=podcast.xpath(".//div[@class='column last']/ul/li/a/text()").get()
podcast_link=podcast.xpath(".//div[@class='column last']/ul/li/a/@href").get()
yield response.follow(url=podcast_link, callback=self.parse_podcasts, meta={'category_name':category_name,'sub_category_name':sub_category_name,'podcast_name':podcast_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']
podcast_name = response.request.meta['podcast_name']
podcasts = response.xpath("//div[@class='ember-view l-content-width section section--hero product-hero']/section[@class='ember-view']/div[@class='l-row']")
for podcast in podcasts:
rate = podcast.xpath(".//div[@class='l-column small-12 medium-7 large-8 small-valign-top']/div[@class='l-row']/div[@class='l-column small-7 medium-12 small-valign-top']/header[@class='product-header product-header--padded-start podcast-header']/ul[@class='product-header__list podcast-header__list']/li[2]//ul[@class='inline-list inline-list--mobile-compact']/li[@class='inline-list__item']/figure/figcaption/text()[1]").get()
rating_number = podcast.xpath(".//div[@class='l-column small-12 medium-7 large-8 small-valign-top']/div[@class='l-row']/div[@class='l-column small-7 medium-12 small-valign-top']/header[@class='product-header product-header--padded-start podcast-header']/ul[@class='product-header__list podcast-header__list']/li[2]//ul[@class='inline-list inline-list--mobile-compact']/li[@class='inline-list__item']/figure/figcaption/text()[3]").get()
yield{
'category_name':category_name,
'sub_category_name':sub_category_name,
'podcast_name':podcast_name,
'rate':rate,
'rating_number':rating_number
}
next_page=response.xpath("//div[@class='content']/div[@class='padder']/div[@class='selectedgenre']/ul[@class='list paginate']/li/a[@class='selected']/@href").get()
if next_page:
yield scrapy.Request(url=next_page, callback=self.parse_podcasts, dont_filter=True)
谁能帮助我了解问题所在以及如何解决?
谢谢!!!
---编辑1---
更改“allowed_domains”部分后得到的日志:
2020-11-18 13:49:18 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2020-11-18 13:49:18 [scrapy.extensions.httpcache] DEBUG: Using filesystem cache storage in C:\Users\shima\projects\apple_podcasts\.scrapy\httpcache
2020-11-18 13:49:18 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2020-11-18 13:49:18 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://podcasts.apple.com/robots.txt> (referer: None) ['cached']
2020-11-18 13:49:18 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://podcasts.apple.com/us/genre/podcasts-arts/id1301> (referer: None) ['cached']
2020-11-18 13:49:18 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://podcasts.apple.com/us/genre/podcasts-arts/id1301> (referer: https://podcasts.apple.com/us/genre/podcasts-arts/id1301) ['cached']
2020-11-18 13:49:18 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://podcasts.apple.com/us/genre/podcasts-arts-books/id1482> (referer: https://podcasts.apple.com/us/genre/podcasts-arts/id1301) ['cached']
---编辑2---
删除“try and except”语句后得到的日志:
2020-11-18 13:53:07 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2020-11-18 13:53:07 [scrapy.extensions.httpcache] DEBUG: Using filesystem cache storage in C:\Users\shima\projects\apple_podcasts\.scrapy\httpcache
2020-11-18 13:53:07 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
2020-11-18 13:53:07 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://podcasts.apple.com/robots.txt> (referer: None) ['cached']
2020-11-18 13:53:07 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://podcasts.apple.com/us/genre/podcasts-arts/id1301> (referer: None) ['cached']
2020-11-18 13:53:07 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://podcasts.apple.com/us/genre/podcasts-arts/id1301> (referer: https://podcasts.apple.com/us/genre/podcasts-arts/id1301) ['cached']
2020-11-18 13:53:07 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://podcasts.apple.com/us/genre/podcasts-arts-books/id1482> (referer: https://podcasts.apple.com/us/genre/podcasts-arts/id1301) ['cached']
解决方案
您的执行日志没有任何问题。
2020-11-17 18:06:42 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://podcasts.apple.com/us/genre/podcasts-arts/id1301> (referer: None)
这些是调试消息,它告诉您您的蜘蛛抓取了提到的 URL。如果你不想看到它,你可以LOG_LEVEL='INFO'
在你的settings.py
.
另外,您没有定义任何要抓取的项目,只是定义了要抓取的页面,因此蜘蛛将遍历页面并最终关闭。
编辑:
我没有检查你的整个代码,但我确实执行了一次并注意到了一些问题。
此选择器将返回空:
alphabets=response.xpath("//div[@class='content']/div[@class='padder']/div[@class='selectedgenre']")
您使用@class
而不是@id
两者'content'
和'selectedgenre'
。XPath 应该是:
//div[@id='content']/div[@class='padder']/div[@id='selectedgenre']
请注意,这将只返回一个选择器,因此您不应该对其进行迭代。您可能想要遍历链接。
这也将返回空:
alphabet_link=alphabet.xpath(".//ul[@class='list alpha']/li/a[@class='selected']/@href").get()
由于您正在使用的 XPath 中没有a
类'selected'
(默认情况下)元素。
我想会有更多损坏的 XPath,因此您应该检查选择器以确保您的 XPath 是正确的并且它们返回您期望的内容。没有与robots.txt
btw 相关的问题。
推荐阅读
- python - Python FTP服务器下载文件名中带有特定关键字的最新文件
- swift - Checking multiple conditions in a switch statement
- azure-service-fabric - 如何在所有 Service Fabric 节点中调用方法?
- postgresql - 如何从 .bat 文件在 heroku 上的远程 postgres 数据库上运行代码
- r - 代码优化——data.table,当前for循环多引用优化成data.table
- ionic-framework - 离子:如何修复 UnhandledPromiseRejectionWarning?
- mule - Mule 4 中的批处理
- jquery - jQuery - 如果 div 重叠调整 css 顶部值
- python - 在python中的指定html块之间提取文本
- testng - KotlinTest 是否也支持 TestNG?