我试着从https://www.xercise4less.co.uk/find-a-gym/上收集所有健身房的数据。
在开发人员工具中,我找到了一个指向Web的指针,它应该将这些信息存储在https://www.xercise4less.co.uk/Umbraco/Api/FindAGymApi/GetAll下,但是当我在浏览器中运行它时,
'ObjectContent`1‘1’类型未能序列化内容类型'text/xml;charset=utf-8‘的响应体
同样,如果我运行以下代码:
# user_agent argument is optional here and results are the same whether I include it or not
httr::GET('https://www.xercise4less.co.uk/Umbraco/Api/FindAGymApi/GetAll', httr::user_agent("httr"))对怎么做有什么想法吗?
或者,我可以(几乎)访问所有的健身房ID
library(rvest)
library(magrittr)
url <- "https://www.xercise4less.co.uk/find-a-gym/"
my_pg <- read_html(url)
my_pg %>% html_nodes('select > option')但是,我仍然不知道如何迭代所有的in,以获得完整的坐标/位置列表。谢谢你的指点。
发布于 2019-10-11 19:23:28
您几乎在那里,您只需要设置正确的请求头预期服务器,然后你得到所有的信息为所有健身房。
library(httr)
headers = c('Accept'='application/json, text/javascript, */*; q=0.01')
r <- content(httr::GET(url = 'https://www.xercise4less.co.uk/Umbraco/Api/FindAGymApi/GetAll', httr::add_headers(.headers=headers)))
print(r)发布于 2019-10-11 14:11:55
这段代码应该可以让您得到所需的东西,或者至少更接近它。我放弃了第二个例子。我可能会移除第一行。
final <- as.character(my_pg %>% html_nodes('select > option'))
df <- data.frame(do.call(rbind, strsplit(final, '>', fixed=TRUE)), stringsAsFactors = FALSE)
df$X1 <-sapply(strsplit(df$X1, '=', fixed=TRUE), "[", 2 )
df$X1 <- gsub('[\"]', '', df$X1)
df$X2 <-sapply(strsplit(df$X2, '<', fixed=TRUE), "[", 1 )
df = subset(df, select = -c(X3) )输出
X1 X2
1 <NA> Select a location
2 1104 Xercise4Less Bolton Gym
3 1248 Xercise4Less Bradford Gym
4 1249 Xercise4Less Brierley Hill Gym
5 1250 Xercise4Less Bristol Gym
6 1251 Xercise4Less Burnley Gymhttps://stackoverflow.com/questions/58342552
复制相似问题