開始使用免費開始

建立 Selector

我們已經把某個網站的 URL 放在字串變數 url,並用 requests 函式庫將該網站的內容放入字串變數 html。你的任務是使用儲存在 html 的 HTML 原始碼來建立一個 Selector 物件 sel

本練習屬於課程

Python 網頁爬蟲

檢視課程

練習說明

  • 在下方兩個空格中填入適當內容,建立 Selector 物件 sel,並讓它使用字串 html 作為輸入文字。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# 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('//*') ) )
編輯並執行程式碼