我是R的新手,我正在尝试从这个网站获得数据:https://spritacular.org/gallery。
我想知道地点,时间和时间。我遵循这个指南,使用我单击的元素(..card title,..card subtitle,.mb-0)单击的SelectorGadget。
但是,它总是输出{xml_nodeset (0)},我不知道为什么它没有得到这些元素。
这是我的代码:
url <- "https://spritacular.org/gallery"
sprite_gallery <- read_html(url)
sprite_location <- html_nodes(sprite_gallery, ".card-title , .card-subtitle , .mb-0")
sprite_location
当我改变网站,从一个不同的网站抓取一些东西,它的工作,所以我不知道我做错了什么,如何修复它,这是我第一次做这样的事情,我感谢你的洞察力!
发布于 2022-11-18 05:54:37
根据评论,这个网站有JS嵌入,信息只有在打开浏览器时才会打开。如果您转到开发人员工具和网络选项卡,您可以看到底层json数据。
如果您为这个api地址发布了一个GET
请求,您将得到一个包含所有结果的列表。从他们那里,你可以用切片和骰子的方式获得所需的信息。
一种方法是:我考虑了提交图像的用户的名称,发现同一个用户提交了多个图像。因此,输出中有重复的名称和位置,但是图像URL是不同的。参考这个博客,了解如何向下钻取json数据,以便在R中生成有用的数据
library(httr)
library(tidyverse)
getURL <- 'https://api.spritacular.org/api/observation/gallery/?category=&country=&cursor=cD0xMTI%3D&format=json&page=1&status='
# get the raw json into R
UOM_json <- httr::GET(getURL) %>%
httr::content()
exp_output <- pluck(UOM_json, 'results') %>%
enframe() %>%
unnest_longer(value) %>%
unnest_wider(value) %>%
select(user_data, images) %>%
unnest_wider(user_data) %>%
mutate(full_name = paste(first_name, last_name)) %>%
select(full_name, location, images) %>%
rename(., location_user = location) %>%
unnest_longer(images) %>%
unnest_wider(images) %>%
select(full_name, location, image)
我们的exp_output
输出
> head(exp_output)
# A tibble: 6 × 3
full_name location image
<chr> <chr> <chr>
1 Kevin Palivec Jones County,Texas,United States https://d1dzduvcvkxs60.cloudfront.net/observation_image/1d4cc82f-f3d2…
2 Kamil Świca Lublin,Lublin Voivodeship,Poland https://d1dzduvcvkxs60.cloudfront.net/observation_image/3b6391d1-f839…
3 Kamil Świca Lublin,Lublin Voivodeship,Poland https://d1dzduvcvkxs60.cloudfront.net/observation_image/9bcf10d7-bd7c…
4 Kamil Świca Lublin,Lublin Voivodeship,Poland https://d1dzduvcvkxs60.cloudfront.net/observation_image/a7dea9cf-8d6e…
5 Evelyn Lapeña Bulacan,Central Luzon,Philippines https://d1dzduvcvkxs60.cloudfront.net/observation_image/539e0870-c931…
6 Evelyn Lapeña Bulacan,Central Luzon,Philippines https://d1dzduvcvkxs60.cloudfront.net/observation_image/c729ea03-e1f8…
>
https://stackoverflow.com/questions/74482932
复制相似问题