1

私は R にかなり慣れていないため、Forbes Web サイトからデータを取得するのに問題があります。

私の現在の機能は次のとおりです。

URL =

http://www.forbes.com/global2000/list/#page:1_sort:0_direction:asc_search:_filter:All%20industries_filter:All%20countries_filter:All%20states

データ = readHTMLTable(url)

ただし、Forbes の Web サイトは、リンク内に「#」記号で固定されています。必要なデータを解析するために rselenium パッケージをダウンロードしましたが、reselenium に精通していません。

reselenium に関するアドバイスや専門知識を持っている人はいますか? reselenium を使用して Forbes からデータを取得する方法はありますか? 理想的には、Web サイトの 1 ページ目、2 ページ目などからデータを取得したいと考えています。

ありがとう!

4

2 に答える 2

4

Or another way using the API used to populate the webpage. This downloads all 2000 companies at one time.

library(httr)
library(RJSONIO)
url <- "http://www.forbes.com/ajax/load_list/"
query <- list("type" = "organization",
              "uri" = "global2000",
              "year" = "2014")
response <- httr::GET(url, query=query)
dat_string <- as(response, "character")
dat_list <- RJSONIO::fromJSON(dat_string, asText=TRUE)
df <- data.frame(rank = sapply(dat_list, "[[", 1),
                 company = sapply(dat_list, "[[", 3),
                 country=sapply(dat_list, "[[", 10),
                 sales=sapply(dat_list, "[[", 6),
                 profits=sapply(dat_list, "[[", 7),
                 assets=sapply(dat_list, "[[", 8),
                 market_value=sapply(dat_list, "[[", 9), stringsAsFactors=F)
df <- df[order(df$rank),]
于 2015-02-12T18:57:23.723 に答える
1

少しハックですが、rvest と read.delim を使用した私のソリューションは次のとおりです...

library(rvest)

url <- "http://www.forbes.com/global2000/list/#page:1_sort:0_direction:asc_search:_filter:All%20industries_filter:All%20countries_filter:All%20states"
a <- html(url) %>%
  html_nodes("#thelist") %>%
  html_text()
con <- textConnection(a)
df <- read.delim(con, sep="\t", header=F, skip=12, stringsAsFactors=F)
close(con)
df$V1[df$V1==""] <- df$V3[df$V1==""]
df$V2 <- df$V3 <- NULL
df <- subset(df, V1!="")
df$index <- 1:nrow(df)
df2 <- data.frame(company=df$V1[df$index%%6==1],
                  country=df$V1[df$index%%6==2],
                  sales=df$V1[df$index%%6==3],
                  profits=df$V1[df$index%%6==4],
                  assets=df$V1[df$index%%6==5],
                  market_value=df$V1[df$index%%6==0])
于 2015-02-12T18:38:15.833 に答える