所以我传递了一个start_url
,这是一页新闻文章(例如cnn.com)。我不想把这篇文章的内容摘录出来。为此,我使用^{
rules = (
Rule(LinkExtractor(allow=('regexToMatchArticleUrls',),
deny=('someDenyUrls')), callback='parse_article_page'),
)
def parse_article_page(self,response):
#extracts the title, date, body, etc of article
但是,我仍然可以从各个文章页面中抓取到与regexToMatchArticleUrls
匹配的链接,因为它们是指向同一网站其他部分的链接(我不能使regex更具限制性)。在
但是,为什么这些链接在DEPTH_LIMIT=1
时被爬网呢?是因为DEPTH_LIMIT
重置从LinkExtractor
提取的每个链接,即文章页面的url?有没有一种方法可以使DEPTH_LIMIT
工作,或者扩展{
为了使depthmidleware正常工作,需要将meta属性从一个请求传递到另一个请求,否则,
depth
将在每个新请求之后设置为0。在不幸的是,默认情况下,crawspider不会在一个请求到下一个请求之间保留这个meta属性。在
这可以通过使用spider中间件(
middlewares.py
)来解决:另外,别忘了在您的
^{pr2}$settings.py
中包含此中间件:相关问题 更多 >
编程相关推荐