Database |> Language |> 

CSS Selectors

CSS selectors help you transform the pixels you see in your browser into fields that integrate right into your applications (yes it’s not xpath but it gets the job done surprisingly well).

Contents

  1. How do CSS selectors work in SQL?
  2. Getting specific elements
  3. Getting attributes

How do CSS selectors work in SQL?

The columns you SELECT for in LSD are CSS selectors; this is in contrast to an ordinary SELECT statement where the columns are fields in a table.

SELECT <column_name> FROM <table_name>;

In LSD the equivalent looks like:

SELECT <css_selector> FROM <url>;

For example, if you were interested in querying for the tagline on our current landing page, you’d be looking for the text in the element that matches the h1 selector.

SELECT
  h1 AS tagline
FROM
  https://lsd.so;

Getting specific elements

When you want to pluck more granularly, you can use attribute selectors to match elements that abide by a particular pattern like lobotomized owls.

* + *

For example, suppose we were interested in getting links from the front page of Hacker News that belong to something hosted on GitHub, we could do that using a prefix selector.

SELECT
  a@href
FROM
  https://news.ycombinator.com
GROUP BY
  a[href^="https://github.com"]

Getting attributes

For information on getting the value of an attribute belonging to a SELECT’ed element, see attributes.