html_element() and html_text() Basics
Extract text, attributes, and table data from scraped pages.
read_html(): Loading a Page
read_html() is the entry point for rvest. Pass a URL or a raw HTML string. It returns an xml_document object representing the parsed DOM tree, ready for querying.
library(rvest)
# From a URL (requires internet connection)
# page <- read_html('https://books.toscrape.com')
# From a raw HTML string (great for testing)
page <- read_html('
<html><body>
<h1>Book Store</h1>
<p class="desc">Over 1000 books!</p>
</body></html>
')
class(page) # 'xml_document' 'xml_node'html_element() vs html_elements()
html_element() returns the first matching node (or NA if none found). html_elements() returns all matching nodes as a list. Use singular when you expect exactly one result, plural when scraping lists.
library(rvest)
page <- read_html('
<ul>
<li class="item">Apple</li>
<li class="item">Banana</li>
<li class="item">Cherry</li>
</ul>
')
# Singular: gets first match
first_item <- html_element(page, '.item')
html_text2(first_item) # 'Apple'
# Plural: gets all matches
all_items <- html_elements(page, '.item')
html_text2(all_items) # c('Apple', 'Banana', 'Cherry')
length(all_items) # 3All lessons in this course
- HTML Structure and CSS Selectors
- html_element() and html_text() Basics
- Scraping Tables and Links
- Handling Pagination and Multiple Pages