【问题标题】:Rselenium - How to scrape data from a webpage with no id or names of any sortRselenium - 如何从没有 id 或任何名称的网页中抓取数据
【发布时间】:2017-05-20 06:55:42
【问题描述】:

我目前正在尝试从特定网站 (http://www.faunaeur.org/?no_redirect=1) 抓取生物多样性数据。我设法获得了一些结果,但没有像我希望的那样自动化...第一部分已完成,正在浏览网站:

设置 Rselenium:

library(RSelenium)
download.file("https://github.com/mozilla/geckodriver/releases/download/v0.11.1/geckodriver-v0.11.1-win64.zip",destfile="./gecko.zip")
unzip("./gecko.zip",exdir=".",overwrite=T)
checkForServer(update=T)
selfserv = startServer()
mybrowser1 = remoteDriver(browserName="firefox",extraCapabilities = list(marionette = TRUE))
mybrowser1$open()

然后开始我的浏览(以巴利阿里群岛为例):

mybrowser1$navigate("http://www.faunaeur.org/distribution.php?current_form=species_list")
mybrowser1$findElement(using="xpath","//select[@name='taxon_rank']/option[@value='7']")$clickElement()    # Class
mybrowser1$findElement(using="xpath","//input[@name='taxon_name']")$sendKeysToElement(list('Oligochaeta'))  # Oligochète
mybrowser1$findElement(using="xpath","//select[@name='region']/option[@value='15']")$clickElement()
mybrowser1$findElement(using="xpath","//input[@name='include_doubtful_presence']")$clickElement()
mybrowser1$findElement(using="xpath","//input[@name='submit2']")$clickElement()

从这里我可以使用以下方法下载20个亚种的xls文件:

mybrowser1$findElement(using = "xpath", "//a[@href='JavaScript:document.export_species_list.submit()']")$clickElement()

但这不是我想要的,我不想使用“点击”。是否可以直接在我的 R 环境中从此 JavaScript 链接下载文件,或者使用 Rselenium 直接从网页的源代码中抓取 20 个亚种的表?

我尝试了这两种解决方案,但这是一个僵局......最大的问题是该页面是一个临时页面或“结果页面”,我似乎无法在其中找到任何@value、@id、@我需要的表对应的名称或@class。

关于暗示通过 R 实现自动化的解决方案的任何线索?我需要这种形式的它,因为脚本必须由需要自己创建结果的人运行。提前致谢 !

【问题讨论】:

  • 是的,您需要设置适当的 firefox 选项,请参阅stackoverflow.com/questions/36574012/…。然后将 xls 文件下载到您命名的目录中
  • 我确实已经检查过了。只是想知道是否有任何其他有效的解决方案......因为你是 Rselenium 的开发者,jdharrison,我不认为我会得到更好的答案!谢谢

标签: r web-scraping rcurl rvest rselenium


【解决方案1】:

如果您只想要在网站上显示的表格,可以通过httr 在不使用 Rselenium 的情况下完成,如下所示:

require(rvest)
require(httr)
res <- POST("http://www.faunaeur.org/species_list.php",
            encode = "form", 
            body = list(selected_regions="15",
                        show_what="species list",
                        referring_page="distribution",
                        taxon_rank="7",
                        taxon_name="Oligochaeta",
                        region="15",
                        include_doubtful_presence="yes",
                        submit2="Display Species",
                        show_what="species list",
                        species_or_higher_taxa="species"))
doc <- res %>% read_html
dat <- doc %>% html_table(fill=TRUE, ) %>% .[[9]]
colnames(dat) <- dat[1,]
dat <- dat[-1, ]

这给了你:

            Family                      Species / subspecies
2  Acanthodrilidae       Microscolex dubius (Fletscher 1887)
3    Enchytraeidae      Enchytraeus buchholzi Vejdovsky 1878
4    Enchytraeidae     Fridericia berninii Dozsa-Farkas 1988
5    Enchytraeidae            Fridericia caprensis Bell 1947
...
21        Naididae           Aulophorus furcatus (Oken 1815)

【讨论】:

  • 这太棒了!我的错误是我专注于 Rselenium 包,我没有意识到这可以通过 rvest 和 httr 完成...谢谢 FlooO!
猜你喜欢
  • 2020-09-26
  • 2013-07-15
  • 2019-04-24
  • 2019-03-25
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-28
相关资源
最近更新 更多