Read the HTML tree of BeautifulSoup Official document search (2)

Source: Internet
Author: User

In addition to find () and Find_all (), there are a number of similar methods provided here I would like to say, the parameters and the use of the same, the last four is next, previous is in. next/previous_element ().

Signature:find_parents (name, attrs, string, limit, **kwargs)

Signature:find_parent (name, attrs, string, **kwargs)

Signature:find_next_siblings (name, attrs, string, limit, **kwargs )

Signature:find_next_sibling (name, attrs, string, **kwargs)

Signature:find_previous_siblings (name, attrs, string, limit, * * Kwargs)

Signature:find_previous_sibling (name, attrs, string, **kwargs)

Signature:find_all_next (name, attrs, string, limit, **kwargs)

Signature:find_next (name, attrs, string, **kwargs)

Signature:find_all_previous (name, attrs, string, limit, **kwargs )

Signature:find_previous (name, attrs, string, **kwargs)

BeautifulSoup also provides CSS selectors, the use of roughly the same as the CSS selector, my CSS is only the entry level, here is not much to explain ...:

1Soup.select ("title")2 #[<title>the dormouse ' s story</title>]3 4Soup.select ("p Nth-of-type (3)")5 #[<p class= "story" >...</P>]6 7Soup.select ("Body a")8 #[<a class= "sister" href= "Http://example.com/elsie " id= "Link1" >ELSIE</A>9 #<a class= "sister" href= "Http://example.com/lacie " id= "Link2" >LACIE</A>Ten #<a class= "sister" href= "Http://example.com/tillie" id= "Link3" >TILLIE</A>] One  ASoup.select ("HTML Head title") - #[<title>the dormouse ' s story</title>] -  theSoup.select ("head > Title") - #[<title>the dormouse ' s story</title>] -  -Soup.select ("p > a") + #[<a class= "sister" href= "Http://example.com/elsie " id= "Link1" >ELSIE</A> - #<a class= "sister" href= "Http://example.com/lacie " id= "Link2" >LACIE</A> + #<a class= "sister" href= "Http://example.com/tillie" id= "Link3" >TILLIE</A>] A  atSoup.select ("p > A:nth-of-type (2)") - #[<a class= "sister" href= "Http://example.com/lacie " id= "Link2" >LACIE</A>] -  -Soup.select ("p > #link1") - #[<a class= "sister" href= "Http://example.com/elsie " id= "Link1" >ELSIE</A>] -  inSoup.select ("Body > a") - # [] to  + #The above seems to understand, should be > Words is must be children, space words to show descendants. -  theSoup.select ("#link1 ~. Sister") * #[<a class= "sister" href= "Http://example.com/lacie " id= "Link2" >LACIE</A> $ #<a class= "sister" href= "Http://example.com/tillie" id= "Link3" >TILLIE</A>]Panax Notoginseng  -Soup.select ("#link1 +. Sister") the #[<a class= "sister" href= "Http://example.com/lacie " id= "Link2" >LACIE</A>] +  ASoup.select (". Sister") the #[<a class= "sister" href= "Http://example.com/elsie " id= "Link1" >ELSIE</A> + #<a class= "sister" href= "Http://example.com/lacie " id= "Link2" >LACIE</A> - #<a class= "sister" href= "Http://example.com/tillie" id= "Link3" >TILLIE</A>] $  $Soup.select ("#link1") - #[<a class= "sister" href= "Http://example.com/elsie " id= "Link1" >ELSIE</A>] -  theSoup.select ("A#link2") - #[<a class= "sister" href= "Http://example.com/lacie " id= "Link2" >LACIE</A>]Wuyi  the #The following seems to be looking through the ID: -Soup.select ("#link1") Wu #[<a class= "sister" href= "Http://example.com/elsie " id= "Link1" >ELSIE</A>] -  AboutSoup.select ("A#link2") $ #[<a class= "sister" href= "Http://example.com/lacie " id= "Link2" >LACIE</A>] -  - #match any one -Soup.select ("#Link1, #link2 ") A #[<a class= "sister" href= "Http://example.com/elsie " id= "Link1" >ELSIE</A> + #<a class= "sister" href= "Http://example.com/lacie" id= "Link2" >LACIE</A>] the  - #Of course, you can use the value of the property to match $Soup.select ('a[href= "Http://example.com/elsie"]') the #[<a class= "sister" href= "Http://example.com/elsie " id= "Link1" >ELSIE</A>] the  theSoup.select ('a[href^= "http://example.com/"]') the #[<a class= "sister" href= "Http://example.com/elsie " id= "Link1" >ELSIE</A> - #<a class= "sister" href= "Http://example.com/lacie " id= "Link2" >LACIE</A> in #<a class= "sister" href= "Http://example.com/tillie" id= "Link3" >TILLIE</A>] the  theSoup.select ('a[href$= "Tillie"]') About #[<a class= "sister" href= "Http://example.com/tillie " id= "Link3" >TILLIE</A>] the  theSoup.select ('a[href*= ". Com/el"]') the #[<a class= "sister" href= "Http://example.com/elsie " id= "Link1" >ELSIE</A>] +  - #I can't read this. theMultilingual_markup ="""Bayi <p lang= "en" >Hello</p> the <p lang= "en-us" >howdy, y ' all</p> the <p lang= "EN-GB" >pip-pip, old fruit</p> - <p lang= "fr" >bonjour mes amis</p> - """ theMultilingual_soup =BeautifulSoup (multilingual_markup) theMultilingual_soup.select ('p[lang|=en]') the #[<p lang= "en" >HELLO</P> the #<p lang= "en-us" >howdy, y ' all</p> - #<p lang= "EN-GB" >pip-pip, old Fruit</p>] the  the #choose one to use Select_one () theSoup.select_one (". Sister")94 #<a class= "sister" href= "Http://example.com/elsie " id= "Link1" >Elsie</a>

Read the HTML tree of BeautifulSoup Official document search (2)

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.