UseSQL
  • Introduction
  • Getting Started
    • Source Formats
    • Output Formats
    • Managing Keys
    • Sandbox
    • Saved Queries
  • Client Libraries
    • HTML Templating
    • JavaScript Web Client
  • Tutorials
    • Integrate With Your Static Site
    • Share Data In Slack
    • Show external query results in a Google Sheet
    • Query across a Google Sheet and a CSV file
    • Securing Your Key
    • Querying Airtable
    • Integrate With ProcFu
    • Integrate with a private Google Sheet
    • Updating Google Sheets
    • Retrieving data from tables on a web page
    • Displaying reports in Google Sheets from MySQL
    • Querying StackBy Tables
  • Resources
    • Demo Sandbox
    • OpenAPI Docs
    • Redoc API Docs
    • Roadmap
    • Changelog
Powered by GitBook
On this page
  • Method 1: Table Index
  • Method 2: Table Class
  • Method 3: Table String Match
  • Method 4: Table Id

Was this helpful?

  1. Tutorials

Retrieving data from tables on a web page

PreviousUpdating Google SheetsNextDisplaying reports in Google Sheets from MySQL

Last updated 4 years ago

Was this helpful?

In order to query tables within web pages, you have a number of options. The examples illustrated below are querying some sample tables on . Each example links back to the demo sandbox for you to try yourself. By default, the first table on the page is retrieved, but you can query for specific tables within a page with four different methods. So if a web page only has one table you can simply supply the web page's URL as shown below.

SELECT * FROM "https://www.usesql.com/samples/tables"

Method 1: Table Index

The easiest way to explore a page likely will be with this method. This is a zero-based index so the first table will be 0. You just need to append a hashtag, and the index enclosed with square brackets. Below is an example using this method.

SELECT * FROM "https://www.usesql.com/samples/tables#[0]"

Here is another example of getting the second table on the webpage.

SELECT * FROM "https://www.usesql.com/samples/tables#[1]"

Method 2: Table Class

You can also select a table using its class attribute as defined in its html. In order to this you just need to append a hashtag, and a period preceding the class name. Below is an example using this method.

SELECT * FROM "https://www.usesql.com/samples/tables#.sample-table"

This method can be combined with using the table index so if there are two tables with the class sample-table you would use the query below.

SELECT * FROM "https://www.usesql.com/samples/tables#.sample-table[1]"

Method 3: Table String Match

Another way to choose a table that you want to query is to append #+ to the end of the website URL with a string that you see in the table.

SELECT * FROM "https://www.usesql.com/samples/tables#+Weekend"

Like with the html class attribute selector this method can be combined with selecting a table index as more than one table may have the string that's being supplied. The example below illustrates this.

Method 4: Table Id

If you own the page the most reliable way to query a specific table is likely to be this method. You can use a table's html attribute id to indicate to UseSQL which table you want to query. An example can be shown for a table that has an id="top-titles".

SELECT * FROM "https://www.usesql.com/samples/tables#+Title[1]"

SELECT * FROM "https://www.usesql.com/samples/tables#top-titles"

this page
[Example In Sandbox]
[Example In Sandbox]
[Example In Sandbox]
[Example In Sandbox]
[Example In Sandbox]
[Example In Sandbox]
[Example In Sandbox]
[Example In Sandbox]