R:用httr模拟复杂的形式

时间:2016-10-30 09:29:58

标签: r get httr

我正在尝试使用httr获取that form的结果。

看了form results后,我尝试了以下内容:

library(httr)
library(stringr)

r = str_c("http://www.memoiredeshommes.sga.defense.gouv.fr/fr/arkotheque/",
          "client/mdh/base_morts_pour_la_france_premiere_guerre/index.php")

q = list(
  "action" = 1,
  "todo" = "rechercher",
  "le_id"  = "",
  "multisite" = "",
  "r_c_nom" = "mo",
  "r_c_nom_like" = 1,
  "r_c_prenom" = "",
  "r_c_prenom_like" = 1,
  "r_c_naissance_jour_mois_annee_jj_debut" = "",
  "r_c_naissance_jour_mois_annee_mm_debut" = "",
  "r_c_naissance_jour_mois_annee_yyyy_debut" = 1890,
  "r_c_naissance_jour_mois_annee_jj_fin" = "",
  "r_c_naissance_jour_mois_annee_mm_fin" = "",
  "r_c_naissance_jour_mois_annee_yyyy_fin" = "",
  "r_c_id_naissance_departement" = "",
  "hidden_c_id_naissance_departement" = "",
  "r_c_id_naissance_pays" = "",
  "hidden_c_id_naissance_pays" = "",
  "r_annot_c_id_grade" = "",
  "hidden_c_id_grade" = "",
  "r_annot_c_id_unite" = "",
  "hidden_c_id_unite" = "",
  "r_annot_c_id_recrutement_bureau" = "",
  "hidden_c_id_recrutement_bureau" = "",
  "r_annot_c_classe" = "",
  "r_annot_c_recrutement_matricule" = "",
  "r_annot_c_id_naissance_lieu" = "",
  "hidden_c_id_naissance_lieu" = "",
  "r_annot_c_deces_jour_mois_annee_jj_debut" = "",
  "r_annot_c_deces_jour_mois_annee_mm_debut" = "",
  "r_annot_c_deces_jour_mois_annee_yyyy_debut" = "",
  "r_annot_c_deces_jour_mois_annee_jj_fin" = "",
  "r_annot_c_deces_jour_mois_annee_mm_fin" = "",
  "r_annot_c_deces_jour_mois_annee_yyyy_fin" = "",
  "r_annot_c_id_deces_lieu" = "",
  "hidden_c_id_deces_lieu" = "",
  "r_annot_c_deces_lieu_complement" = "",
  "r_annot_c_deces_lieu_complement_like" = 1,
  "r_annot_c_id_deces_departement" = "",
  "hidden_c_id_deces_departement" = "",
  "r_annot_c_id_deces_pays" = "",
  "hidden_c_id_deces_pays" = "",
  "r_annot_c_id_transcription_etablissement_lieu" = "",
  "hidden_c_id_transcription_etablissement_lieu" = "",
  "r_annot_c_id_transcription_etablissement_departement" = "",
  "hidden_c_id_transcription_etablissement_departement" = "",
  "r_annot_c_id_transcription_etablissement_pays" = "",
  "hidden_c_id_transcription_etablissement_pays" = ""
)

t = GET(r, query = q, verbose())
writeLines(content(t, "text", encoding = "UTF-8"), "~/Desktop/test.html")

...根本不起作用(我得到的只是NA)。

我做错了什么?

1 个答案:

答案 0 :(得分:5)

你可以这样试试

library(rvest)
html_session(url) %>%
  rvest:::request_POST(url, body = q, encode = "form") %>%
  read_html  %>%
  html_table 
# [[1]]
#             Nom                     Prénom(s) Date de naissance                   Département/Pays de naissance Détail     Images Panier Lien Fiche annotée
# 1          MOAL                    Alain Marc        10-08-1890                                  29 - Finistère Détail Visualiser Panier  Ark           oui
# 2          MOAL                          Jean        22-12-1890                                  29 - Finistère Détail Visualiser Panier  Ark           oui
# 3          MOAL                  Joseph Marie        29-04-1890                                  29 - Finistère Détail Visualiser Panier  Ark           oui
# 4        MOALIC           Pierre Joseph Marie        05-04-1890                                  29 - Finistère Détail Visualiser Panier  Ark           oui
# ...