Reveal By Response
We have pre-loaded a Response
object, named response
, with the content from a secret website. Your job is to figure out the URL and the title of the website using the response variable. You learned how to find the URL in the last lesson. To find the website title, what you need to know is:
- The title is the text from the
title
element - The
title
element is a child of thehead
element, which is a child of thehtml
root element.
To note: the html
root element only has one child head
element, and the head
element only has one child title
element.
This exercise is part of the course
Web Scraping in Python
Exercise instructions
- Assign to the variable
this_url
the URL used to load theresponse
variable. - Assign to the variable
this_title
the title of the website used to load theresponse
variable. Since we only want the text from the single element we will select, we use theextract_first()
method to extract the text. - Regardless of whether you use
xpath
orcss
, make sure that you are selecting the text within the title element, and not just the title itself.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Get the URL to the website loaded in response
this_url = ____
# Get the title of the website loaded in response
this_title = response.____.extract_first()
# Print out our findings
print_url_title( this_url, this_title )