【发布时间】:2020-05-29 11:06:23
【问题描述】:
我在从这个网页抓取文本信息时遇到了一些麻烦:http://www.iplant.cn/info/Acer%20stachyophyllum?t=foc
我需要的是这个网页中心的文字信息:“树木到 15 m 高,雌雄异株。......” 我尝试使用 R 包 rvest 中的 read_html 函数,但一无所获。有人可以帮我吗?非常感谢。
【问题讨论】:
标签: r web-scraping
我在从这个网页抓取文本信息时遇到了一些麻烦:http://www.iplant.cn/info/Acer%20stachyophyllum?t=foc
我需要的是这个网页中心的文字信息:“树木到 15 m 高,雌雄异株。......” 我尝试使用 R 包 rvest 中的 read_html 函数,但一无所获。有人可以帮我吗?非常感谢。
【问题讨论】:
标签: r web-scraping
这部分页面是通过 xhr 调用生成的。您可以通过以下方式从任何物种中获取您正在寻找的特定文本:
get_description <- function(species_name)
{
url <- "http://www.iplant.cn/ashx/getfoc.ashx"
query <- paste0("?key=", gsub(" ", "+", species_name),
"&key_no=&m=", runif(1), 9)
jsonlite::fromJSON(paste0(url, query))$Description
}
例如:
get_description("Actaea asiatica")
#> [1] "<p>Rhizome black-brown, with numerous slender fibrous roots.
#> Stems 30--80 cm tall, terete, 4--6(--9) mm in diam., unbranched,
#> basally glabrous, apically white pubescent. Leaves 2 or 3, proximal
#> cauline leaves 3 × ternately pinnate ...<truncated>
get_description("Acer stachyophyllum")
# > [1] "<p>Trees to 15 m tall, dioecious. Bark yellowish brown, smooth.
#> Branchlets glabrous. Leaves deciduous; petiole 2.5-8 cm, slightly
#> pubescent near apex; leaf blade ovate or oblong, 5-11 × 2.5-6 cm,
#> undivided or 3-lobed, papery, abaxially densely pale or white pubescent,
#> becoming less so when mature or nearly glabrous, adaxially glabrous,
#> 3-5-veined at base abaxially, rarely with rudimentary...<truncated>
【讨论】: