请求一个 Selector
我们已将某网站的 URL 预先加载到字符串变量 url 中,并使用 requests 库将该网站的内容放入字符串变量 html。您的任务是使用存储在 html 中的 HTML 源码创建一个 Selector 对象 sel。
本练习是课程的一部分
Python Web 爬取
练习说明
- 在下面的两个空白处填写代码,创建
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('//*') ) )