Requesting a Selector
We have pre-loaded the URL for a particular website in the string variable url
and use the requests library to put the content from the website into the string variable html
. Your task is to create a Selector
object sel
using the HTML source code stored in html
.
This exercise is part of the course
Web Scraping in Python
Exercise instructions
- Fill in the two blanks below to assign to create the
Selector
objectsel
which uses the stringhtml
as the text it inputs.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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('//*') ) )