Css/xpath selector number of child nodes/parent/sibling nodes

Source: Internet
Author: User
Tags xpath

0. Refer to 1. initialization
 from Import Selectorin [326]: text="" "...: <div> ...:     <a>1a</a> ...     : <p>2p</p> ...:     <p>3p</p> ...     : </div> "" " In [327]: sel=selector (text=text) in [print(sel.extract ())
2. Parent node/previous next sibling node
In [329]: Sel.xpath ('//a/Parent::*/p'). Extract () out[329]: ['<p>2p</p>','<p>3p</p>']in [[+]: Sel.xpath ('//p/preceding-sibling::a'). Extract () out[330]: ['<a>1a</a>']in [331]: Sel.xpath ('//a/following-sibling::p'). Extract () out[331]: ['<p>2p</p>','<p>3p</p>']
3.CSS number of sub-nodes 3.1 general
# Full child node list , counting from the first child node, and satisfying child node tag qualificationIn [332]: Sel.css ('a: Nth-child (1) '). Extract () out[332]: ['<a>1a</a>']# complete list of child nodes, counting from the last child node, and satisfying the child node tag limitIn [333]: Sel.css ('a: Nth-last-child (1) '). Extract () out[333]: []in [334]: Sel.css ('p:nth-child (1)'). Extract () out[334]: []in [335]: Sel.css ('P:nth-child (2)'). Extract () out[335]: ['<p>2p</p>']in [336]: Sel.css ('P:nth-child (3)'). Extract () out[336]: ['<p>3p</p>']in [337]: Sel.css ('p:nth-last-child (1)'). Extract () out[337]: ['<p>3p</p>']in [338]: Sel.css ('P:nth-last-child (2)'). Extract () out[338]: ['<p>2p</p>']in [339]: Sel.css ('P:nth-last-child (3)'). Extract () out[339]: []
3.2 Special Reference
In [340]: Sel.css ('a: First-child '). Extract () out[340]: ['<a>1a</a>']in [341]: Sel.css ('a: Last-child '). Extract () out[341]: []in [342]: Sel.css ('P:first-child'). Extract () out[342]: []in [343]: Sel.css ('P:last-child'). Extract () out[343]: ['<p>3p</p>']
3.3 The above-child is modified to-of-type, and only the corresponding list of sub-nodes after filtering is counted 4. Xpath Number of child nodes
In [344]: Sel.xpath ('//div'). Extract () out[344]: ['<div>\n <a>1a</a>\n <p>2p</p>\n <p>3p</p>\n</div>']in [345]: Sel.xpath ('//div/*'). Extract () out[345]: ['<a>1a</a>','<p>2p</p>','<p>3p</p>']in [346]: Sel.xpath ('//div/node ()'). Extract () out[346]: ['\ n','<a>1a</a>','\ n','<p>2p</p>','\ n','<p>3p</p>','\ n']in [347]: Sel.xpath ('//div/a'). Extract () out[347]: ['<a>1a</a>']in [348]: Sel.xpath ('//div/p'). Extract () out[348]: ['<p>2p</p>','<p>3p</p>']in [349]:in [349]: Sel.xpath ('//div/a[1]'). Extract () out[349]: ['<a>1a</a>']in [[Sel.xpath]:'//div/a[last ()]'). Extract () out[350]: ['<a>1a</a>']in [351]:in [351]: Sel.xpath ('//div/p[1] '). Extract ()#the equivalent of a filtered list of child nodesOUT[351]: ['<p>2p</p>']in [352]: Sel.xpath ('//div/p[Last ()] '). Extract () out[352]: ['<p>3p</p>']in [353]: Sel.xpath ('//div/p[Last ()-1] '). Extract () out[353]: ['<p>2p</p>']in [354]:in [354]: Sel.xpath ('//div/*[1]'). Extract ()#Full child node listOUT[354]: ['<a>1a</a>']in [355]: Sel.xpath ('//div/*[last ()]'). Extract () out[355]: ['<p>3p</p>']in [356]:in [356]: Sel.xpath ('//div/node () [1]'). Extract ()#include plain textOUT[356]: ['\ n']in [357]: Sel.xpath ('//div/node () [Last ()]'). Extract () out[357]: ['\ n']

Css/xpath selector number of child nodes/parent/sibling nodes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.