What is CSS?
CSS (Cascading Style Sheets) acts as the clothing of a web page, specifying its layout, colors, fonts, and overall design.
CSS Selectors are patterns used to select specific elements within an HTML document composed of multiple elements.
In web crawling, CSS selectors help quickly identify and extract relevant HTML elements.
Basic CSS Selectors
1. Element Selector
-
Selects elements based on their HTML tag name.
-
Example: The
pselector applies styles to all<p>elements.
<p>paragraph 1</p> <p>paragraph 2</p>
p { color: blue; }
2. Class Selector
-
Uses
.to select elements with a specific class -
Example: The
.classnameselector selects all elements that has the class name "classname".
<p class="classname">paragraph 1</p> <p class="classname">paragraph 2</p>
.classname { background-color: yellow; }
3. ID Selector
-
Uses
#to select elements with a specific ID. -
Example: The
#idnameselector selects the element with the ID "idname".
<p id="idname">paragraph 1</p>
#idname { font-size: 24px; }
4. Attribute Selector
-
Uses
[attribute=value]to select elements with a specific attribute. -
Example: The
[type="text"]selector selects all elements with thetype="text"attribute.
<input type="text">
input[type='text'] { border: 1px solid black; }
Advanced CSS Selectors
1. Descendant Selector
-
Selects elements nested within a specified parent element.
-
Example:
div pselects all<p>elements inside<div>.
<div> <p>paragraph 1</p> <p>paragraph 2</p> </div> <p>paragraph 3</p>
div p { color: red; }
2. Child Selector
-
Uses
>to select only direct child elements of a specified element. -
Example:
ul > liselects only the direct child<li>elements of<ul>.
<ul> <li>list 1</li> <li>list 2</li> </ul>
ul > li { color: green; }
Applying CSS Selectors in BeautifulSoup
In the BeautifulSoup library, CSS selectors can be used with the soup.select method.
from bs4 import BeautifulSoup html = """ <html> <body> <div id="wrap"> <h1 class="title">h1</h1> <p class="content">paragraph 1</p> <p class="content">paragraph 2</p> </div> </body> </html> """ soup = BeautifulSoup(html, 'html.parser') # Select all elements with the class name "content" content_elements = soup.select('.content') print(content_elements)
For more detailed content on CSS, visit the Intro to CSS Course.
Practice
Click the Run Code button on the right-hand side of the screen to view crawling results or edit the code!
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help
Code Editor
Execution Result