2017-10-10 3 views
1

Scrapy는 내 사이트를 크롤링하고 404 응답의 링크를 찾아서 JSON 파일로 반환합니다. 이것은 정말 잘 작동합니다.404 오류의 모든 사례 얻기

그러나 복제 필터가 이러한 링크를 포착하고 다시 시도하지 않기 때문에 나쁜 링크의 모든 인스턴스를 얻는 방법을 알 수 없습니다.

우리 사이트에는 수천 페이지가 있기 때문에 여러 팀이 섹션을 관리하므로 하나를 찾고 전체 사이트에서 바꾸기보다는 섹션 당 잘못된 링크 보고서를 만들어야합니다.

도움을 주시면 감사하겠습니다.

나의 현재 크롤러 : 나는 몇 가지 사용자 정의 속는 필터를 시도했지만 결국 그들 중 누구도 일하지

import scrapy 
from scrapy.spiders import CrawlSpider, Rule 
from scrapy.linkextractors.lxmlhtml import LxmlLinkExtractor 
from scrapy.item import Item, Field 

# Add Items for exporting to JSON 
class DevelopersLinkItem(Item): 
    url = Field() 
    referer = Field() 
    link_text = Field() 
    status = Field() 
    time = Field() 

class DevelopersSpider(CrawlSpider): 
    """Subclasses Crawlspider to crawl the given site and parses each link to JSON""" 

    # Spider name to be used when calling from the terminal 
    name = "developers_prod" 

    # Allow only the given host name(s) 
    allowed_domains = ["example.com"] 

    # Start crawling from this URL 
    start_urls = ["https://example.com"] 

    # Which status should be reported 
    handle_httpstatus_list = [404] 

    # Rules on how to extract links from the DOM, which URLS to deny, and gives a callback if needed 
    rules = (Rule(LxmlLinkExtractor(deny=([ 
     '/android/'])), callback='parse_item', follow=True),) 

    # Called back to for each requested page and used for parsing the response 
    def parse_item(self, response): 
     if response.status == 404: 
      item = DevelopersLinkItem() 
      item['url'] = response.url 
      item['referer'] = response.request.headers.get('Referer') 
      item['link_text'] = response.meta.get('link_text') 
      item['status'] = response.status 
      item['time'] = self.now.strftime("%Y-%m-%d %H:%M") 

      return item 

.

답변