Python: Scrapy Shell 사용 튜 토리 얼

5207 단어 pythonscrapyshell
Scrapy shell 은 대화 형 셸 입 니 다. Scrapy shell 을 사용 하 는 것 에 익숙해 지면 Scrapy shell 은 파충류 개발 에 매우 좋 은 테스트 도구 라 는 것 을 알 게 될 것 입 니 다.Scrapy 셸 을 사용 하기 전에 ipython 을 설치 해 야 합 니 다.http://www.lfd.uci.edu/~ gohlke / pythonlibs / 해당 버 전의 ipython 을 찾 아 설치 합 니 다).
셸 을 사용 하면 다음 명령 을 사용 하여 셸 을 사용 할 수 있 습 니 다.        
scrapy shell <url>
그 중에서 < url > 은 바로 당신 이 잡 고 싶 은 페이지 url 입 니 다.
    셸 사용 하기    Scrapy shell 은 몇 가지 유용 한 기능 함수 가 내 장 된 python 콘 솔 프로그램 으로 볼 수 있 습 니 다.
        기능 함수
  • shelp () - 일련의 사용 가능 한 대상 과 함수 출력
  • fetch (request or url) - 주어진 url 또는 기 존 request 요청 대상 에서 response 대상 을 다시 생 성하 고 기 존 대상 을 업데이트 합 니 다
  • view (response) - 브 라 우 저 를 사용 하여 기 존의 response 대상 을 엽 니 다 (다시 말 하면 html 페이지)
  •         Scrapy 개체        Scrapy shell 을 사용 하여 지정 한 페이지 를 다운로드 할 때 Response 대상 과 Selector 대상 (Html 과 XML 모두 적용) 과 같은 사용 가능 한 대상 을 생 성 합 니 다.
            사용 가능 한 대상 은 다음 과 같 습 니 다.
  • crawler - 현재 Crawler 대상
  • spider 
  • request - 마지막 페이지 요청 대상 가 져 오기
  • response - 마지막 으로 가 져 온 페이지 를 포함 하 는 응답 대상
  • sel    - 최신 다운로드 페이지 의 Selector 대상
  • settings - 현재 의 Scrapy settings
  •     Scrapy 셸 예        나의 개인 블 로 그 를 테스트 로 한다.http://blog.csdn.net/php_fly
            우선 셸 을 시작 합 니 다.        
    scrapy shell http://blog.csdn.net/php_fly --nolog

    이 명령 을 실행 하면 Scrapy downloader 를 사용 하여 지정 한 url 페이지 데 이 터 를 다운로드 하고 사용 가능 한 대상 과 함수 목록 을 출력 합 니 다.
        [s] Available Scrapy objects:
        [s]   crawler    <scrapy.crawler.Crawler object at 0x0000000002AEF7B8>
        [s]   item       {}
        [s]   request    <GET http://blog.csdn.net/php_fly>
        [s]   response   <200 http://blog.csdn.net/php_fly>
        [s]   sel        <Selector xpath=None data=u'<html xmlns="http://www.w3.org/1999/xhtm'>
        [s]   settings   <CrawlerSettings module=None>
        [s]   spider     <Spider 'default' at 0x4cdb940>
        [s] Useful shortcuts:
        [s]   shelp()           Shell help (print this help)
        [s]   fetch(req_or_url) Fetch request (or URL) and update local objects
        [s]   view(response)    View response in a browser

        토목인 블 로그 의 글 목록 하이퍼링크 가 져 오기
     In [9]: sel.xpath("//span[@class='link_title']/a/@href").extract()
        Out[9]:
        [u'/php_fly/article/details/19364913',
         u'/php_fly/article/details/18155421',
         u'/php_fly/article/details/17629021',
         u'/php_fly/article/details/17619689',
         u'/php_fly/article/details/17386163',
         u'/php_fly/article/details/17266889',
         u'/php_fly/article/details/17172381',
         u'/php_fly/article/details/17171985',
         u'/php_fly/article/details/17145295',
         u'/php_fly/article/details/17122961',
         u'/php_fly/article/details/17117891',
         u'/php_fly/article/details/14533681',
         u'/php_fly/article/details/13162011',
         u'/php_fly/article/details/12658277',
         u'/php_fly/article/details/12528391',
         u'/php_fly/article/details/12421473',
         u'/php_fly/article/details/12319943',
         u'/php_fly/article/details/12293587',
         u'/php_fly/article/details/12293381',
         u'/php_fly/article/details/12289803']

        scrapy 셸 의 요청 방식 수정:
         >>> request = request.replace(method="POST")
            >>> fetch(request)
            [s] Available Scrapy objects:
            [s]   crawler    <scrapy.crawler.Crawler object at 0x1e16b50>
            ...

    Spider 에서 Scrapy shell 을 호출 하여 파충류 가 운행 하 는 과정 에서 어떤 응답 이 원 하 는 지 확인 해 야 할 때 가 있 습 니 다.
        이 요 구 는 scrapy. shell. inspect 를 통 해response 함수 구현
        다음은 spider 에서 scrapy 셸 을 어떻게 호출 하 는 지 에 대한 예 입 니 다.
    from scrapy.spider import Spider
    
    
    class MySpider(Spider):
        name = "myspider"
        start_urls = [
            "http://example.com",
            "http://example.org",
            "http://example.net",
        ]
    
        def parse(self, response):
            # We want to inspect one specific response.
            if ".org" in response.url:
                from scrapy.shell import inspect_response
                inspect_response(response)
    
            # Rest of parsing code.

    파충 류 를 시작 할 때 콘 솔 은 다음 과 같은 정 보 를 출력 합 니 다.    
    2014-02-20 17:48:31-0400 [myspider] DEBUG: Crawled (200) <GET http://example.com> (referer: None)
        2014-02-20 17:48:31-0400 [myspider] DEBUG: Crawled (200) <GET http://example.org> (referer: None)
        [s] Available Scrapy objects:
        [s]   crawler    <scrapy.crawler.Crawler object at 0x1e16b50>
        ...
        >>> response.url
        'http://example.org'

    메모: Scrapy engine 이 scrapy shell 에 점용 되 었 을 때 Scrapy shell 의 fetch 함 수 는 사용 할 수 없습니다.하지만 스 크 래 피 셸 에서 물 러 날 때 거 미 는 멈 춘 곳 에서 계속 기어 다 닌 다.  
    저자: 토목인 이 었 다 (http://blog.csdn.net/php_fly)
    원본 주소:http://blog.csdn.net/php_fly/article/details/19555969
    참고 글: Scrapy shell

    좋은 웹페이지 즐겨찾기