| 參考:https://www.douban.com/note/618385613/,因為原文中沒有轉(zhuǎn)化,就自己操作一下,已記錄學(xué)習(xí)的過程,向原作者致敬。這里將不能運行的代碼修改了一下。
1,rvest包簡介和安裝
rvest包是hadley大神的又一力作,使用它能更方便地提取網(wǎng)頁上的信息,包括文本、數(shù)字、表格等,本文對rvest包的運用做一個詳細介紹,希望能夠幫助你在網(wǎng)頁抓取的武器庫中新添一把利器。
從CRAN上安裝發(fā)行版:
install.packages("rvest")
也可以從github上安裝開發(fā)版本:
install.packages("devtools")
devtools::install_github("hadley/rvest")
2,rvest用法簡介
下面對rvest包中的主要函數(shù)的功能做一下說明:
read_html(): 讀取html文檔的函數(shù),其輸入可以是線上的url,也可以是本地的html文件,甚至是包含html的字符串也可以。
html_nodes(): 選擇提取文檔中制定元素的部分??梢允褂胏ss selectors,例如html_nodes(doc, "table td");也可以使用xpath selectors,例如html_nodes(doc, xpath = "http://table//td")。
html_tag(): 提取標(biāo)簽名稱;
html_text(): 提取標(biāo)簽內(nèi)的文本;
html_attr(): 提取指定屬性的內(nèi)容;
html_attrs(): 提取所有的屬性名稱及其內(nèi)容;
html_table(): 解析網(wǎng)頁數(shù)據(jù)表的數(shù)據(jù)到R的數(shù)據(jù)框中。
html_form(),set_values()和submit_form() 分別表示提取、修改和提交表單。
3,rvest抓取示例
網(wǎng)頁抓取的一般步驟我總結(jié)有如下三步:
首先,明確所要抓取的內(nèi)容
然后,通過html的標(biāo)簽名稱、屬性以及id等確切的描述定位到該內(nèi)容的位置
最后,提取相關(guān)的內(nèi)容信息,必要時再做一些數(shù)據(jù)處理
3.1 :提取新浪NBA新聞標(biāo)題
網(wǎng)址:http://sports.sina.com.cn/nba/

使用SelectorGadget的谷歌插件,點擊標(biāo)題,復(fù)制nodes:

library(rvest)
library(stringr)
library(XML)
library(xml2)
url='http://sports.sina.com.cn/nba/'
web=read_html(url)
title <- html_nodes(web,".item p a")%>%html_text
head(title)
結(jié)果:
> head(title)
[1] "20中5三分全丟!毒瘤瓜再上線 想解毒必用這招?"
[2] "威少準(zhǔn)三雙雷霆慘遭逆轉(zhuǎn) 替補逆天籃網(wǎng)兩連勝"
[3] "全能球哥今日上線!末節(jié)細節(jié)倆操作是勝負手"
[4] "天賦大戰(zhàn)!十個跑跳長手怪的比賽你要跪著看"
[5] "隊史唯2表現(xiàn)換不到勝利 末節(jié)16分大帝真盡力了"
[6] "比爾34分奇才再下一城 布克缺陣太陽功虧一簣"
插件的使用參見
vignette("selectorgadget")
以上提取的主要是文本信息,對于網(wǎng)頁中表格中的數(shù)據(jù)可以直接采用html_table()來獲取。
3.2 提取網(wǎng)站中的表格信息
網(wǎng)址:http://hz.house.ifeng.com/detail/2014_10_28/50087618_1.shtml

因為網(wǎng)頁里面有表格,這里直接讀取表格,然后提取即可。
library(rvest)
library(magrittr)
url <- 'http://hz.house.ifeng.com/detail/2014_10_28/50087618_1.shtml'
content <- read_html(url)
trade <- html_table(content, header = TRUE)[[1]]
names(trade) <- trade[1,]
trade <- trade[-1,]
write.xlsx(trade,"d:/trade.xlsx")
結(jié)果:

獲取節(jié)點信息
用%>% 符號進行層級劃分。web就是之前存儲網(wǎng)頁信息的變量,所以我們從這里開始,然后html_nodes()函數(shù)獲取網(wǎng)頁里的相應(yīng)節(jié)點。在下面代碼里我簡單的重現(xiàn)了原網(wǎng)頁里的一個層級結(jié)構(gòu)??梢钥吹?,實際上我們要爬取的信息在25個class屬性為pl的<p>標(biāo)簽里的文本。
# <p class=pl>
# [清] 曹雪芹 著 / 人民文學(xué)出版社 / 1996-12 / 59.70元
# </p>
而對于這樣的結(jié)構(gòu),在htmlnodes()函數(shù)里的寫法就是簡單的 "p.pl",其中“.”表示class屬性的值,如果是id屬性則用“#”,如果大家學(xué)過CSS選擇器就很好理解了,是完全一致的。
最后我們用html_text()函數(shù)表示獲取文本信息,否則返回的是整個<p>標(biāo)簽。 總體上用以下一行代碼就可以實現(xiàn):
position<-web %>% html_nodes("p.pl") %>% html_text()
position
想要學(xué)習(xí)更多,我們可以在Rstudio里的命令行輸入如下代碼查詢html_nodes()函數(shù)的相關(guān)用法:
?html_nodes
Rvest這個包的說明文檔里給出了一些其他例子
library(rvest)
library(stringr)
library(xml2)
ateam <- read_html("http://www.boxofficemojo.com/movies/?id=ateam.htm")
# 然后下面代碼分別獲取了ateam這個網(wǎng)頁里<center>標(biāo)簽里<td>的全部內(nèi)容
ateam %>% html_nodes("center") %>% html_nodes("td")
# {xml_nodeset (7)}
# [1] <td align="center" colspan="2"><font size="4">Domestic Tot ...
# [2] <td valign="top">Distributor: <b><a href="/studio/chart/?s ...
# [3] <td valign="top">Release Date: <b><nobr><a href="/schedule ...
# [4] <td valign="top">Genre: <b>Action</b>\n</td>\n
# [5] <td valign="top">Runtime: <b>1 hrs. 57 min.</b>\n</td>
# [6] <td valign="top">MPAA Rating: <b>PG-13</b>\n</td>\n
# [7] <td valign="top">Production Budget: <b>$110 million</b>\n< ...
# 獲取了ateam這個網(wǎng)頁里<center>標(biāo)簽里<font>的全部內(nèi)容
ateam %>% html_nodes("center") %>% html_nodes("font")
#[1] <font size="4">Domestic Total Gross: <b>$77,222,099</b></font>
# 接著官方例子中還給出了獲取特定序位的html標(biāo)簽的方法,用到了magrittr包里的extract2函數(shù):
library(magrittr)
#下面兩行代碼都可以獲得該網(wǎng)頁中第一個<table>標(biāo)簽(由extract2(1)或`[[`(1)獲?。┲械乃?lt;img>標(biāo)簽里的內(nèi)容,
ateam %>% html_nodes("table") %>% extract2(1) %>% html_nodes("img")
ateam %>% html_nodes("table") %>% `[[`(1) %>% html_nodes("img")
# 運行結(jié)果如下:
# {xml_nodeset (6)}
# [1] <img src="https://images-na.ssl-images-amazon.com/images/M/MV5BMTc4ODc4NTQ1N15BMl5B ...
# [2] <img src="http://www.assoc-amazon.com/e/ir?t=boxofficemojo-20&amp;l=as2&o=1& ...
# [3] <img src="http://www.assoc-amazon.com/e/ir?t=boxofficemojo-20&amp;l=as2&o=1& ...
# [4] <img src="/img/misc/bom_logo1.png" width="245" height="56" alt="Box Office Mojo"/>
# [5] <img src="/img/misc/IMDbSm.png" width="34" height="16" alt="IMDb" valign="middle"/>
# [6] <img src="http://b.scorecardresearch.com/p?c1=2&amp;c2=6034961&cv=2.0&cj=1"/>
#同理我們也可以獲得網(wǎng)頁里前兩個<table>標(biāo)簽儲存的所有<img>標(biāo)簽里的內(nèi)容:
ateam %>% html_nodes("table") %>% `[`(1:2) %>% html_nodes("img")
ateam %>% html_nodes("table") %>% extract(1:2) %>% html_nodes("img")
3.3 用rvest從趕集網(wǎng)抓取二手房單頁面數(shù)據(jù)
gurl <- "http://cs.ganji.com/fang5/yuhuashazitang/o1/"
getData <- function(gurl){
# 抓取趕集網(wǎng)二手房源單頁的數(shù)據(jù)
library(rvest)
# 趕集網(wǎng)首頁篩選長沙-雨花區(qū)-砂子塘的二手房源,獲得鏈接,o1為頁數(shù)
tmp <- gurl %>% html_session %>%
read_html(encoding="utf-8") %>%
html_nodes("div.f-main-list>div>div")
# 單個房源的puid
puid <- tmp %>% html_attr("id")
# 單個房源的鏈接
itemURL <-tmp %>% html_attr("href") %>%
gsub(pattern="/fang5",replacement="http://cs.ganji.com/fang5")
# 縮略圖鏈接
smallImg <- tmp %>% html_nodes("dl>dt>div>a>img") %>% html_attr("src")
# 標(biāo)題
iTitle <- tmp %>% html_nodes("dl>dd>a") %>% html_attr("title")
# 戶型
iLayout <- tmp %>% html_nodes("dl>dd[data-huxing]") %>% html_attr("data-huxing")
# 面積
iArea <- tmp %>% html_nodes("dl>dd[data-huxing]") %>%
html_attr("data-area") %>%
gsub(pattern="[^0-9]",replacement="")
# 篩選朝向等數(shù)據(jù)
iTmp <- tmp %>% html_nodes("dl>dd[data-huxing]>span") %>% html_text
iOrientation <- iTmp[seq(from=5,to=length(iTmp),by=9)] # 提取朝向
iFloor <- iTmp[seq(from=7,to=length(iTmp),by=9)] %>% # 提取樓層
gsub(pattern="\n",replacement="")
iDecoration <- iTmp[seq(from=9,to=length(iTmp),by=9)] # 提取裝修
# 提取地址
iAddr <- tmp %>% html_nodes("dl>dd>span.area") %>% html_text %>%
gsub(pattern="\n",replacement=" ") %>%
gsub(pattern=" ",replacement="")
# 提取價格
iPrice <- tmp %>% html_nodes("dl>dd>div.price>span:first-child") %>% html_text
# 提取單價
iTime <- tmp %>% html_nodes("dl>dd>div.time") %>% html_text %>%
gsub(pattern="[^0-9]",replacement="") %>% as.numeric
# 合并數(shù)據(jù)框
iData <- data.frame(puid=puid,
iLayout=iLayout,
iArea=iArea,
iPrice=iPrice,
iTime=iTime,
# iDecoration=iDecoration,
iFloor=iFloor,
iOrientation=iOrientation,
itemURL=itemURL,
smallImg=smallImg,
iTitle=iTitle,
iAddr=iAddr,
stringsAsFactors=FALSE)
# 返回數(shù)據(jù)框
return(iData)
}
result <- getData(gurl)
write.xlsx(result,"d:/result.xlsx")
結(jié)果:

3.4 如何用rvest包爬取豆瓣圖書250的數(shù)據(jù)-例2
用R中的rvest包爬取豆瓣top250圖書的基本信息(包括書名、評分、作者、譯者、出版社、出版時間,價格),然后根據(jù)出版社和出版時間進行進一步的分析
for (i in 1:length(ind)){
web<-read_html(str_c("https://book.douban.com/top250?start=",ind[i]),encoding="UTF-8")
#爬取圖書的作者、出版社、出版時間、價格
p <-web%>%html_nodes("p.pl")%>%html_text()
#爬取圖書的name
q <-web%>%html_nodes(".pl2 a")%>%html_text()
#爬取圖書的rate
r <- web%>%html_nodes(".rating_nums")%>%html_text()
#消除圖書的name q中的空格
q <-str_replace_all(q," ","")
q <-str_replace_all(q,"\n","")
#賦值圖書書名和評分
name <- q
rate <- r
#對p進行處理
#提取作者名字
p1 <-str_split_fixed(p,"/",2)
author <- p1[,1];
#提取翻譯者名字
p2 <-str_split_fixed(p1[,2],"/",2)
a <-str_detect(p2[,1],"出版")
b <- str_detect(p2[,1],"書店")
interpre <-p2[,1]
interpre[a|b] <-"NA"
#提取出版商
p3 <-str_split_fixed(p2[,2],"/",2)
publisher <-p3[,1]
publisher[a] <-p2[a,1]
publisher[b] <-p2[b,1]
#提取出版時間
p4 <-str_split_fixed(p3[,2],"/",2)
publish_time <-p4[,1]
publish_time[a]<- p3[a,1]
publish_time[b]<- p3[b,1]
publish_time <- str_replace(publish_time,"年","-")
publish_time <- str_replace(publish_time,"月","-")
#提取價格
p5 <-str_split_fixed(p4[,2],"/",2)
price <- p5[,1]
price[a] <-p4[a,1]
price[b] <-p4[b,1]
#創(chuàng)建數(shù)據(jù)框存儲以上信息
book <-data_frame(name,rate,author,interpre,publisher,publish_time,price)
book_inf <-rbind(book_inf,book)
}
write.xlsx(book_inf,"d:/book.xlsx")
結(jié)果:

3.5 爬取了戴申在科學(xué)網(wǎng)博客
# 1 此均值非彼均值
# 2 [轉(zhuǎn)載]孔丘、孔子、孔老二,它究竟是一只什么鳥?
# 3 大數(shù)據(jù)分析之——k-means聚類中的坑
# 4 大數(shù)據(jù)分析之——足彩數(shù)據(jù)趴取
# 5 [轉(zhuǎn)載]老王這次要攤事了,當(dāng)年他主管的部門是事被重新抖出來。
# 6 [轉(zhuǎn)載]黨衛(wèi)軍是這樣抓人的。
# posttime count_of_read
# 1 2015-03-08 216 次閱讀
# 2 2015-02-10 190 次閱讀
# 3 2015-01-18 380 次閱讀
# 4 2015-01-10 437 次閱讀
# 5 2015-01-05 480 次閱讀
# 6 2015-01-05 398 次閱讀
# paperlink
# 1 http://blog.sciencenet.cn/blog-556556-872813.html
# 2 http://blog.sciencenet.cn/blog-556556-866932.html
# 3 http://blog.sciencenet.cn/blog-556556-860647.html
# 4 http://blog.sciencenet.cn/blog-556556-858171.html
# 5 http://blog.sciencenet.cn/blog-556556-856705.html
# 6 http://blog.sciencenet.cn/blog-556556-856640.html
write.table(final,"final.csv",fileEncoding="GB2312")
#抓取的數(shù)據(jù)需要在Excel進一步加工,加工后讀取進來,進一步做分析
a <- read.table("dai_shen_blog_0326.csv",header=TRUE,sep=";",fileEncoding="GB2312")#Mac OS 環(huán)境下,要sep=";"
a$posttime <- as.Date(a$posttime)
a$paperlink <- as.character(a$paperlink)
a$papername <- as.character(a$papername)
a$count_of_read_NO. <- as.numeric(a$count_of_read_NO.)
library(ggplot2)
qplot(posttime,count_of_read_NO.,data=a,geom="point",colour=repost,size=6)
3.7 爬取了NBA 2014-2015常規(guī)賽技術(shù)統(tǒng)計排行 - 得分榜
#Crawl NBA player statistics from sina
#web http://nba.sports.sina.com.cn/playerstats.php?s=0&e=49&key=1&t=1
library(rvest)
library(stringr)
library(sqldf)
rm(NBAdata)
start <- seq(0,250,50)
end <- seq(49,299,50)
getdata <- function(i){
url <- paste0('http://nba.sports.sina.com.cn/playerstats.php?s=',start[i],'&e=',end[i],'&key=1&t=1')
rank <- url %>% html_session() %>% html_nodes("table") %>% .[[2]] %>% html_nodes("td:nth-child(1)") %>% html_text()%>%.[-1]%>%as.numeric()
player <- url %>% html_session() %>% html_nodes("table") %>% .[[2]] %>% html_nodes("td:nth-child(2)") %>% html_text()%>%.[-1]%>%str_sub(9,100)%>%as.character()
team <- url %>% html_session() %>% html_nodes("table") %>% .[[2]] %>% html_nodes("td:nth-child(3)") %>% html_text()%>%.[-1]%>%str_sub(9,100)%>%as.character()
avg_score <- url %>% html_session() %>% html_nodes("table") %>% .[[2]] %>% html_nodes("td:nth-child(4)") %>% html_text()%>%.[-1]
total_score <- url %>% html_session() %>% html_nodes("table") %>% .[[2]] %>% html_nodes("td:nth-child(5)") %>% html_text()%>%.[-1]
total_shoot <- url %>% html_session() %>% html_nodes("table") %>% .[[2]] %>% html_nodes("td:nth-child(6)") %>% html_text()%>%.[-1]
three_point <- url %>% html_session() %>% html_nodes("table") %>% .[[2]] %>% html_nodes("td:nth-child(7)") %>% html_text()%>%.[-1]
punish_point <- url %>% html_session() %>% html_nodes("table") %>% .[[2]] %>% html_nodes("td:nth-child(8)") %>% html_text()%>%.[-1]
avg_time <- url %>% html_session() %>% html_nodes("table") %>% .[[2]] %>% html_nodes("td:nth-child(9)") %>% html_text()%>%.[-1]
total_involve <- url %>% html_session() %>% html_nodes("table") %>% .[[2]] %>% html_nodes("td:nth-child(10)") %>% html_text()%>%.[-1]
data.frame(rank,player,team,avg_score,total_score,total_shoot,three_point,punish_point,avg_time,total_involve)
}
NBAdata <- data.frame()
for(i in 1:6){
NBAdata <- rbind(NBAdata,getdata(i))
}
NBAdata <- sqldf("select distinct * from NBAdata")
write.table(NBAdata,"NBAdata.csv",sep=",",fileEncoding="GB2312")
head(NBAdata)
# rank player team avg_score total_score
# 1 1 拉塞爾-威斯布魯克 雷霆 27.3 1556
# 2 2 詹姆斯-哈登 火箭 27.1 1900
# 3 3 勒布朗-詹姆斯 騎士 25.8 1600
# 4 4 安東尼-戴維斯 鵜鶘 24.6 1403
# 5 5 德馬庫斯-考辛斯 國王 23.8 1308
# 6 6 斯蒂芬-庫里 勇士 23.4 1618
# total_shoot three_point punish_point avg_time
# 1 42.7% 30.1% 84.6% 33.8
# 2 44% 36.8% 86.6% 36.8
# 3 49.2% 35.4% 71.9% 36.2
# 4 54.5% 10% 81.4% 36.2
# 5 46.5% 28.6% 80.2% 33.7
# 6 47.9% 42.2% 91.4% 32.9
# total_involve
# 1 57
# 2 70
# 3 62
# 4 57
# 5 55
# 6 69
3.8 拉勾網(wǎng)爬了一下
library(rvest)
lagou <- "http://www.lagou.com/jobs/list_%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90?kd=%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90&spc=2&pl=&gj=&xl=&yx=&gx=&st=&labelWords=&lc
=&workAddress=&city=%E6%B7%B1%E5%9C%B3&requestId=&pn=3"
web<-html(lagou,encoding="UTF-8") #讀取數(shù)據(jù),規(guī)定編碼
#之前我是用關(guān)鍵字搜索,閱讀html代碼,獲得html_nodes里需要什么屬性,不過許多瀏覽器有開發(fā)者工具,可以直接獲得層級信息。如遨游
position<-web %>% html_nodes("li div.hot_pos_l a") %>% html_text()
#上面就是直接讀取數(shù)據(jù),獲得位置信息
#不過在后面做其他網(wǎng)站時發(fā)現(xiàn),有時候信息儲存在同類數(shù)據(jù)里(如div沒有class等等),建議是找一個大的分類,先獲得表格信息,再做數(shù)據(jù)
list_lagou<-web %>% html_nodes("li.clearfix")
#這里正確找準(zhǔn)正確的劃分點很重要。有<li class="odd clearfix">,其實用li.clearfix一樣可以取(對于空格二選一,如"li.odd"或者"li.clearfix")
#接下來的company/position照選即可,因為事先已經(jīng)分好了list,所以每一個出多少心里有數(shù)。。
在講完原理之后,現(xiàn)在開始嘗試寫代碼,因為里面涉及太多的選取數(shù)據(jù)工作。為了避免出現(xiàn)太多變量,我最后是編了一個函數(shù),輸出數(shù)據(jù)庫。
#下面開始寫代碼,首先寫一個函數(shù)getdata,會輸出一個數(shù)據(jù)框
getdata<-function(page,urlwithoutpage){
url=paste0(urlwithoutpage,page) #這里輸入拉勾網(wǎng)沒有頁碼的url
web<-html(url,encoding="UTF-8") #讀取數(shù)據(jù),規(guī)定編碼,access用
list_lagou<-web %>% html_nodes("li.clearfix") #獲得一個清單,15個職位
title<-list_lagou %>% html_nodes("div.hot_pos_l div.mb10 a")%>%html_text()
company<-list_lagou %>% html_nodes("div.hot_pos_r div.mb10 a")%>%html_text()
link<-gsub("\\?source\\=search","",list_lagou %>% html_nodes("div.hot_pos_l div.mb10 a")%>%html_attr("href"))
#接下來的由于數(shù)據(jù)都存在span里,沒有很好的劃分。這個取數(shù)要復(fù)雜一些。我在這里,研究他們的表,先取15個完整list,然后用seq等序列取數(shù)
#之后要研究是否有更好的方法
#如果有table,可以直接用data.table取數(shù)更快。。。
temp<-list_lagou %>% html_nodes("div.hot_pos_l span")
city<-temp[seq(1,90,by=6)] %>% html_text()
salary<-gsub("月薪:","",temp[seq(2,90,by=6)]%>% html_text())
year<-gsub("經(jīng)驗:","",temp[seq(3,90,by=6)]%>% html_text())
degree<-gsub("最低學(xué)歷:","",temp[seq(4,90,by=6)]%>%html_text())
benefit<-gsub("職位誘惑:","",temp[seq(5,90,by=6)]%>% html_text())
time<-temp[seq(6,90,by=6)]%>%html_text()
data.frame(title,company,city,salary,year,degree,benefit,time,link)
}
獲取函數(shù),這里先爬一頁!
url<-"http://www.lagou.com/jobs/list_%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90?kd=%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90&spc=2&pl=&gj=&xl=&yx=&gx=&st=&labelWords=&lc=&workAddress=&city=%E6%B7%B1%E5%9C%B3&requestId=&pn="
final<-data.frame()
for (i in 3){
final<-rbind(final,getdata(i,url))
} #定義個數(shù),把上面的getdata得到的Data.frame合并
head(final)
3.9 R的爬蟲和回歸模型案例-以北京自如房租價格為例
library(rvest)
library(stringr)
library(XML)
library(xml2)
WebSpider <- function(m){
url <- str_c(cp,"?p=",m)
web <- read_html(url,encoding = "UTF-8")#抓取網(wǎng)頁信息
name_rough <- web %>% html_nodes("h3") %>% html_text() #獲取粗房屋名
area_rough <- web %>% html_nodes("h4") %>% html_text() #提取區(qū)位
price_rough <- web %>% html_nodes("p.price") %>% html_text() #提取價格
price <- str_extract(price_rough, "[0-9]+") %>% as.numeric()#提取精確價格
detail <- web %>% html_nodes("div.detail") %>% html_text() #提取其他信息
#合并成數(shù)據(jù)框
data.frame(name_rough,area_rough,forward,mate_num,location,price,detail)
# 4、接著是觀察翻頁規(guī)律,然后遇到了一個坑。原以為之后的頁碼不過是http://www.ziroom.com/z/nl/z3.html的基礎(chǔ)上加上/1、/2........,我依照這個思路抓了一番,也獲得了將近4000多條數(shù)據(jù),原以為這大概就是全部吧。但我仔細看數(shù)據(jù)才發(fā)現(xiàn),這樣下來的基本都是房山、大興和通州等的數(shù)據(jù),基本沒有城六區(qū)的,城六區(qū)的只有選了區(qū)域選項后才會出現(xiàn):
dc <- "http://www.ziroom.com/z/nl/z3-d23008614.html"
xc <- "http://www.ziroom.com/z/nl/z3-d23008626.html"
cy <- "http://www.ziroom.com/z/nl/z3-d23008613.html"
hd <- "http://www.ziroom.com/z/nl/z3-d23008618.html"
ft <- "http://www.ziroom.com/z/nl/z3-d23008617.html"
sjs <- "http://www.ziroom.com/z/nl/z3-d23008623.html"
cp <- "http://www.ziroom.com/z/nl/z3-d23008611.html"
# 這樣一來,只有逐區(qū)的來進行翻頁爬了。為此,只能選定部分區(qū)域來做分析了。
results_cp <- data.frame()
for(m in 1:118){ #118為昌平區(qū)信息的總頁碼
results_cp <- rbind(results_cp,WebSpider(m))#合并單個區(qū)每一次循環(huán)輸出的數(shù)據(jù)
}
#依次重復(fù)獲得7個區(qū)的數(shù)據(jù)
results <- rbind(results_cp, results_cy,results_dc,results_ft,
results_hd,results_sjs,results_xc) #將所有各區(qū)數(shù)據(jù)的合并
}
有沒有關(guān)于R語言爬蟲的書籍或者資源?
有一本《基于R語言的自動數(shù)據(jù)收集》翻譯得相當(dāng)好# 從零開始學(xué)習(xí)rvest網(wǎng)絡(luò)爬蟲抓數(shù)據(jù)-Stone.Hou 2017/5/1
[大神 Hadley rvest in GitHub] https://github.com/hadley/rvest