rvest包简介
rvest包是Hadley Wickham大神开发的一个专门用于网络数据抓取的R语言包,目前的发行版本为0.3.2,关于rvest包的描述以及用法可参考rvest帮助文档,花上一点时间阅读帮助文档,相信你就可以写出自己的爬虫了。
help(package=“rvest”)
rvest帮助文档: http://127.0.0.1:17483/library/rvest/html/00Index.html
csdn中文版版:
http://blog.csdn.net/sadfasdgaaaasdfa/article/details/45372307
R语言链家爬虫:
#加载所需的包 rm(list=ls()) library("xml2") library("rvest") library("dplyr") library("stringr") #对爬取页数进行设定并创建数据框 i<-1:10 house_inf<-data.frame() #使用for循环进行批量数据爬取(发现url的规律,写for循环语句) for (i in 1:10){ web<- read_html(str_c("http://hz.lianjia.com/ershoufang/pg",i),encoding="UTF-8") #用SelectorGadget定位节点信息并爬取房名 house_name<-web%>%html_nodes(".houseInfo a")%>%html_text() #爬取二手房基本信息并消除空格 house_basic_inf<-web%>%html_nodes(".houseInfo")%>%html_text() house_basic_inf<-str_replace_all(house_basic_inf," ","") #SelectorGadget定位节点信息爬取地址 house_address<-web%>%html_nodes(".positionInfo a")%>%html_text() #SelectorGadget定位节点信息爬取总价 house_totalprice<-web%>%html_nodes(".totalPrice")%>%html_text() #SelectorGadget定位节点信息爬取单价 house_unitprice<-web%>%html_nodes(".unitPrice span")%>%html_text() #创建数据框存储以上信息 house<-data_frame(house_name,house_basic_inf,house_address,house_totalprice,house_unitprice) house_inf<-rbind(house_inf,house) } #将数据写入csv文档 write.csv(house_inf,file="D:/house_inf.csv") #总共抓取了链家杭州二手房100个页面3000条房价信息