okay now let's do the inverse of what we learned previously means this time we're 1
going to navigate down in the HTML tree using XPath so let's take as an example 2
this div element with a class attribute set to intro and previously to get all 3
the "p" elements that are inside it we used this XPath expression //div 4
with a class attribute set to intro and then /p now this /p is like a shortcut 5
of an axis called child which we use to get all the children of a specific 6
element which is this div in this case so alternatively we can replace the "p" by 7
an axis called child::p and of course we 8
still get the same output if we don't want to be specific about the children 9
elements we are looking for we can replace this "p" by the node() function we 10
learned about before now another example I want to show you is what if you want 11
to get all the elements that are listed after this "div" sorry that are listed 12
after this "div" in other words how can we get all the elements that are after this 13
closing tag well in XPath we have an axis called following which will 14
basically get us all the elements that are listed after a specific element so 15
let's replace child by following and as you can see we do get pretty much every 16
element listed after this div element even some sub elements like this "p" for 17
example and all the list items including the text. Now this div element its parent 18
is this body element right? so let's say we want to get all its brothers means 19
all the elements that are after this div which also share the same parent which is 20
this body to do that we can use an axis called following-sibling and like this 21
we do only get the elements that have the same parent of this div which 22
is the body element. Now one last axis and want to show you 23
is called descendant so again let's use this div as an example and let's say 24
want to get its children which are the "p" elements and we also want to get its 25
grand children means it children children like this span for example so 26
let's replace following sibling by 27
descendant now let's take a look at the output so we got its first child which 28
is this "p" element and we got its child children which are this text element 29
because it's considered as a grandchild and this span element with its text 30
element too and the same thing applies to this second child "p". So this was 31
everything for this video my advice to you is to repeat and repeat everything 32
we did in this video I really encourage you to make yourself comfortable using 33
XPath because it's really mandatory to do web scraping of course I'm gonna 34
add an assignment to this section to help you practice and memorize everything so 35
make sure to solve the assignment