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 efficiently locate HTML elements containing desired information.
Basic CSS Selectors
1. Element Selector
-
Uses the name of an HTML element to select it.
-
Example: The
p
selector selects 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
.classname
selector selects all elements with 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
#idname
selector 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; }
Composite Selectors
1. Descendant Selector
-
Uses a space between elements to select descendant elements.
-
Example:
div p
selects 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 > li
selects only the direct child<li>
elements of<ul>
.
<ul> <li>list 1</li> <li>list 2</li> </ul>
ul > li { color: green; }
Using CSS Selectors with 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 side of the screen to view crawling results or modify the code!
Lecture
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help
Code Editor
Execution Result