CrawlSpider 规则不适用于目标站点中实际存在的 link
CrawlSpider Rule not working for a link that actually exists in target site
我一直在努力寻找一种方法让我的蜘蛛工作。场景是这样的:我正在尝试查找特定目标网站中包含的特定域的所有 URL。为此,我定义了一些规则,这样我就可以抓取网站并找到我感兴趣的链接。
问题是它似乎不起作用,即使我知道网站内有格式正确的链接。
这是我的蜘蛛:
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
class sp(CrawlSpider):
name = 'sp'
start_urls = ['https://nationalpavementexpo.com/show/floor-plan-exhibitor-list/']
custom_settings = {
'LOG_LEVEL': 'INFO',
'DEPTH_LIMIT': 4
}
rules = (
Rule(LinkExtractor(unique=True, allow_domains='a2zinc.net'), callback='parse_item'),
Rule(LinkExtractor(unique=True, canonicalize=True, allow_domains = 'nationalpavementexpo.com'))
)
def parse_item(self, response):
print(response.request.url)
yield {'link':response.request.url}
因此,总而言之,我试图找到 https://nationalpavementexpo.com/show/floor-plan-exhibitor-list/ 及其子部分中包含的 'a2zinc.net' 中的所有链接。
如你们所见,目标网站内至少出现了 3 次所需链接。
有趣的是,当我使用另一个也包含感兴趣的链接的目标站点(如 this one)测试蜘蛛时,它按预期工作,我看不出有什么不同。
此外,如果我在解析方法中定义一个 Link Extractor 实例(如下面的代码片段所示),它也能够找到所需的链接,但我认为这不是最好的使用 CrawlSpider + Rules 的方式。
def parse_item(self, response):
le = LinkExtractor(allow_domains='a2zinc.net')
links = le.extract_links(response)
for link in links:
yield {'link': link.url}
知道问题的原因是什么吗?
非常感谢。
您的代码有效。唯一的问题是您已将日志记录级别设置为 INFO
,而正在提取的链接返回状态代码 403
,该代码仅在 DEBUG
级别可见。注释掉您的自定义设置,您将看到正在提取链接。
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
class sp(CrawlSpider):
name = 'sp'
start_urls = ['https://nationalpavementexpo.com/show/floor-plan-exhibitor-list/']
custom_settings = {
# 'LOG_LEVEL': 'INFO',
# 'DEPTH_LIMIT': 4
}
rules = (
Rule(LinkExtractor(allow_domains='a2zinc.net'), callback='parse_item'),
Rule(LinkExtractor(unique=True, canonicalize=True, allow_domains = 'nationalpavementexpo.com'))
)
def parse_item(self, response):
print(response.request.url)
yield {'link':response.request.url}
输出:
我一直在努力寻找一种方法让我的蜘蛛工作。场景是这样的:我正在尝试查找特定目标网站中包含的特定域的所有 URL。为此,我定义了一些规则,这样我就可以抓取网站并找到我感兴趣的链接。
问题是它似乎不起作用,即使我知道网站内有格式正确的链接。
这是我的蜘蛛:
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
class sp(CrawlSpider):
name = 'sp'
start_urls = ['https://nationalpavementexpo.com/show/floor-plan-exhibitor-list/']
custom_settings = {
'LOG_LEVEL': 'INFO',
'DEPTH_LIMIT': 4
}
rules = (
Rule(LinkExtractor(unique=True, allow_domains='a2zinc.net'), callback='parse_item'),
Rule(LinkExtractor(unique=True, canonicalize=True, allow_domains = 'nationalpavementexpo.com'))
)
def parse_item(self, response):
print(response.request.url)
yield {'link':response.request.url}
因此,总而言之,我试图找到 https://nationalpavementexpo.com/show/floor-plan-exhibitor-list/ 及其子部分中包含的 'a2zinc.net' 中的所有链接。
如你们所见,目标网站内至少出现了 3 次所需链接。
有趣的是,当我使用另一个也包含感兴趣的链接的目标站点(如 this one)测试蜘蛛时,它按预期工作,我看不出有什么不同。
此外,如果我在解析方法中定义一个 Link Extractor 实例(如下面的代码片段所示),它也能够找到所需的链接,但我认为这不是最好的使用 CrawlSpider + Rules 的方式。
def parse_item(self, response):
le = LinkExtractor(allow_domains='a2zinc.net')
links = le.extract_links(response)
for link in links:
yield {'link': link.url}
知道问题的原因是什么吗?
非常感谢。
您的代码有效。唯一的问题是您已将日志记录级别设置为 INFO
,而正在提取的链接返回状态代码 403
,该代码仅在 DEBUG
级别可见。注释掉您的自定义设置,您将看到正在提取链接。
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
class sp(CrawlSpider):
name = 'sp'
start_urls = ['https://nationalpavementexpo.com/show/floor-plan-exhibitor-list/']
custom_settings = {
# 'LOG_LEVEL': 'INFO',
# 'DEPTH_LIMIT': 4
}
rules = (
Rule(LinkExtractor(allow_domains='a2zinc.net'), callback='parse_item'),
Rule(LinkExtractor(unique=True, canonicalize=True, allow_domains = 'nationalpavementexpo.com'))
)
def parse_item(self, response):
print(response.request.url)
yield {'link':response.request.url}
输出: