R爬蟲實(shí)踐—抓取國(guó)自然基金信息【上篇】和R爬蟲實(shí)踐—抓取國(guó)自然基金信息【中篇】都是對(duì)國(guó)自然數(shù)據(jù)的局部抓取,突然發(fā)現(xiàn)費(fèi)這勁也是蠻傻的尺碰,直接爬取所有信息不是更省事嘛挣棕,哎,有時(shí)候真的不能怪別人說(shuō)自己軸了亲桥。
首先洛心,打開(kāi)網(wǎng)頁(yè)http://fund.zsci.com.cn/,在學(xué)科分類中選擇醫(yī)學(xué)科學(xué)部题篷,批準(zhǔn)年度選擇2019皂甘,得到約8000多條國(guó)自然信息。
具體如何爬鹊看铡偿枕?
爬取策略上和上兩篇有不同之處,之前都是直接爬取下圖左側(cè)中的信息户辫,這次想讓信息更為全面渐夸,進(jìn)入每條國(guó)自然詳情頁(yè)面進(jìn)行的信息抓取。
首先渔欢,獲取首頁(yè)國(guó)自然信息的詳情頁(yè)鏈接墓塌,
#進(jìn)入總網(wǎng)頁(yè)第一頁(yè)
url1<-c("http://fund.zsci.com.cn/Index/index/xk_name/%E5%8C%BB%E5%AD%A6%E7%A7%91%E5%AD%A6%E9%83%A8/xkid/3416/start_year/2019/end_year/2019/xmid/0/search/1/p/1.html")
#讀取網(wǎng)頁(yè)內(nèi)容
web <- read_html(url1,encoding = "utf-8")
#獲取第一頁(yè)所有記錄的網(wǎng)址信息
link <- web %>% html_nodes('ul.layuiadmin-card-status a') %>% html_attr("href")
link <- paste0("http://fund.zsci.com.cn/",link)
link
然后,獲取檢索結(jié)果最大頁(yè)碼奥额,
#進(jìn)入總網(wǎng)頁(yè)第一頁(yè)
url1<-c("http://fund.zsci.com.cn/Index/index/xk_name/%E5%8C%BB%E5%AD%A6%E7%A7%91%E5%AD%A6%E9%83%A8/xkid/3416/start_year/2019/end_year/2019/xmid/0/search/1/p/1.html")
#讀取網(wǎng)頁(yè)內(nèi)容
web <- read_html(url1,encoding = "utf-8")
lastpage_link <- web %>% html_nodes("div.layui-box a") %>% html_attr("href")
lastpage_link <- paste0("http://fund.zsci.com.cn/",lastpage_link[length(lastpage_link)])
#獲取總頁(yè)數(shù)
lastpage_web <- read_html(lastpage_link,encoding = "utf-8")
lastpage_number <- lastpage_web %>% html_nodes("div.layui-box span.current") %>% html_text() %>% as.integer()
最后苫幢,正式開(kāi)始爬取~~~ 具體思路過(guò)程:先爬取首頁(yè)信息的詳情頁(yè)鏈接———利用循環(huán)獲取首頁(yè)20個(gè)詳情頁(yè)面內(nèi)的信息——然后開(kāi)始第二頁(yè)(如此循環(huán)往復(fù))——直到最后一頁(yè)的最后一條信息。
i=1
site <- 'http://fund.zsci.com.cn/Index/index/xk_name/%E5%8C%BB%E5%AD%A6%E7%A7%91%E5%AD%A6%E9%83%A8/xkid/3416/start_year/2019/end_year/2019/xmid/0/search/1/p/'
#創(chuàng)建一個(gè)空的數(shù)據(jù)框用來(lái)存儲(chǔ)抓取的數(shù)據(jù)
results <- data.frame(ID="基金號(hào)",Title="題目",Author="負(fù)責(zé)人",Position="職稱",Department="申請(qǐng)單位",
Type="研究類型",Project="項(xiàng)目批準(zhǔn)號(hào)",Date="批準(zhǔn)年度",Money="金額",
dateline="研究期限",Keywords_chinese="中文關(guān)鍵詞",Keywords_english="英文關(guān)鍵詞")
for(i in 1:lastpage_number){
url <-paste0(site,i,".html")
web <- read_html(url)
#獲取第一頁(yè)所有記錄的網(wǎng)址信息
link <- web %>% html_nodes('ul.layuiadmin-card-status a') %>% html_attr("href") #獲取了負(fù)責(zé)人和單位信息
link <- paste0("http://fund.zsci.com.cn/",link)
a=1
for (a in 1:length(link)) {
link_web <- read_html(link[a],encoding = "utf-8")
#---獲得基金標(biāo)題---
Title <- link_web %>% html_nodes('div h2') %>% html_text() # 標(biāo)題內(nèi)容解析
Title
#---獲得眾多信息---
Information <- link_web %>% html_nodes('div.layui-table-body div') %>% html_text()
Information
#---獲取基金ID號(hào)---
ID <- Information[seq(3,length(Information),11)]
ID
#---獲取負(fù)責(zé)人信息---
Author <- Information[seq(4,length(Information),11)]
Author
#---負(fù)責(zé)人職稱---
Position <- Information[seq(5,length(Information),11)]
Position
#---獲得申請(qǐng)單位---
Department <- Information[seq(6,length(Information),11)]
Department
#---獲取研究類型---
jijintype <- Information[seq(8,length(Information),11)]
jijintype
#---獲得項(xiàng)目號(hào)---
Project <- Information[seq(1,length(Information),11)]
Project
#---獲取批準(zhǔn)時(shí)間---
Date <- Information[seq(2,length(Information),11)]
Date
#---獲取基金金額---
Money <- Information[seq(7,length(Information),11)]
Money
#---獲取研究期限---
dateline <- Information[seq(9,length(Information),11)]
dateline
#---獲取關(guān)鍵詞---
Keywords_chinese <- Information[seq(10,length(Information),11)]
Keywords_chinese
Keywords_english <- Information[seq(11,length(Information),11)]
Keywords_english
result <- data.frame(ID=ID,Title=Title,Author=Author,Position=Position,Department=Department,
Type=jijintype,Project=Project,Date=Date,Money=Money,dateline=dateline,
Keywords_chinese=Keywords_chinese,Keywords_english=Keywords_english)
#合并某一頁(yè)面數(shù)據(jù)成數(shù)據(jù)框
results <- rbind(results,result)
}
}
最終爬取結(jié)果如下垫挨,由于信息較多韩肝,爬取速度會(huì)慢一些。
往期回顧
R爬蟲在工作中的一點(diǎn)妙用
R爬蟲必備基礎(chǔ)——HTML和CSS初識(shí)
R爬蟲必備基礎(chǔ)——靜態(tài)網(wǎng)頁(yè)+動(dòng)態(tài)網(wǎng)頁(yè)
R爬蟲必備——rvest包的使用
R爬蟲必備基礎(chǔ)——CSS+SelectorGadget
R爬蟲必備基礎(chǔ)—Chrome開(kāi)發(fā)者工具(F12)
R爬蟲必備基礎(chǔ)—HTTP協(xié)議
R爬蟲必備—httr+POST請(qǐng)求類爬蟲(網(wǎng)易云課堂)
R爬蟲必備基礎(chǔ)—rvest為什么不用于動(dòng)態(tài)網(wǎng)頁(yè)九榔?
R爬蟲必備——httr+GET請(qǐng)求類爬蟲(解螺旋課程)
R爬蟲實(shí)戰(zhàn)—抓取PubMed文章的基本信息
R爬蟲實(shí)踐—抓取國(guó)自然基金信息【上篇】
R爬蟲實(shí)踐—抓取國(guó)自然基金信息【中篇】