R语言 爬取百度地图天气预报

R语言 爬取百度地图天气预报

# Wed Jul 07 00:36:30 2021 -

# 字符编码:UTF-8
# R 版本:R 4.1 x64 for window 11
# [email protected]
# 个人笔记不负责任,拎了个梨
#.rs.restartR()
require(rjson)
require(jsonlite)
rm(list = ls());gc()

http://api.map.baidu.com/weather/v1/?district_id=440118&data_type=all&ak=6Fhk0*******K3SbXMoWxvsu

my_baidu_key <- readRDS('~/r,porject/mycode/my_baidu_key.rds')
cityid <- 440118 #增城区

paste0("http://api.map.baidu.com/weather/v1/?district_id=",
                 cityid,
                 "&data_type=all&ak=",
                 my_baidu_key) |>
  download.file(destfile = paste(runif(1),'.json'),mode = 'wb')

dt <- readr::read_file('0.271609853021801 .json')

dt.json <- dt |> fromJSON()
View(dt.json)
# Wed Jul 07 01:34:04 2021 ----清洗--------------------------
tq.5day <- dt.json$result$forecasts |> as.data.frame()
write.csv(tq.5day,file = '五天天气.csv')
# Wed Jul 07 01:36:39 2021 --end

image.png

你可能感兴趣的:(R语言 爬取百度地图天气预报)