You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R中从URL下载JSON文件出现损坏问题求助

解决R中download.file下载法国地籍JSON文件损坏的问题

我编写了上述R代码,用于从法国地籍网站获取JSON文件下载链接并执行下载,但下载的文件损坏,打开后显示二进制及乱码字符;手动下载该链接的文件却能正常显示JSON内容,不确定是否是download.file函数使用有误,至今未能解决问题。

lien_du_cadastre<-str_replace_all(paste("https://france-cadastre.fr/cadastre/",Nom_ville), " ","")
lien_du_cadastre
serveur_objet$navigate(lien_du_cadastre)

url2=serveur_objet$findElement(using = 'xpath','/html/body/div[4]/div[3]/div[1]/div[4]/div/div[3]/div[2]/div/a[1]')$getElementAttribute("href")[[1]] #donne l'url de téléchargement du plan cadastre
url2 (the url inside is : https://france-cadastre.fr/downloadcenter.php?format=json&echelle=commune&insee=02408&type=parcelles)
file_path <- tempfile(pattern = "", fileext = ".json")
file_path
download.file(url2, file_path, quiet = TRUE, mode = "wb")

问题核心在于download.file发起的请求没有携带你用Selenium打开页面时的会话Cookie和浏览器级请求头。法国地籍网站会验证会话状态,直接用download.file请求会被判定为非合法会话,返回的是反爬页面、登录提示等非JSON内容(表现为乱码/二进制),而手动下载是在同一个浏览器会话中,自然能正常获取。

以下是两种可行的解决方案:

方案一:用Selenium直接模拟下载(最省心)

直接让Selenium点击下载链接,复用已有的会话状态,避免单独请求的问题。需要提前配置Chrome的下载偏好,跳过保存对话框:

library(RSelenium)

# 提前配置Chrome下载设置(初始化driver前执行)
chrome_options <- chromeOptions()
chrome_options$addArguments("--disable-popup-blocking")
chrome_options$setPreference("download.default_directory", tempdir()) # 指定临时目录为下载路径
chrome_options$setPreference("download.prompt_for_download", FALSE)
chrome_options$setPreference("download.directory_upgrade", TRUE)
chrome_options$setPreference("safebrowsing.enabled", TRUE)

# 初始化driver
serveur_objet <- remoteDriver(
  browserName = "chrome",
  extraCapabilities = chrome_options
)
serveur_objet$open()

# 原有导航代码
lien_du_cadastre<-str_replace_all(paste("https://france-cadastre.fr/cadastre/",Nom_ville), " ","")
serveur_objet$navigate(lien_du_cadastre)

# 找到下载链接并点击,自动完成下载
download_link <- serveur_objet$findElement(using = 'xpath','/html/body/div[4]/div[3]/div[1]/div[4]/div/div[3]/div[2]/div/a[1]')
download_link$click()

方案二:复用Selenium的Cookie用httr下载

如果不想用Selenium直接下载,可提取会话Cookie,用httr包发起携带Cookie和浏览器UA的合法请求:

library(RSelenium)
library(httr)

# 原有导航和获取URL代码
lien_du_cadastre<-str_replace_all(paste("https://france-cadastre.fr/cadastre/",Nom_ville), " ","")
serveur_objet$navigate(lien_du_cadastre)
url2 <- serveur_objet$findElement(using = 'xpath','/html/body/div[4]/div[3]/div[1]/div[4]/div/div[3]/div[2]/div/a[1]')$getElementAttribute("href")[[1]]

# 提取Selenium会话中的Cookie,转换为httr可用格式
cookies <- serveur_objet$getAllCookies()
httr_cookies <- lapply(cookies, function(c) {
  cookie(
    name = c$name,
    value = c$value,
    domain = c$domain,
    path = c$path,
    expires = c$expiry
  )
})

# 发起合法请求并保存文件
file_path <- tempfile(pattern = "", fileext = ".json")
response <- GET(
  url2,
  set_cookies(.cookies = httr_cookies),
  user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")
)
writeBin(content(response, "raw"), file_path)

内容的提问来源于stack exchange,提问作者Christ Bouka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 16:25:22