liveBook · Manning

Structured extraction

As explained in D3.js in Action, Third Edition

Data extraction is the process of obtaining selected information from a source such as a web page or document.

Structured extraction selects useful fields from a web page or document and reshapes them into data ready for a custom visualization. It replaces tedious manual copying with either a quick table-conversion service or a script when finer control matters. section 14.1

A practical extraction workflow:

  • Map source structures to output categories, such as assigning painting-table indexes to oil, watercolor, or print mediums. section 14.1
  • Select matching HTML tables with document.querySelectorAll(), then iterate through their rows. section 14.1
  • Collect only the fields needed for the visualization, including each painting’s title, image URL, and medium. section 14.1

Van Gogh painting data

A Wikipedia page contains more than a thousand paintings across HTML tables. A browser-console script extracts selected paintings for a timeline of Van Gogh’s final ten years while leaving letter sketches aside. section 14.1

Listing 14.1 Extracting data from Van Gogh’s paintings on Wikipedia
const tables = [                         #1
  { index: 0, medium: "oil" },          
  { index: 1, medium: "oil" },          
  { index: 2, medium: "oil" },          
  { index: 3, medium: "oil" },          
  { index: 4, medium: "oil" },          
  { index: 5, medium: "oil" },                                           
  { index: 6, medium: "watercolor" },                                    
  { index: 7, medium: "print" },                                         
  { index: 8, medium: "print" },                                         
];                                                                       

const domTables = document.querySelectorAll(     #2
  ".wikitable.sortable"                         
);                                              
const paintings = [];     #3

domTables.forEach((table, i) => {                                        #4
  if (i <= tables.length - 1) {                                        
    const medium = tables[i].medium;                                    
                                                                        
    const rows = table.querySelectorAll("tbody tr");                  
    rows.forEach(row => {                                              
      let title;                                                         #5
      if (row.querySelector(".thumbcaption i a")) {                    
        title = row.querySelector(".thumbcaption i a")                  
          .textContent;                                                 
      } else if (row.querySelector(".thumbcaption i")) {                
        title = row.querySelector(".thumbcaption i")                    
          .textContent;                                                 
      } else {                                                         
        title = null;                                                   
      }                                                                

      let imageLink;                                                     #6
      if (row.querySelector(".thumbinner img")) {                      
        const image = row.querySelector(                                
          ".thumbinner img"                                             
        ).srcset;                                                       
        imageLink = `https${image.slice(                              
          image.indexOf("1.5x, ")+6,-3                                  
        )}`;                                                          
      } else {                                                         
        imageLink = null;                                             
      }                                                               

      paintings.push({                                                  #7
        title: title,                                                 
        imageLink: imageLink,                                          
        medium: medium                                                  
      });                                                               
    })
  }
});

console.log(paintings);                                             #8

Use the complete listing to see table classification, DOM selection, and the start of the paintings collection implemented in JavaScript. section 14.1

How D3.js in Action, Third Edition covers it

How deep it goes
Demonstrates table classification, DOM selection, row iteration, and field collection through a browser-console script.
Examples
Uses Van Gogh painting tables from Wikipedia and extracts titles, image URLs, and mediums for a timeline.
What it assumes
Uses browser-console JavaScript, CSS selectors, HTML tables, and basic DOM traversal.
What you can do after
Build it from scratch by extracting selected records from HTML tables in a browser console.
How it connects
Connects web scraping with the DOM, HTML tables, JavaScript, and data visualization.

Also explained in

Compare how 4 books explain structured extraction →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.