Selector をリクエストする
文字列変数 url に特定のウェブサイトの URL を読み込み、requests ライブラリでそのサイトのコンテンツを取得して、文字列変数 html に格納してあります。あなたのタスクは、html に保存された HTML ソースコードを使って、Selector オブジェクト sel を作成することです。
この演習はコースの一部です
Pythonで学ぶWebスクレイピング
演習の手順
- 下の空欄2か所を埋めて、文字列
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('//*') ) )