Ric*_*sta 5 r web-scraping httr rvest
我正在测试R中的一些web scrape脚本.我已经阅读了很多教程,文档并尝试了不同的东西,但到目前为止还没有成功.
我试图抓取的URL就是这个.它有公共,政府数据,没有针对网络抓取工具的声明.它是葡萄牙语,但我相信这不会是一个大问题.
它显示了一个包含多个字段的搜索表单.我的测试是搜索来自特定州("RJ",在这种情况下,该字段是"UF")和城市("Rio de Janeiro",在"MUNICIPIO"字段中)的数据.通过单击"Pesquisar"(搜索),它显示以下输出:
使用Firebug,我发现它调用的URL(使用上面的参数)是:
http://www.dataescolabrasil.inep.gov.br/dataEscolaBrasil/home.seam?buscaForm=buscaForm&codEntidadeDecorate%3AcodEntidadeInput=&noEntidadeDecorate%3AnoEntidadeInput=&descEnderecoDecorate%3AdescEnderecoInput=&estadoDecorate%3A**estadoSelect=33**&municipioDecorate%3A**municipioSelect=3304557**&bairroDecorate%3AbairroInput=&pesquisar.x=42&pesquisar.y=16&javax.faces.ViewState=j_id10
Run Code Online (Sandbox Code Playgroud)
该网站使用jsessionid,使用以下内容可以看到:
library(rvest)
library(httr)
url <- GET("http://www.dataescolabrasil.inep.gov.br/dataEscolaBrasil/")
cookies(url)
Run Code Online (Sandbox Code Playgroud)
知道它使用了jsessionid,我使用cookies(url)检查这些信息,并将其用于这样的新URL:
url <- read_html("http://www.dataescolabrasil.inep.gov.br/dataEscolaBrasil/home.seam;jsessionid=008142964577DBEC622E6D0C8AF2F034?buscaForm=buscaForm&codEntidadeDecorate%3AcodEntidadeInput=33108064&noEntidadeDecorate%3AnoEntidadeInput=&descEnderecoDecorate%3AdescEnderecoInput=&estadoDecorate%3AestadoSelect=org.jboss.seam.ui.NoSelectionConverter.noSelectionValue&bairroDecorate%3AbairroInput=&pesquisar.x=65&pesquisar.y=8&javax.faces.ViewState=j_id2")
html_text(url)
Run Code Online (Sandbox Code Playgroud)
好吧,输出没有数据.实际上,它有一条错误消息.翻译成英文,它基本上说会话已经过期.
我认为这是一个基本的错误,但我四处寻找并找不到克服这个问题的方法.
这种组合对我有用:
library(curl)
library(xml2)
library(httr)
library(rvest)
library(stringi)
# warm up the curl handle
start <- GET("http://www.dataescolabrasil.inep.gov.br/dataEscolaBrasil/home.seam")
# get the cookies
ck <- handle_cookies(handle_find("http://www.dataescolabrasil.inep.gov.br/dataEscolaBrasil/home.seam")$handle)
# make the POST request
res <- POST("http://www.dataescolabrasil.inep.gov.br/dataEscolaBrasil/home.seam;jsessionid=" %s+% ck[1,]$value,
user_agent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:40.0) Gecko/20100101 Firefox/40.0"),
accept("*/*"),
encode="form",
multipart=FALSE, # this gens a warning but seems to be necessary
add_headers(Referer="http://www.dataescolabrasil.inep.gov.br/dataEscolaBrasil/home.seam"),
body=list(`buscaForm`="buscaForm",
`codEntidadeDecorate:codEntidadeInput`="",
`noEntidadeDecorate:noEntidadeInput`="",
`descEnderecoDecorate:descEnderecoInput`="",
`estadoDecorate:estadoSelect`=33,
`municipioDecorate:municipioSelect`=3304557,
`bairroDecorate:bairroInput`="",
`pesquisar.x`=50,
`pesquisar.y`=15,
`javax.faces.ViewState`="j_id1"))
doc <- read_html(content(res, as="text"))
html_nodes(doc, "table")
## {xml_nodeset (5)}
## [1] <table border="0" cellpadding="0" cellspacing="0" class="rich-tabpanel " id="j_id17" sty ...
## [2] <table border="0" cellpadding="0" cellspacing="0">\n <tr>\n <td>\n <img alt="" ...
## [3] <table border="0" cellpadding="0" cellspacing="0" id="j_id18_shifted" onclick="if (RichF ...
## [4] <table border="0" cellpadding="0" cellspacing="0" style="height: 100%; width: 100%;">\n ...
## [5] <table border="0" cellpadding="10" cellspacing="0" class="dr-tbpnl-cntnt-pstn rich-tabpa ...
Run Code Online (Sandbox Code Playgroud)
我使用BurpSuite检查发生的情况,并使用“Copy as cURL”的输出在命令行进行快速测试,并添加--verbose到我可以验证发送/接收的内容。然后我模仿了curl参数。
从裸搜索页面开始,会话 ID 和 bigip 服务器的 cookie 已经预热(即,将随每个请求一起发送,因此您不必弄乱它们),但您仍然需要在URL 路径,因此我们必须检索它们,然后填写它。