HTML Structure and CSS Selectors
Understand DOM trees and write CSS selectors to target elements.
The HTML DOM Tree
Every web page is structured as a Document Object Model (DOM) tree. HTML elements nest inside each other forming a parent-child hierarchy. Web scraping navigates this tree to extract data.
# HTML structure conceptually:
# <html>
# <body>
# <div class='container'>
# <h1 id='title'>Hello</h1>
# <p class='text'>World</p>
# </div>
# </body>
# </html>
# rvest lets us query this tree with CSS selectors
library(rvest)
html <- read_html('<div><h1>Title</h1><p class="info">Text</p></div>')
html_text(html_element(html, 'h1'))CSS Selector: Tag Selector
The simplest CSS selector targets elements by their tag name. Writing p selects all paragraph elements. Writing h1 selects all level-1 headings.
library(rvest)
html <- read_html('
<div>
<p>First paragraph</p>
<p>Second paragraph</p>
<h2>A heading</h2>
</div>
')
# Tag selector: selects all <p> elements
nodes <- html_elements(html, 'p')
html_text2(nodes)
# Returns: c('First paragraph', 'Second paragraph')All lessons in this course
- HTML Structure and CSS Selectors
- html_element() and html_text() Basics
- Scraping Tables and Links
- Handling Pagination and Multiple Pages