美文网首页
R爬虫实践—抓取国自然基金信息【下篇】

R爬虫实践—抓取国自然基金信息【下篇】

作者: Clariom | 来源:发表于2020-08-26 11:14 被阅读0次

    R爬虫实践—抓取国自然基金信息【上篇】和R爬虫实践—抓取国自然基金信息【中篇】都是对国自然数据的局部抓取,突然发现费这劲也是蛮傻的,直接爬取所有信息不是更省事嘛,哎,有时候真的不能怪别人说自己轴了。

    首先,打开网页http://fund.zsci.com.cn/,在学科分类中选择医学科学部,批准年度选择2019,得到约8000多条国自然信息。

    image

    具体如何爬取?

    爬取策略上和上两篇有不同之处,之前都是直接爬取下图左侧中的信息,这次想让信息更为全面,进入每条国自然详情页面进行的信息抓取。

    image

    首先,获取首页国自然信息的详情页链接,

    #进入总网页第一页
    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")
    #读取网页内容
    web <- read_html(url1,encoding = "utf-8")
    #获取第一页所有记录的网址信息
    link <- web %>% html_nodes('ul.layuiadmin-card-status a') %>% html_attr("href")  
    link <- paste0("http://fund.zsci.com.cn/",link)
    link
    

    然后,获取检索结果最大页码,

    #进入总网页第一页
    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")
    #读取网页内容
    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)])
    #获取总页数
    lastpage_web <- read_html(lastpage_link,encoding = "utf-8")
    lastpage_number <-  lastpage_web %>% html_nodes("div.layui-box span.current") %>% html_text() %>% as.integer()
    

    最后,正式开始爬取~~~ 具体思路过程:先爬取首页信息的详情页链接———利用循环获取首页20个详情页面内的信息——然后开始第二页(如此循环往复)——直到最后一页的最后一条信息。

    
    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/'
    #创建一个空的数据框用来存储抓取的数据
    results <- data.frame(ID="基金号",Title="题目",Author="负责人",Position="职称",Department="申请单位",
                          Type="研究类型",Project="项目批准号",Date="批准年度",Money="金额",
                          dateline="研究期限",Keywords_chinese="中文关键词",Keywords_english="英文关键词")
    
    for(i in 1:lastpage_number){
      url <-paste0(site,i,".html")
      web <- read_html(url)
      #获取第一页所有记录的网址信息
      link <- web %>% html_nodes('ul.layuiadmin-card-status a') %>% html_attr("href")  #获取了负责人和单位信息
      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")
        #---获得基金标题---
        Title <- link_web %>% html_nodes('div h2') %>% html_text() # 标题内容解析
        Title
        #---获得众多信息---
        Information <-  link_web %>% html_nodes('div.layui-table-body div') %>% html_text()
        Information
        #---获取基金ID号---
        ID <- Information[seq(3,length(Information),11)]
        ID
        #---获取负责人信息---
        Author <-  Information[seq(4,length(Information),11)]
        Author
        #---负责人职称---
        Position <- Information[seq(5,length(Information),11)]
        Position 
        #---获得申请单位---
        Department <-  Information[seq(6,length(Information),11)]
        Department
        #---获取研究类型---
        jijintype <-  Information[seq(8,length(Information),11)]
        jijintype
        #---获得项目号---
        Project <-  Information[seq(1,length(Information),11)]
        Project
        #---获取批准时间---
        Date <-  Information[seq(2,length(Information),11)]
        Date
        #---获取基金金额---
        Money <- Information[seq(7,length(Information),11)]
        Money 
        #---获取研究期限---
        dateline <- Information[seq(9,length(Information),11)]
        dateline
        #---获取关键词---
        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)
        #合并某一页面数据成数据框
        results <- rbind(results,result)
      }
    }
    

    最终爬取结果如下,由于信息较多,爬取速度会慢一些。

    image

    往期回顾
    R爬虫在工作中的一点妙用
    R爬虫必备基础——HTML和CSS初识
    R爬虫必备基础——静态网页+动态网页
    R爬虫必备——rvest包的使用
    R爬虫必备基础——CSS+SelectorGadget
    R爬虫必备基础—Chrome开发者工具(F12)
    R爬虫必备基础—HTTP协议
    R爬虫必备—httr+POST请求类爬虫(网易云课堂)
    R爬虫必备基础—rvest为什么不用于动态网页?
    R爬虫必备——httr+GET请求类爬虫(解螺旋课程)
    R爬虫实战—抓取PubMed文章的基本信息
    R爬虫实践—抓取国自然基金信息【上篇】
    R爬虫实践—抓取国自然基金信息【中篇】

    相关文章

      网友评论

          本文标题:R爬虫实践—抓取国自然基金信息【下篇】

          本文链接:https://www.haomeiwen.com/subject/sbevrktx.html