2015-08-08 91 views
1

我在学习scrapy,目前我正在试图解析bbc网站。生成单一链接的scrapy规则

我觉得我已经做得很好,但规则只生成一个单一的链接。下面是代码:

class BBCSpider(CrawlSpider): 
    name = "bbc" 
    allowed_domains = ["http://www.bbc.com"] 
    start_urls = [ 
     "http://www.bbc.com/news/world", 
    ] 

    rules = [ 
     Rule(LinkExtractor(allow=r"http://www.bbc.com/news/world-.*"), 
      callback='parse_item', follow=True) 
    ] 


    def parse_item(self, response): 
     print(response) 

目前,只产生一个单一的链路(http://www.bbc.com/news/world-middle-east-33833400)。我完全不知道为什么。正则表达式匹配页面上更多的链接。

非常感谢。

回答

0

很多环节都是这样的一个(具有相对URL):

<a href="/news/world-middle-east-33833400" class="title-link"> 
    ... 
</a> 

检查只有news/world-.*

rules = [ 
    Rule(LinkExtractor(allow=r"/news/world-.*"), 
     callback='parse_item', follow=True) 
] 

此外,allowed_domains应包含域:

allowed_domains = ["bbc.com"] 
+0

仍然没有工作。我尝试了一堆链接来查看正则表达式是否匹配它们,它确实如此。 –

+0

@WebMatrix没关系,更新。现在适合我。 – alecxe

+0

完美。现在工作。 –