【问题标题】:Looking for recommendations on scraping specific data from this unstructured, paginated HTML website寻找有关从这个非结构化、分页 HTML 网站抓取特定数据的建议
【发布时间】:2021-08-04 20:26:50
【问题描述】:

正如标题所述,我正在尝试从网站中提取数据。具体来说,我正在尝试从 each of the species pages found here 中提取 host susceptibilityhost 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 "                                                                                                                                                             
...           

从这里开始,我不熟悉如何从字符串中专门选择/过滤所需的信息。我尝试了一些stringrgsubrm_between 过滤功能,但所有尝试均未成功。我不知道从哪里开始让这段代码访问在线数据库上的许多物种页面,或者如何指示它保存聚合数据。我前面的路多好啊!

【问题讨论】:

    标签: r web-scraping rvest stringr


    【解决方案1】:

    这里有一个技巧。

    可以得到索引

    1. 'Susceptible host species'
    2. 'Insusceptible host species'
    3. 'Families containing susceptible hosts'

    1 和 2 之间的所有内容都是 susceptible_species,2 和 3 之间的所有内容都是 insusceptible_species

    library(rvest)
    library(stringr)
    
    pvo <- read_html("http://bio-mirror.im.ac.cn/mirrors/pvo/vide/descr042.htm")
    
    all_values <- pvo %>% html_elements("h4 li") %>% html_text() 
    which(all_values == 'Susceptible host species')
    
    sus_index <- grep('Susceptible host species', all_values, fixed = TRUE)
    insus_index <- grep('Insusceptible host species', all_values, fixed = TRUE)
    family_sus_index <- grep('Families containing susceptible hosts', all_values, fixed = TRUE)
    
    susceptible_species <- all_values[(sus_index+1):(insus_index-1)]
    susceptible_species
    
    # [1] "Chenopodium amaranticolor" "Chenopodium quinoa"        "Cucumis sativus"          
    # [4] "Cucurbita pepo"            "Cynara scolymus"           "Gomphrena globosa"        
    # [7] "Nicotiana benthamiana"     "Nicotiana clevelandii"     "Nicotiana glutinosa"      
    #[10] "Ocimum basilicum"          "Vigna unguiculata"    
        
    insusceptible_species <- all_values[(insus_index+1):(family_sus_index-1)]
    insusceptible_species
    #[1] "Nicotiana rustica"   "Nicotiana tabacum"   "Phaseolus vulgaris "
    

    【讨论】:

    • 感谢您的回复。有趣的是,这对我不起作用。 which(all_values...grep() 正在生成字符(空)
    • 找到了阻止创建 all_values 的问题(%&gt;% html_elements("h4 li") 中缺少逗号,应该是 %&gt;% html_elements("h4, li")。它现在正在工作;谢谢!
    猜你喜欢
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 2018-06-26
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多