![](/img/trans.png)
[英]R How to web scrape data from StockTwits with RSelenium?
[英]Rselenium - How to scrape data from a webpage with no id or names of any sort
我目前正在嘗試從特定網站 ( http://www.faunaeur.org/?no_redirect=1 ) 抓取生物多樣性數據。 我設法得到了一些結果,但沒有我希望的那么自動化......第一部分已經完成,瀏覽網站:
設置硒:
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、@name 或 @對應於我需要的表的類。
任何暗示通過 R 自動完成的解決方案的線索? 我需要這種形式的腳本,因為腳本必須由需要自己創建結果的人運行。 提前致謝 !
如果您只想要網站上顯示的表格,則可以通過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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.