2014-01-24 1 views
1

많은 수의 URL을 크롤링하고 있으며 'meta name = "robots"content = "noindex"'로 페이지를 구문 분석 할 수 있는지 궁금합니다. 여기에 나열된 거부 규칙을 보면은 거부 규칙이 URL에만 적용되는 것처럼 보입니다. scrapy가 xpath를 무시할 수 있습니까?Scrapy가 noindex를 무시합니다

from scrapy.selector import HtmlXPathSelector 
from scrapy.contrib.spiders import CrawlSpider, Rule 
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor 

from wallspider.items import Website 


class Spider(CrawlSpider): 
    name = "browsetest" 
    allowed_domains = ["www.mydomain.com"] 
    start_urls = ["http://www.mydomain.com",] 

    rules = (
     Rule(SgmlLinkExtractor(allow=('/browse/')), callback="parse_items", follow= True), 
     Rule(SgmlLinkExtractor(allow=(),unique=True,deny=('/[1-9]$', '(bti=)[1-9]+(?:\.[1-9]*)?', '(sort_by=)[a-zA-Z]', '(sort_by=)[1-9]+(?:\.[1-9]*)?', '(ic=32_)[1-9]+(?:\.[1-9]*)?', '(ic=60_)[0-9]+(?:\.[0-9]*)?', '(search_sort=)[1-9]+(?:\.[1-9]*)?', 'browse-ng.do\?', '/page/', '/ip/', 'out\+value', 'fn=', 'customer_rating', 'special_offers', 'search_sort=&', 'facet='))), 
    ) 

    def parse_items(self, response): 
     hxs = HtmlXPathSelector(response) 
     sites = hxs.select('//html') 
     items = [] 

     for site in sites: 
      item = Website() 
      item['url'] = response.url 
      item['canonical'] = site.xpath('//head/link[@rel="canonical"]/@href').extract() 
      item['robots'] = site.select('//meta[@name="robots"]/@content').extract() 
      items.append(item) 

     return items 
+1

해당 페이지 검색을 건너 뛰시겠습니까? 그렇다면 메타 로봇을 검색하기 위해 페이지를 검색해야하기 때문에 불가능합니다. – Rolando

+0

죄송합니다. 제 질문에 대한 답을 구했습니다. 'meta name = "robots"content = "noindex"'가 포함 된 URL을 구문 분석하지 않을 수 있습니까? –

+0

xpath를 거부 할 수 있습니까? –

답변

4

불행히도 CrawlSpider은 원하는 작업에 대한 옵션을 제공하지 않습니다. 그럼에도 불구하고이를 달성하기위한 방법을 재정의 할 수 있습니다. 거미에이 방법을 추가

봅니다 :

문서가 충분하지 않습니다 때마다
def _response_downloaded(self, response): 
     # Check whether this page contains the meta noindex in order to skip the processing. 
     sel = Selector(response) 
     if sel.xpath('//meta[@content="noindex"]'): 
      return 

     return super(Spider, self)._response_downloaded(response) 

, 당신은 당신이 변경하고 사용중인 버전의 경우, 단지에주의 할 수 있는지 확인하기 위해 소스 코드를 확인하실 수 있습니다 . github에서 최신 소스 코드를 브라우징 할 수 있습니다 : https://github.com/scrapy/scrapy/blob/master/scrapy/contrib/spiders/crawl.py#L61

그러나 시스템의 소스 코드를 확인하는 것이 좋습니다.연산자로 쉽게 완료 할 수있는 IPython을 사용하는 경우.

+0

소스 코드에 대한 팁 - 저는 파이썬과 프로그래밍에 익숙하지 않아 도움이 대단히 도움이되었습니다. –

+0

다른 크롤러의 경우 메타 컨텐트에 noindex가 포함 된 경우에만 구문 분석을 수행하는 방법은 무엇입니까? 'super (Spider, self) .parse_items (response) '를 반환하겠습니까? –

+0

@ Murdrae 예, if 블록 내에서 슈퍼 리턴 (...)을 이동하십시오. – Rolando

관련 문제