【问题标题】:How to avoid null character when scraping a web page with getURL() in R?在 R 中使用 getURL() 抓取网页时如何避免空字符?
【发布时间】:2018-04-06 08:51:04
【问题描述】:

我在使用 RCurlgetURL() 函数抓取网站时遇到问题。例如,http://dogecoin.com 会返回一个错误,指出 NULL 字符位于链的中间(字面翻译。

> x <- getURL("http://dogecoin.com/")
Error in curlPerform(curl = curl, .opts = opts, .encoding = .encoding) : 
  caractère nul au milieu de la chaîne : '\037\x8b\b\0\0\0\0\0\0\003\xed]\xebr\xdbF\x96\xfe\035=E\x9b\xa9\x89\xe4]\x82\xd4͗8\x92R\xb2|\x9d\x91-\x97\xa5\xac7\x95IiA\002$a\x81\0\x82\x8bhf2ﰯ\xb1\xaf\xb1\xfbb\xfb}\xa7\033WB\022E{\xa62\025\xa5*2I\xf4\xbdO\x9f\xcbw\xcei\xec\xdd{vrt\xf6\xe3\xbb\xe7j\x92N\xfd\x83\xb5\xbd\xfc\037\xd7v\016\xd6\024\xfeۻgY\xf2\xc1\xfcw~^\xf9\xb2\xf0\xf1\xdc\024=\xc7\177}\xd5\xc7_\xa5\xf8Y\xff\x91\xbf\xf2\x9fR\033\xe7\xf7\xf1\xaf.\xde\xc7\003\xa5\xe4\xef_\xe5\xef\177\xe1\xaf\xfe\x88V\xf4\xaf\xfa_)φ\xf1\177\xed/JI\177ů|\xae\xfaR\xfe\xaf\xe7\xe7\xdd\xf3>\xfe\xe21?+\xf9\\\xfe9Gs\xbabu\xa2ғ\xd4Y\x93\x9f\x93PM\xed\xf8"\x8bz\xeaҍ\xe7j\xe6\016\022/u{j\026\xcezR²\016\xd6־₩7nj\xcb\xf7\xaf\xf6R/\xf5݃g\xe1\xd8\035\x86^\xb0\xd7\xd7\xdf\xf1`\xca2É\035'n\xba\xdf\xc9ґ\xf5\xb8\xc3\n\xfa\xf70H\xdd\0\xbf\xe7\025\x95\x97(;Pa\xe4\006\030J\026\017]\025\xb9nl\xa5\xa1\xc5\177\x95㍽\xd4\xf6\xd50\x8bc7\030λjd_\x86\xb1\xeb\xa8\xc1\\\x9dN\xbc\x81\xad^\aY\x82\xd1

在极少数情况下,它会返回干净的 HTML 代码,但大多数时候我都会遇到此错误。这似乎与他们的网站有关,并且您可以看到有几个奇怪的字符,例如 ₩ 和 4͗。

可以选择使用getURLcontent() 下载原始数据,但我无法将二进制内容转换为 HTML。

我尝试更改 .encoding 参数,但它没有给出预期的结果。我怎么能抓取这个网页?

编辑:详细模式

> getURL("http://dogecoin.com/", verbose = TRUE)
*   Trying 192.30.252.153...
* Connected to dogecoin.com (192.30.252.153) port 80 (#0)
> GET / HTTP/1.1
Host: dogecoin.com
Accept: */*

< HTTP/1.1 200 OK
< Server: GitHub.com
< Date: Wed, 25 Oct 2017 10:12:26 GMT
< Content-Type: text/html; charset=utf-8
< Transfer-Encoding: chunked
< Last-Modified: Tue, 16 May 2017 01:27:52 GMT
< Access-Control-Allow-Origin: *
< Expires: Wed, 25 Oct 2017 10:05:08 GMT
< Cache-Control: max-age=600
< Content-Encoding: gzip
< X-GitHub-Request-Id: A4D0:66A8:93356A1:D740FF7:59F0638A
< 
Error in curlPerform(curl = curl, .opts = opts, .encoding = .encoding) : 
  caractère nul au milieu de la chaîne : '\037\x8b\b\0\0\0\0\0\0\003\xed]\xebr\xdbF\x96\xfe\035=E\x9b\xa9\x89\xe4]\x82\xd4͗8\x92R\xb2|\x9d\x91-\x97\xa5\xac7\x95IiA\002$a\x81\0\x82\x8bhf2ﰯ\xb1\xaf\xb1\xfbb\xfb}\xa7\033WB\022E{\xa62\025\xa5*2I\xf4\xbdO\x9f\xcbw\xcei\xec\xdd{vrt\xf6\xe3\xbb\xe7j\x92N\xfd\x83\xb5\xbd\xfc\037\xd7v\016\xd6\024\xfeۻgY\xf2\xc1\xfcw~^\xf9\xb2\xf0\xf1\xdc\024=\xc7\177}\xd5\xc7_\xa5\xf8Y\xff\x91\xbf\xf2\x9fR\033\xe7\xf7\xf1\xaf.\xde\xc7\003\xa5\xe4\xef_\xe5\xef\177\xe1\xaf\xfe\x88V\xf4\xaf\xfa_)φ\xf1\177\xed/JI\177ů|\xae\xfaR\xfe\xaf\xe7\xe7\xdd\xf3>\xfe\xe21?+\xf9\\\xfe9Gs\xbabu\xa2ғ\xd4Y\x93\x9f\x93PM\xed\xf8"\x8bz\xeaҍ\xe7j\xe6\016\022/u{j\026\xcezR²\016\xd6־₩7nj\xcb\xf7\xaf\xf6R/\xf5݃g\xe1\xd8\035\x86^\xb0\xd7\xd7\xdf\xf1`\xca2É\035'n\xba\xdf\xc9ґ\xf5\xb8\xc3\n\xfa\xf70H\xdd\0\xbf\xe7\025\x95\x97(;Pa\xe4\006\030J\026\017]\025\xb9nl\xa5\xa1\xc5\177\x95㍽\xd4\xf6\xd50\x8bc7\030λjd_\x86\xb1\xeb\xa8\xc1\\\x9dN\xbc\x81\xad^\aY\x82\xd1
> 

【问题讨论】:

  • 我不知道如何解决您的问题,但这里有一个对我有用的替代方案:library(httr); GET("http://dogecoin.com/",write_disk("test.txt")); x &lt;- readLines("test.txt")

标签: r rcurl


【解决方案1】:

RCurl::getURL() 似乎没有检测到 Content-Encoding: gzip 标头,也没有检测到前两个字节的“魔术”代码,也表明内容是 gzip 编码的。

我会建议 - 正如迈克尔所做的那样 - 切换到 httr,原因我稍后会介绍,但这会是一个更好的 httr 成语:

library(httr)

res <- GET("http://dogecoin.com/")
content(res)

content() 函数提取原始响应并返回一个xml2 对象,该对象类似于XML 库解析对象,如果使用RCurl::getURL(),您可能会使用该对象。

另一种方法是给RCurl::getURL()添加一些拐杖:

html_text_res <- RCurl::getURL("http://dogecoin.com/", encoding="gzip")

在这里,我们明确通知 getURL() 内容是 gzip'd,但这充满危险,如果上游服务器决定使用 brotli 编码,那么你会得到一个错误。

如果您仍想使用RCurl 与切换到httr,我建议您为该站点执行以下操作:

RCurl::getURL("http://dogecoin.com/", 
              encoding = "gzip",
              httpheader = c(`Accept-Encoding` = "gzip"))

Here' 给 getURL() 解码拐杖,但也明确告诉上游服务器 gzip 是 ? 并且它应该使用该编码发送数据。

但是,httr 将是更好的选择,因为它和它使用的 curl 包以更彻底的方式处理 Web 服务器交互和内容。

【讨论】:

  • 谢谢,我将使用httr 选项,因为它是一个更好的选择。
猜你喜欢
  • 1970-01-01
  • 2023-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多