【问题标题】:Error using R and httr to get the content of the page: http://www2.bmf.com.br/pages/portal/bmfbovespa/lumis/lum-tipo-de-participante-ptBR.asp使用 R 和 httr 获取页面内容时出错:http://www2.bmf.com.br/pages/portal/bmfbovespa/lumis/lum-tipo-de-participante-ptBR.asp
【发布时间】:2018-06-03 01:19:13
【问题描述】:

我要获取页面内容http://www2.bmf.com.br/pages/portal/bmfbovespa/lumis/lum-tipo-de-participante-ptBR.asp

当我将此网址复制并粘贴到浏览器的位置时,我会获得页面的全部内容。

但是,我使用 R 和 httr 包使用 POST(发送“dData1”参数)和 GET 两种方法均不成功。

POST方法传递参数“dData1”

library(httr);

url="http://www2.bmf.com.br/pages/portal/bmfbovespa/lumis/lum-tipo-de-participante-ptBR.asp";
body = list(dData1="16/05/2018");
POST(url, body = body, encode = "form", verbose());

结果是:

-> POST /pages/portal/bmfbovespa/lumis/lum-tipo-de-participante-ptBR.asp HTTP/1.1
-> Host: www2.bmf.com.br
 (...omitted...)
-> 
>> dData1=16%2F05%2F2018

<- HTTP/1.1 200 OK
(...omitted...)
<- 
Response [http://www2.bmf.com.br/pages/portal/bmfbovespa/lumis/lum-tipo-de-participante-ptBR.asp]
  Date: 2018-06-02 16:28
  Status: 200
  Content-Type: text/html
  Size: 111 kB
NA

即使我尝试了简单的 GET,我也无法获取页面的内容:

library(httr);

url="http://www2.bmf.com.br/pages/portal/bmfbovespa/lumis/lum-tipo-de-participante-ptBR.asp";

GET(url,verbose())

结果是:

-> GET /pages/portal/bmfbovespa/lumis/lum-tipo-de-participante-ptBR.asp HTTP/1.1
(...omitted...)
-> 
<- HTTP/1.1 200 OK
(...omitted...)
<- 
Response [http://www2.bmf.com.br/pages/portal/bmfbovespa/lumis/lum-tipo-de-participante-ptBR.asp]
  Date: 2018-06-02 16:33
  Status: 200
  Content-Type: text/html
  Size: 140 kB
NA

我已经使用浏览器开发工具检查了请求标头,但我无法弄清楚我做错了什么,也无法获取此页面的内容。任何提示将不胜感激。

【问题讨论】:

    标签: r post get httr


    【解决方案1】:

    那个网站不是UTF-8编码的,所以你需要找到正确的编码并设置它来解析内容:

    my_url <- "http://www2.bmf.com.br/pages/portal/bmfbovespa/lumis/lum-tipo-de-participante-ptBR.asp"
    response <- GET(my_url)
    response
    content(response,as = "parsed",encoding = "iso-8859-1")
    

    结果:

    > content(response,as = "parsed",encoding = "iso-8859-1")
    {xml_document}
    <html class="no-js" lang="pt-br">
        [1] <head>\n<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">\n<meta name="viewport" content="width=device-width, initial-scale=1.0">\n<link rel=" ...
    [2] <body>\n<!-- Google Tag Manager -->\r\n<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-KPF8G3" height="0" width="0" style="display:none;visibil ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-24
      • 1970-01-01
      • 2016-02-28
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多