我有一些规则是从数据库中动态获取的,并将它们添加到我的Spider中:
self.name = exSettings['site']
self.allowed_domains = [exSettings['root']]
self.start_urls = ['http://' + exSettings['root']]
self.rules = [Rule(SgmlLinkExtractor(allow=(exSettings['root'] + '$',)), follow= True)]
denyRules = []
for rule in exSettings['settings']:
linkRegex = rule['link_regex']
if rule['link_type'] == 'property_url':
propertyRule = Rule(SgmlLinkExtractor(allow=(linkRegex,)), follow=True, callback='parseProperty')
self.rules.insert(0, propertyRule)
self.listingEx.append({'link_regex': linkRegex, 'extraction': rule['extraction']})
elif rule['link_type'] == 'project_url':
projectRule = Rule(SgmlLinkExtractor(allow=(linkRegex,)), follow=True) #not set to crawl yet due to conflict if same links appear for both
self.rules.insert(0, projectRule)
elif rule['link_type'] == 'favorable_url':
favorableRule = Rule(SgmlLinkExtractor(allow=(linkRegex,)), follow=True)
self.rules.append(favorableRule)
elif rule['link_type'] == 'ignore_url':
denyRules.append(linkRegex)
#somehow all urls will get ignored if allow is empty and put as the first rule
d = Rule(SgmlLinkExtractor(allow=('testingonly',), deny=tuple(denyRules)), follow=True)
#self.rules.insert(0,d) #I have tried with both status but same results
self.rules.append(d)
我的数据库中有以下规则:
link_regex: /listing/\d+/.+ (property_url)
link_regex: /project-listings/.+ (favorable_url)
link_regex: singapore-property-listing/ (favorable_url)
link_regex: /mrt/ (ignore_url)
我在日志中看到了这一点:
http://www.propertyguru.com.sg/singapore-property-listing/property-for-sale/mrt/125/ang-mo-kio-mrt-station> (referer: http://www.propertyguru.com.sg/listing/8277630/for-sale-thomson-grand-6-star-development-)
不应该拒绝
/mrt/
吗?为什么我仍会爬行上述链接? 最佳答案
据我所知,deny
参数必须位于相同的SgmlLinkExtractor
中,且具有allow
模式。
在您的情况下,您创建了允许使用SgmlLinkExtractor
(favorable_url
)的'singapore-property-listing/'
。但是此提取程序没有任何deny
模式,因此它也提取/mrt/
。
要解决此问题,您应该将deny
模式添加到对应的SgmlLinkExtractor
中。另请参见related question。
也许有一些方法可以定义全局deny
模式,但是我还没有看到它们。
关于python - 粗鲁的否认规则不被忽视,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8794693/