Python: Scrapy Shell 사용 튜 토리 얼
셸 을 사용 하면 다음 명령 을 사용 하여 셸 을 사용 할 수 있 습 니 다.
scrapy shell <url>
그 중에서 < url > 은 바로 당신 이 잡 고 싶 은 페이지 url 입 니 다.셸 사용 하기 Scrapy shell 은 몇 가지 유용 한 기능 함수 가 내 장 된 python 콘 솔 프로그램 으로 볼 수 있 습 니 다.
기능 함수
사용 가능 한 대상 은 다음 과 같 습 니 다.
우선 셸 을 시작 합 니 다.
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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
로마 숫자를 정수로 또는 그 반대로 변환그 중 하나는 로마 숫자를 정수로 변환하는 함수를 만드는 것이었고 두 번째는 그 반대를 수행하는 함수를 만드는 것이었습니다. 문자만 포함합니다'I', 'V', 'X', 'L', 'C', 'D', 'M' ; 문자열이 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.