2014-12-11 49 views
0

由於某些原因,某個移動網址正在被抓取,並且生成的網址在被抓取時發生錯誤。我希望scrapy只是忽略url,不要調用解析方法或其他任何東西。禁止抓取某個網址

class MySpider(scrapy.Spider): 

    # name, allowed_domains etc 
    rules = Rule(LxmlLinkExtractor(deny=r'/m/.+') # deny http://example.com/m/anything-here.html 

但這是行不通的,這樣的鏈接仍然被抓取。

回答

0

根據the docs

deny(正則表達式(或列表)) - 一個單一的正則表達式(或正則表達式的清單),該(絕對)的URL必須按順序匹配要被排除(即未提取)。

/m/.+將不匹配絕對URL,如http://example.com/m/anything-here.html。出於同樣的原因,你需要的.+就完了,你需要在一開始至少.*

>>> print(re.match(r'/m/.+', 'http://example.com/m/anything-here.html')) 
None 
>>> print(re.match(r'.*/m/.+', 'http://example.com/m/anything-here.html')) 
<_sre.SRE_Match object; span=(0, 39), match='http://example.com/m/anything-here.html'> 
+0

如果他想否認只是問題的域,更好的表達是通過'http://例子.com/m /.+',因爲他可能還想要像'http:// test.com/m/something.html'那樣的其他人。 – bosnjak 2014-12-11 10:04:15

+0

@勞倫斯:當然,但是考慮到他寫的問題的方式,以及他用'/ m /.+'寫的這個事實,我敢肯定他想拒絕(1)任何URL '/ m /',或者(2)路徑組件以'/ m /'開頭的任何URL,而不是特定的域。 – abarnert 2014-12-13 01:59:04