【发布时间】:2021-03-05 11:12:08
【问题描述】:
我正在尝试根据特定人的姓名(在 R 中)从网站(网址)下载图片。 我收到以下错误
Error in read_xml.raw(raw, encoding = encoding, base_url = base_url, as_html = as_html, :
CHAR() can only be applied to a 'CHARSXP', not a 'NULL'
这是回溯
19.read_xml.raw(raw, encoding = encoding, base_url = base_url, as_html = as_html,
options = options)
18.read_xml.connection(con, encoding = encoding, ..., as_html = as_html,
base_url = x, options = options)
17.read_xml.character(x, encoding = encoding, ..., as_html = TRUE,
options = options)
16.read_xml(x, encoding = encoding, ..., as_html = TRUE, options = options)
15.withCallingHandlers(expr, warning = function(w) if (inherits(w,
classes)) tryInvokeRestart("muffleWarning"))
14.suppressWarnings(read_xml(x, encoding = encoding, ..., as_html = TRUE,
options = options))
13.read_html.default(., image_page)
12.read_html(., image_page)
11.html_nodes(., "img")
10.xml2::xml_attr(x, name, default = default)
9.html_attr(., "src")
8.handle_url(handle, url, ...)
7.httr::GET(.)
6.is.response(x)
5.stopifnot(is.response(x))
4.httr::content(., "raw")
3.writeBin(., paste0("~/", ceo_name, ".jpg"))
2.paste0(site, image_page) %>% read_html(image_page) %>% html_nodes("img") %>%
html_attr("src") %>% {
grep("gstatic", ., value = TRUE)
} %>% 1[] %>% httr::GET() %>% httr::content("raw") %>% writeBin(paste0("~/", ...
1.get_image(as.character("Mark Lloyd"))
我不明白为什么。请问,有人可以启发我吗? 非常感谢
代码
> library(rvest)
> library(httr)
>
> get_image <- function(ceo_name)
+ {
+ site <- "https://www.icobench.com"
+ query <- paste0(site, "/ico/max-crowdfund/team", url_escape(ceo_name))
+
+ image_page <- read_html(query) %>%
+ html_nodes(xpath = "//a[contains(text(), 'Images')]") %>%
+ html_attr("href")
+
+ paste0(site, image_page) %>%
+ read_html(image_page) %>%
+ html_nodes("img") %>%
+ html_attr("src") %>%
+ {grep("gstatic", ., value = TRUE)} %>%
+ `[`(1) %>%
+ httr::GET() %>%
+ httr::content("raw") %>%
+ writeBin(paste0("~/", ceo_name, ".jpg"))
+ }
>
> get_image(as.character("Mark Lloyd"))
【问题讨论】:
标签: r web-scraping rvest httr