rvest

repository·main·Indexed 23 days ago

https://github.com/tidyverse/rvest

An R package for scraping and harvesting data from web pages. It provides tools for reading HTML and extracting data using CSS selectors and XPath, including functions like read_html(), html_elements(), and html_table() to convert HTML tables into data frames.

Tokens
663
Snippets
3
Records
4
Agent score
31%

What's inside rvest

  1. Best practices for scraping multiple pages

    main
    When scraping multiple pages, it is highly recommended to use rvest in conjunction with the polite package. The polite package helps ensure you are respecting robots.txt and avoids overwhelming websites with too many requests.
  2. Install rvest

    main

    You can install rvest either as part of the full tidyverse collection or as a standalone package using install.packages().

    # The easiest way to get rvest is to install the whole tidyverse:
    install.packages("tidyverse")
    
    # Alternatively, install just rvest:
    install.packages("rvest")
  3. Scrape web pages with rvest

    main

    The standard workflow for scraping data with rvest involves three steps:

    1. Read the HTML: Use read_html() to load a web page.
    2. Select elements: Use html_elements() to find all nodes matching a CSS selector or XPath expression, or html_element() to find a single node.
    3. Extract data: Use functions like html_text2() for text content or html_attr() for attribute values.

    rvest is designed to work with magrittr pipes (|>) for easy expression of scraping tasks.

    library(rvest)
    
    # 1. Read an HTML page
    starwars <- read_html("https://rvest.tidyverse.org/articles/starwars.html")
    
    # 2. Find elements matching a CSS selector
    films <- starwars |> html_elements("section")
    
    # 3. Extract text from a specific element within those nodes
    title <- films |> 
      html_element("h2") |> 
      html_text2()
    
    # 4. Extract attribute values (returns a string)
    episode <- films |> 
      html_element("h2") |> 
      html_attr("data-id") |> 
      readr::parse_integer()
  4. Convert HTML tables to data frames

    main

    If a web page contains tabular data, you can use html_table() to convert an HTML element (representing a <table>) directly into a data frame (tibble).

    html <- read_html("https://en.wikipedia.org/w/index.php?title=The_Lego_Movie&oldid=998422565")
    
    html |> 
      html_element(".tracklist") |> 
      html_table()