Scraping with Children
We did a cute trick in the lesson to calculate how many children there were of one of the div elements belonging to the class course-block. Here we ask you to find the number of children of a mystery element (already stored within a Selector object, so you can use the xpath or css method).
To be explicit, we have created the Selector object mystery in the following way:
- We first loaded a
Responsevariable using a secret website as the input. - Then we used a call to the
xpathmethod to create aSelectorListof elements (but we won't say which ones) - Finally, we let
mysterybe the firstSelectorobject of thisSelectorList.
This exercise is part of the course
Web Scraping in Python
Exercise instructions
Fill in the blank below to chain on a call to
xpathso that we can calculate the number of children of the mystery element; we assign this number to the variablehow_many_kids.- Remember, if you use
xpath, this really is an instance of chaining, so don't forget to use a period (.) as glue.
- Remember, if you use
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Calculate the number of children of the mystery element
how_many_kids = len( mystery.xpath( ____ ) )
# Print out the number
print( "The number of elements you selected was:", how_many_kids )