CodeGym /Courses /Python SELF EN /Introduction to CSS

Introduction to CSS

Python SELF EN
Level 30 , Lesson 0
Available

1. CSS Basics for Web Scraping

For successful web scraping, understanding the HTML structure and CSS classes on a page is key. By knowing how page elements are styled and structured with CSS, you can more precisely target the data you need and extract it. Let’s check out how linking CSS to HTML, using selectors, and attributes like style, class, id, and name can help you work with the structure of web pages for scraping.

CSS is responsible for the styling of web pages, but for web scraping purposes, we can think of CSS as a tool to understand the structure and target elements. Let’s explore the key CSS concepts important for scraping:

  • Selectors — these are rules that point to specific HTML elements. Using them helps to precisely identify the desired data.
  • Attributes class, id, and name — these are unique identifiers helping to highlight and distinguish elements. For scraping, they’re super useful since they help isolate the needed elements, making data extraction easier.

2. Linking CSS to an HTML Document

CSS can be linked to HTML in different ways. It’s important to understand these methods to navigate through elements and identify their styles and classes since this helps you isolate the target data.

External File

CSS is often linked as an external file, which you can spot in an HTML document via the <link> tag in the <head> section. External CSS files define styles for the entire page, including identifiers and classes, making navigation easier when scraping.

HTML

<head>
    <link rel="stylesheet" href="styles.css">
</head>
    

Internal Styles

Sometimes styles are defined within the page using the <style> tag. Internal styles can be found in the <head> section of a page and act as hints for understanding classes and identifiers to target the elements you need.

HTML

<head>
<style>
  .price {
    color: red;
  }
</style>
</head>

Inline Styles (Attribute style)

Inline styles are found directly in HTML tags and affect only specific elements. The style attribute often contains unique properties that can be helpful for identifying target data.

HTML

<p style="color: red; font-size: 18px;">Text with inline style</p>
HTML

<p style="color: red; font-size: 18px;">Text with inline style</p>

3. Selectors in CSS

CSS selectors are used for applying styles to elements, but for web scraping, their main use is precisely choosing elements containing the data you need. Let’s go over the main selector types that can be used when scraping.

Main Types of Selectors

Tag Selector: This selector targets all elements of a specific tag (e.g., <p> or <div>). In web scraping, tag selectors are helpful for extracting info from tags that may contain text, images, or other data.

CSS

p {
  color: blue;
}
    

Class Selector: This selector targets elements with a specific class attribute value. A class is marked with a dot (.) before its name. In scraping, classes are super handy because they can identify elements that share the same style, like a product list.

CSS
      
        .price {
            color: red;
          }
      
    
CSS
      
        .price {
            color: red;
          }
      
    
HTML
      
        <p class="price">Price: $99</p>
      
    

ID Selector: This selector targets an element with a unique id attribute, marked with a #. In scraping, id is super useful for selecting unique elements, like a headline or button on a page.

CSS

    #product-title {
        font-size: 24px;
      }
CSS

    #product-title {
        font-size: 24px;
      }
HTML
  
    <h1 id="product-title">Product Name</h1>
  
1
Task
Python SELF EN, level 30, lesson 0
Locked
Basics of CSS Linking
Basics of CSS Linking
2
Task
Python SELF EN, level 30, lesson 0
Locked
Internal and Inline Styles
Internal and Inline Styles
3
Task
Python SELF EN, level 30, lesson 0
Locked
Combined Selectors
Combined Selectors
4
Task
Python SELF EN, level 30, lesson 0
Locked
Data extraction using attribute selectors
Data extraction using attribute selectors
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION