Using CSS Selectors

We have used plenty of properties and functions in the preceding sections, looking for desired elements and their content. Beautiful Soup also supports CSS Selectors (with library SoupSieve at https://facelessuser.github.io/soupsieve/selectors/), which enhances its use and allows developers to write effective and efficient codes to traverse the parsed tree. 

CSS Selectors (CSS query or CSS Selector query) are defined patterns used by CSS to select HTML elements, by element name or by using global attributes (ID, Class). For more information on CSS Selectors, please refer to Chapter 3, Using LXML, XPath and CSS Selectors, Introduction to XPath and CSS Selector section. 

For Beautiful Soup, the select() function is used to execute the CSS Selectors. We can perform the searching, traversing, and iteration of elements by defining CSS Selectors. The select() function is implemented individually, that is, it is not extended with other functions and properties found in Beautiful Soup, creating a chain of codes. The select() function returns a list of elements matched to the CSS Selectors provided. It's also notable that code using CSS Selectors are quite short in length compared to the code used in the preceding sections for a similar purpose. 

We will explore a few examples using select() to process CSS Selectors. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.55.151