XPath Chaining
Selector
and SelectorList
objects allow for chaining when using the xpath
method. What this means is that you can apply the xpath
method over once you've already applied it. For example, if sel
is the name of our Selector
, then
sel.xpath('/html/body/div[2]')
is the same as
sel.xpath('/html').xpath('./body/div[2]')
or is the same as
sel.xpath('/html').xpath('./body').xpath('./div[2]')
The only catch is that you need to "glue together" the XPath pieces by using a period at the start of each subsequent XPath string (notice the periods we added to the XPath strings in our examples).
This is a part of the course
“Web Scraping in Python”
Exercise instructions
- Fill in the blank below to chain together two
xpath
calls which result in the same selection as
sel.xpath('//div/span/p[3]')
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Chain together xpath methods to select desired p element
sel.xpath( '//div' ).xpath( ____ )
This exercise is part of the course
Web Scraping in Python
Learn to retrieve and parse information from the internet using the Python library scrapy.
Leverage XPath syntax to explore scrapy selectors. Both of these concepts will move you towards being able to scrape an HTML document.
Exercise 1: XPathologyExercise 2: Counting Elements in the WildExercise 3: Body AppendagesExercise 4: Choose DataCamp!Exercise 5: Off the Beaten XPathExercise 6: Where it's @Exercise 7: Check your ClassExercise 8: Hyper(link) ActiveExercise 9: Secret LinksExercise 10: Selector ObjectsExercise 11: XPath ChainingExercise 12: Divvy Up This ExerciseExercise 13: The Source of the SourceExercise 14: Course Class by InspectionExercise 15: Requesting a SelectorWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.