【问题标题】:Some troubles with Web scraping using R使用 R 进行网页抓取的一些问题
【发布时间】:2020-05-29 11:06:23
【问题描述】:

我在从这个网页抓取文本信息时遇到了一些麻烦:http://www.iplant.cn/info/Acer%20stachyophyllum?t=foc

我需要的是这个网页中心的文字信息:“树木到 15 m 高,雌雄异株。......” 我尝试使用 R 包 rvest 中的 read_html 函数,但一无所获。有人可以帮我吗?非常感谢。

【问题讨论】:

    标签: r web-scraping


    【解决方案1】:

    这部分页面是通过 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>
    

    【讨论】:

    • 谢谢。如何找到查询“?key=Acer+stachyophyllum&key_no=&m=0.24364438299155966”。有没有办法自动获取查询信息?例如,对于另一个网页(iplant.cn/info/Actaea%20asiatica?t=foc),我如何找到查询?
    • @JianZhang 我已更新以使其更通用。您现在只需在函数中输入拉丁名称,它就会得到描述文本。
    • 谢谢。这些真的很有帮助。
    • 嗨。另一个相关问题:我试图为另一个数据源修改你的 R 脚本,如下所示: get_descriptionISC iplant.cn/ashx/getisc.ashx" query
    猜你喜欢
    • 2014-12-28
    • 2021-11-02
    • 1970-01-01
    • 2022-12-31
    • 1970-01-01
    相关资源
    最近更新 更多