Skip to main content
All CollectionsRayn Air Rayn Air (Rayn JS)
How to point your RaynJS at the right css selectors content
How to point your RaynJS at the right css selectors content

Understand how to find the right css selectors for content ingestion

Niels Baarsma avatar
Written by Niels Baarsma
Updated over a week ago

To maximize the effectiveness of Rayn’s contextual segmentation, it’s crucial to accurately target the unique content on your web pages. RaynJS relies on CSS selectors to locate and ingest the content that needs to be contextualized. This article will guide you through the process of finding the appropriate CSS selectors using the “Inspect” feature in your web browser.

What is a CSS Selector

CSS, or Cascading Style Sheets, is a style sheet language used to define the presentation of HTML documents. It allows developers to enhance the visual appeal of web pages by controlling layout, colors, fonts, and other design elements. CSS is integral to modern web development, enabling websites to provide a more engaging and user-friendly experience.

A CSS selector is a pattern that identifies the HTML elements you want to style or manipulate. It tells the browser which elements to apply specific styles to, making it possible for different headers to have unique appearances or for visited links to look different from unvisited ones. CSS selectors are essential not only for styling but also for selecting specific content within a page, which is crucial for tasks like content ingestion with RaynJS. They allow you to precisely target elements, ensuring that styles or scripts affect only the intended parts of a web page.

Step-by-Step Guide

1. Open the Web Page

Navigate to the web page containing the content you want RaynJS to ingest.

2. Access the Developer Tools

Google Chrome / Microsoft Edge:

  • Right-click on the element containing your unique content.

  • Select “Inspect” from the context menu.

Mozilla Firefox:

  • Right-click on the element.

  • Select “Inspect Element”.

3. Locate the Desired Element

The Developer Tools panel will open, highlighting the HTML element you selected. This element should encapsulate the unique content you wish to target.

4. Examine the HTML Structure

Look for unique identifiers such as id or class attributes that you can use in your CSS selector.

  • IDs (id): Unique identifiers for elements. Prefixed with # in CSS selectors.

  • Classes (class): Can be shared among multiple elements. Prefixed with . in CSS selectors.

Example HTML:

<div id="main-content" class="article-body">
<!-- Unique content goes here -->
</div>

5. Determine a Unique CSS Selector

  • Prefer IDs Over Classes: IDs are unique on a page, making them ideal for precise targeting.

  • Example Selector: #main-content

  • Use Class Combinations: If IDs are not available, combine classes to create a more specific selector.

  • Example Selector: .article-body .content-section

  • Avoid Generic Selectors: Stay away from common classes like .container or .row that may select multiple elements.

6. Test the CSS Selector

  • Go to the Rayn Management Console.

  • Enter the CSS selector(s) you've found.

  • Enter the URL you wish to test and hit Preview.

  • Rayn will create a screenshot of the URL and will circle the content that was selected with the given CSS selector(s).

Best Practices

  • Ensure Uniqueness: The selector should point to content unique to each page to improve segmentation accuracy.

  • Consistency Across Pages: If deploying RaynJS on multiple pages, use selectors that are consistent or adjust them as needed.

  • Dynamic Content Caution: Be wary of elements that change dynamically, as they may affect content ingestion.

Example Scenario

Suppose your article content is structured as follows:

<article class="post">
<header>
<h1>Article Title</h1>
</header>
<div class="post-content">
<!-- Unique content -->
</div>
</article>

A suitable CSS selector would be:

  • .post .post-content

This selector specifically targets the div containing your unique content within the article.

Common challenges

When using CSS selectors for content ingestion, you may encounter a few common challenges. While CSS selectors are a powerful tool for extracting data from web pages, they can sometimes present difficulties due to the variations and complexities of different target websites.

Here are some of the challenges that can arise when using CSS selectors for web scraping:

1. Dynamic Web Pages: Websites often employ dynamic content that is loaded after the initial page load. This can make it difficult to accurately select elements with static CSS selectors. Developers may need to use JavaScript or specialized tools to handle dynamic content effectively.

2. Nested or Complex HTML Structure: Websites with complex or nested HTML structures can make it challenging to target specific elements with CSS selectors. It may require writing more complex and specific selectors or using XPath instead.

3. Element Attribute Changes: CSS selectors rely on attributes to target web elements. However, if a website frequently changes element attributes or class names, it can break CSS selector-based scrapers. Developers should be aware of such changes and adapt their code accordingly.

4. Captchas and Anti-Scraping Techniques: Some websites implement captchas or other anti-scraping techniques to prevent automated data extraction. While not directly related to CSS selectors, they can pose significant challenges in web scraping. Developers may need to implement additional techniques to bypass these obstacles.

5. Data Across Multiple Pages: In situations where data is spread across multiple pages, navigating and scraping each page can be time-consuming. Developers need to handle pagination and make multiple HTTP requests to extract the desired information.

6. Site Layout Changes: Web page layouts often change over time, and such changes can affect CSS selectors used for web scraping. Notably, if a website undergoes a redesign, previously working selectors may become invalid. Regular maintenance and updates to the selectors are necessary to ensure the scraping scripts continue to function correctly.

CSS Selector Types

Though there are a lot of CSS selectors, you don’t really need to know all of them for Rayn JS content ingestion. This CSS selectors overview lists some that can help set up efficient content ingestion.

Selector

Example

Use Case Scenario

*

*

This selector picks all elements within a page. It’s not that different from a page. Not much use for it but still good to know

.class

.card-title

The simplest CSS selector is targeting the class attribute. If only your target element is using it, then it might be sufficient.

.class1.class2

.card-heading.card-title

There are elements with a class like class=“card-heading card-title”. When we see a space, it is because the element is using several classes. However, there’s no one fixed way of selecting the element. Try keeping the space, if that doesn’t work, then replace the space with a dot.

#id

#card-description

What if the class is used in too many elements or if the element doesn’t have a class? Picking the ID can be the next best thing. The only problem is that IDs are unique per element.

element

h4

To pick an element, all you need to add to the Rayn JS configuration is the HTML tag name.

element.class

h4.card-title

This is the most common type of selector used in Rayn JS content ingestion.

parentElement > childElement

div > h4

Instructs Rayn JS to extract an element inside another. In this example, it will try to find the h4 element whose parent element is a div.

parentElement.class > childElement

div.card-body > h4

Combines the previous logic to specify a parent element and extract a specific CSS child element. This is very useful when the data you want doesn’t have any class or ID but is inside a parent element with a unique class/ID.

[attribute]

[href]

Target an element with no clear class to choose from. Rayn JS will extract all elements containing the specific attribute. In this case, it will take all <a> tags which are the most common element to contain an href attribute.

[attribute=value]

[target=_blank]

Tells Rayn JS to extract only the elements with a specific value inside its attribute.

[attribute~=value]

[title~=rating]

This selector will pick all the elements containing the word ‘rating’ inside its title attribute.

Did this answer your question?