Lecture

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.

HTML: paragraph 1, paragraph 2
<p>paragraph 1</p> <p>paragraph 2</p>
CSS: select paragraph 1, paragraph 2
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".

HTML: Two p elements with class name classname
<p class="classname">paragraph 1</p> <p class="classname">paragraph 2</p>
CSS
.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".

HTML: A p element with ID idname
<p id="idname">paragraph 1</p>
CSS: select element with ID idname
#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 the type="text" attribute.

HTML: input element with type attribute text
<input type="text">
CSS: select input elements with type attribute 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>.

HTML: 2 p elements within div
<div> <p>paragraph 1</p> <p>paragraph 2</p> </div> <p>paragraph 3</p>
CSS: do not select paragraph 3
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>.

HTML: 2 direct child li elements within ul
<ul> <li>list 1</li> <li>list 2</li> </ul>
CSS: select list 1, list 2
ul > li { color: green; }

Using CSS Selectors with BeautifulSoup

In the BeautifulSoup library, CSS selectors can be used with the soup.select method.

Example of using CSS selectors in BeautifulSoup
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

Run
Generate

Execution Result