最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】R语言出错:Error: could not find function "htmlParse"

R crifan 10647浏览 0评论

【问题】

折腾:

【记录】尝试用R语言去抓取网页和提取信息

期间,用如下代码:

> retHtml <- getURL("http://www.yiteng365.com/commodity.do?id=5708&ispng=")
> parsedHtml = htmlParse(retHtml, asText=TRUE,encoding="GBK")
Error: could not find function "htmlParse"

结果出错:

Error: could not find function "htmlParse"

如图:

r lan Error could not find function htmlParse

【解决过程】

1.本来打算参考之前的:

【已解决】运行R语言出错:Error: could not find function "getURL"

去安装库的,但是发现,不知道htmlParse属于哪个库。

2.后来是搜:

R语言 htmlParse

而找到:

R语言:读取淘宝的单品页的名称和价格;*网页爬虫-R语言实现 – R中国用户组-炼数成金-Dataguru专业数据分析社区

而想到是XML库的。

所以再去安装一下XML库。

3.代码:

> install.packages("XML")
trying URL 'http://mirrors.ustc.edu.cn/CRAN/bin/windows/contrib/3.0/XML_3.98-1.1.zip'
Content type 'application/zip' length 4288136 bytes (4.1 Mb)
opened URL
downloaded 4.1 Mb

package ‘XML’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
        C:\Users\CLi\AppData\Local\Temp\RtmpIRchyS\downloaded_packages
> 

如图:

 

downloading xml lib for r language

installed xml lib of r lan

4.然后再试试之前的代码:

结果还是不行:

> parsedHtml = htmlParse(retHtml, asText=TRUE,encoding="GBK")
Error: could not find function "htmlParse"

5.想起来参考别人代码要先去导入此库,然后再运行就可以了:

> require("XML")
Loading required package: XML
> parsedHtml = htmlParse(retHtml, asText=TRUE,encoding="GBK")

如图:

after import xml then htmlparse ok

 

【总结】

1.htmlParse是R语言中的XML库中的函数。

2.使用函数之前,需要导入对应的库,写法是:

require("XML")

转载请注明:在路上 » 【已解决】R语言出错:Error: could not find function "htmlParse"

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.163 seconds, using 21.97MB memory