我一直在尝试创建一个可以轻松更改的简单 Scrapy CrawlSpider 脚本,但我无法弄清楚如何让链接提取器规则正常工作。
这是我的代码:
class LernaSpider(CrawlSpider):
"""Our ad-hoc spider"""
name = "lerna"
def __init__(self, url, allow_follow='.*', deny_follow='', allow_extraction='.*', deny_extraction=''):
parsed_url = urlparse(url)
domain = str(parsed_url.netloc)
self.allowed_domains = [domain]
self.start_urls = [url]
self.rules = (
# Extract links
# and follow links from them (since no callback means follow=True by default).
Rule(SgmlLinkExtractor(allow=(allow_follow, ), deny=(deny_follow, ))),
# Extract links and parse them with the spider's method parse_item
Rule(SgmlLinkExtractor(allow=(allow_extraction, ), deny=(deny_extraction, )), callback='parse_item'),
)
super(LernaSpider, self).__init__()
def parse_item(self, response):
print 'Crawling... %s' % response.url
# more stuff here
我有这段代码,但我永远无法让允许/拒绝规则正常工作,我真的不明白为什么。留下空字符串会导致它否认一切吗?我想既然它是一个 RE,它只会在我输入 '.*' 或其他什么的情况下进行全面拒绝。
任何帮助,将不胜感激。