Skip to contents

page_text() decodes the body to a UTF-8 string, page_json() parses it with jsonlite::fromJSON(), and page_html() with xml2::read_html().

Usage

page_text(resp, encoding = NULL, check = TRUE)

page_json(resp, ..., check = TRUE)

page_html(resp, encoding = NULL, check = TRUE)

Arguments

resp

A response from read_page(), or a URL, which is then read with read_page()'s defaults, so that page_html(url) stands in for rvest::read_html(url).

encoding

The body's charset, overriding the response's.

check

Whether an HTTP status outside 200–299 is an error.

...

Passed on to jsonlite::fromJSON().

Value

page_text(): a UTF-8 string. page_json(): what jsonlite::fromJSON() returns. page_html(): an xml_document whose relative links resolve against resp$url. Errors are classed under safeconduct_error: safeconduct_http_error for a status outside 200–299, carrying status and url, and safeconduct_parse_error for a body that cannot be decoded, or HTML where JSON was asked for.

Details

The charset comes from the response's content-type; without one, page_text() and page_json() assume UTF-8 and page_html() leaves it to the page's own <meta charset>. ISO-8859-1 is read as windows-1252, as browsers do. A body that is not valid in its charset is an error. A "not found" page served with status 200 passes the status check; page_json() refuses an HTML body, and for a page, compare resp$url with the URL asked for.

Examples

if (FALSE) { # \dontrun{
page_json("https://results.vancouver.ca/data/arearesults.json")
page_html("https://www12.statcan.gc.ca/census-recensement/index-eng.cfm")
} # }