2015-08-08 98 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

完美。現在工作。 –