始める無料で始める

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('//*') ) )
コードを編集して実行