Selector 요청하기
문자열 변수 url에는 특정 웹사이트의 URL이 미리 저장되어 있고, requests 라이브러리를 사용해 웹사이트의 콘텐츠를 문자열 변수 html에 담아 두었습니다. 여러분의 과제는 html에 저장된 HTML 소스 코드를 사용해 Selector 객체 sel을 만드는 것입니다.
이 연습은 강의의 일부입니다
Python으로 하는 웹 스크레이핑
연습 안내
- 아래의 두 빈칸을 채워, 문자열
html을 입력 텍스트로 사용하는Selector객체sel을 생성하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Import a scrapy Selector
from scrapy import ____
# Import requests
import requests
# Create the string html containing the HTML source
html = requests.get( url ).content
# Create the Selector object sel from html
sel = Selector( ____ )
# Print out the number of elements in the HTML document
print( "There are 1020 elements in the HTML document.")
print( "You have found: ", len( sel.xpath('//*') ) )