Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
979 views
in Technique[技术] by (71.8m points)

web scraping - R - How to make a click on webpage using rvest or rcurl

I want to download data from this webpage

The data can be easily scraped with rvest.

The code maybe like this :

library(rvest)
library(pipeR)
url <- "http://www.tradingeconomics.com/"
css <-     "#ctl00_ContentPlaceHolder1_defaultUC1_CurrencyMatrixAllCountries1_GridView1"

data <- url %>>%
  html() %>>%
  html_nodes(css) %>>%
  html_table() 

But there is a problem for webpages like this.

There is a + button to show the data of all the countries, but the default is just data of 50 countries.

So if I use the code, I can just scrape data of 50 countries.

The + button is made in javascript, so I want to know if there is a way in R to click the button and then scrape the data.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Sometimes it's better to attack the problem at the ajax web-request level. For this site, you can use Chrome's dev tools and watch the requests. To build the table (the whole table, too) it makes a POST to the site with various ajax-y parameters. Just replicate that, do a bit of data-munging of the response and you're good to go:

library(httr)
library(rvest)
library(dplyr)

res <- POST("http://www.tradingeconomics.com/",
            encode="form",
            user_agent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.50 Safari/537.36"),
            add_headers(`Referer`="http://www.tradingeconomics.com/",
                        `X-MicrosoftAjax`="Delta=true"),
            body=list(
              `ctl00$AjaxScriptManager1$ScriptManager1`="ctl00$ContentPlaceHolder1$defaultUC1$CurrencyMatrixAllCountries1$UpdatePanel1|ctl00$ContentPlaceHolder1$defaultUC1$CurrencyMatrixAllCountries1$LinkButton1",
              `__EVENTTARGET`="ctl00$ContentPlaceHolder1$defaultUC1$CurrencyMatrixAllCountries1$LinkButton1",
              `srch-term`="",
              `ctl00$ContentPlaceHolder1$defaultUC1$CurrencyMatrixAllCountries1$GridView1$ctl01$DropDownListCountry`="top",
              `ctl00$ContentPlaceHolder1$defaultUC1$CurrencyMatrixAllCountries1$ParameterContinent`="",
              `__ASYNCPOST`="false"))


res_t <- content(res, as="text")
res_h <- paste0(unlist(strsplit(res_t, "
"))[-1], sep="", collapse="
")

css <- "#ctl00_ContentPlaceHolder1_defaultUC1_CurrencyMatrixAllCountries1_GridView1"

tab <- html(res_h) %>% 
  html_nodes(css) %>%
  html_table() 

tab[[1]]$COUNTRIESWORLDAMERICAEUROPEASIAAUSTRALIAAFRICA

glimpse(tab[[1]]

Another alternative would have been to use RSelenium to go to the page, click the "+" and then scrape the resultant table.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...