【发布时间】:2021-08-04 20:26:50
【问题描述】:
正如标题所述,我正在尝试从网站中提取数据。具体来说,我正在尝试从 each of the species pages found here 中提取 host susceptibility 和 host insusceptibility 数据。
这些数据可以在个别物种的特定页面上找到,例如Abelia latent tymovirus at its respective URL。
我正在努力提取这些数据,因为 HTML 似乎非常非结构化。例如,主机易感性/不易感性始终存在于节点 h4 中,但与其他不同的标头和列表项一起存在。
这是我第一次尝试网络抓取,我一直在尝试使用 chrome 插件 Web Scraper,它看起来非常直观和灵活。我已经能够让刮板访问多个页面,但我似乎无法指示它专门收集易感性/易感性数据。我尝试使用 SelectorGadget 来准确识别我的选择器应该是什么,但是 HTML 中缺乏结构使得这无效。
关于如何改变我的攻击计划的任何建议?
我也愿意尝试使用 R 的 rvest 包提取数据。到目前为止,我已经能够从特定页面读取 html,提取 h4 和 li 元素,并清理换行符。可重现的代码:
library(rvest)
library(stringr)
pvo <- read_html("http://bio-mirror.im.ac.cn/mirrors/pvo/vide/descr042.htm")
pvo %>%
html_elements("h4, li") %>%
html_text() %>%
str_replace_all("[\n]" , "")
这似乎为我提供了我想要的东西以及无关的数据:
...
[20] "Susceptible host species "
[21] "Chenopodium amaranticolor"
[22] "Chenopodium quinoa"
[23] "Cucumis sativus"
[24] "Cucurbita pepo"
[25] "Cynara scolymus"
[26] "Gomphrena globosa"
[27] "Nicotiana benthamiana"
[28] "Nicotiana clevelandii"
[29] "Nicotiana glutinosa"
[30] "Ocimum basilicum"
[31] "Vigna unguiculata"
[32] "Insusceptible host species"
[33] "Nicotiana rustica"
[34] "Nicotiana tabacum"
[35] "Phaseolus vulgaris "
...
从这里开始,我不熟悉如何从字符串中专门选择/过滤所需的信息。我尝试了一些stringr、gsub 和rm_between 过滤功能,但所有尝试均未成功。我不知道从哪里开始让这段代码访问在线数据库上的许多物种页面,或者如何指示它保存聚合数据。我前面的路多好啊!
【问题讨论】:
标签: r web-scraping rvest stringr