Introduction to XPath 1, parsing in XML and HTML using path expressions
2, including standard function path (all libraries support the same XPath syntax)
3,W3C Standard node:
1 <Body>First node:<HTML>2 <a> <Head>And<Body>;<a>And<Div>;<H1>And<H2>for the sibling node3 <Div> <H1>Is</span>Parent node, the same<span>Is<H1>the child nodes4 <H1>Because<span>Is<H1>The child nodes,<H1>Is<Div>The child nodes, so<Div>Is<span>5 <span>Ancestor node, the same as<span>Is<Div>descendants of a node6 </span>7 </H1>8 <H2>9 </H2>Ten </Div> One </a> A </Body>
Basic syntax: the example element tag is artical label one: artical Select all artical element's child nodes/artical Select the root element artical
ARTICAL/A Select all child element a elements that belong to artical
Div selects all div child elements, no matter where div is
Artical//div selects all div elements that belong to artical, regardless of where the div element is in artical
@class Select all attributes that are named class.
Second: predicate
/ARTICAL/DIV[1] Select all the first div elements that belong to the artical child element
/artical/div[last ()] Selects all the last elements that belong to the artical child element
/artical/div[last ()-1] Select the lowest 2 elements that belong to the artical child element
div[@lang] Select all elements that have a property of Lang
div[@lang = "eng") Select the element under all div with the lang attribute as Eng
Three:
/div/* Select all child nodes that belong to the DIV element
* Select all elements
div[@*] Select all title elements with attributes
div/a|//div/p Select A and P elements for all DIV elements
span | UL selects all elements of Span and UL in the document
Artical/div/pl|//span Select Pl under All div and all spans in the document
Precautions:
1) in accordance with the review elements of the wording is not necessarily correct, according to the source of the Web page.
Because not the same, the Web page source code is what you see
2) The browser has its own copy XPath function, review elements you try, different browsers different methods
3) XPath has a speed of c, so the accuracy is higher according to [@class = "]
Python Basics------XPath Basics