Python Scrapy 튜토리얼 중 질문
조회수 1970회
안녕하세요. 스크래피 튜토리얼을 따라하며 공부 중에 모르는 부분이 있어 질문 드립니다. 코드는 아래와 같습니다.
def parse(self, response):
for href in response.css("ul.directory.dir-col > li > a::attr('href')"):
url = response.urljoin(href.extract())
yield scrapy.Request(url, callback=self.parse_dir_contents)
위 함수코드 중 ul.directory.dir-col > li > a::attr('href')와 callback=self.parse_dir_contents에 대한 질문 인데요.
첫번째 css 경로인 것 같은데 ">" 기호로 "/"를 대신 한 것인지 궁금합니다. 그리고 callback=self.parse_dir_contents 이 부분이 이해가 잘 되지 않는데 간단하게 개념 설명 해주시면 감사하겠습니다. ^
인터넷 검색과 메뉴얼을 찾아보고 있는데 잘 모르겠네요. 답답한 맘에 질문 드립니다.
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
첫번째 ul.directory.dir-col > li > a::attr('href')
위는 CSS Selector 입니다.
CSS Selector에서 E>F 와 같이 표현하면, F는 E의 자식 노드를 의미합니다. F가 여러개 있겠지만, 꼭 E 밑에 있는 F만 선택하는다는 의미입니다.
두번째 callback=self.parse_dir_contents
참고3을 참고해보면
Now the parse() method only extract the interesting links from the page, builds a full absolute URL using the response.urljoin method (since the links can be relative) and yields new requests to be sent later, registering as callback the method parse_dir_contents() that will ultimately scrape the data we want. What you see here is the Scrapy’s mechanism of following links: when you yield a Request in a callback method, Scrapy will schedule that request to be sent and register a callback method to be executed when that request finishes.
url에 있는 문서에 포함된 추가적인 링크들을 찾기 위한 함수인 것 같습니다. 위 참고3의 예제에서 보면, url로 부터 얻은 response에서 하위 링크 정보를 추출하고, 다음 크롤링 대상으로 예약하는 것으로 보입니다.
댓글 입력