rvest爬取鸡蛋期货数据(遇到的问题)

1.之前用rvest爬取网页表格,很顺利,但这次用该语句时,因为电脑编码问题,一直提示
Error in doc_parse_raw(x, encoding = encoding, base_url = base_url, as_html = as_html,  : 
  input conversion failed due to input error, bytes 0x86 0x31 0x31 0x30 [6003]

别的电脑可以出结果,我的电脑因为今天520,打算和女朋友统一战线,有点小脾气 ,那我只好想想其他方法。以下是之前所用代码。

library(rvest)
tdist<-read_html("http://vip.stock.finance.sina.com.cn/q/view/vFutures_History.php?page=28&breed=JD0&start=2010-01-01&end=2018-05-20&jys=dce&pz=JD&hy=JD0&type=inner&name=%E5%A4%A7%E8%B1%861109", encoding = 'GB2312')
t1<-tdist %>%html_table(fill = TRUE)
write.csv(t1,"D:/qaac.csv")

2.想想有啥新方法,可以绕过雷区,毕竟小脾气对直男来说是无解的。以下是新方法
library(rvest)
f <- tempfile()#创建临时文件
download.file("http://vip.stock.finance.sina.com.cn/q/view/vFutures_History.php?page=28&breed=JD0&start=2010-01-01&end=2018-05-20&jys=dce&pz=JD&hy=JD0&type=inner&name=%E5%A4%A7%E8%B1%861109", f)
fchars <- readChar(f, file.info(f)$size)
stringi::stri_enc_detect(fchars)
# 发现文档是 GB18030 编码,上面那个方法就是这个编码问题
futf8 <- stringi::stri_encode(fchars, "GB18030", "UTF8")
fhtml <- rvest::html(futf8)
#以上全为重新为网页编码的过程,此法可通用

t1<-fhtml %>%html_table(fill = TRUE)

t1<-t1[[4]]#定位表格位置
write.csv(t1,"D:/0qaac.csv")
哪位大神如果知道第一种方法该如何处理,欢迎留言,感激涕零。



猜你喜欢

转载自blog.csdn.net/weixin_41725746/article/details/80382265
今日推荐