• 主页
  • 课程

    关于课程

    • 课程归档
    • 成为一名讲师
    • 讲师信息
    教学以及管理操作教程

    教学以及管理操作教程

    ¥1,000.00 ¥100.00
    阅读更多
  • 特色
    • 展示
    • 关于我们
    • 问答
  • 事件
  • 个性化
  • 博客
  • 联系
  • 站点资源
    有任何问题吗?
    (00) 123 456 789
    weinfoadmin@weinformatics.cn
    注册登录
    恒诺新知
    • 主页
    • 课程

      关于课程

      • 课程归档
      • 成为一名讲师
      • 讲师信息
      教学以及管理操作教程

      教学以及管理操作教程

      ¥1,000.00 ¥100.00
      阅读更多
    • 特色
      • 展示
      • 关于我们
      • 问答
    • 事件
    • 个性化
    • 博客
    • 联系
    • 站点资源

      未分类

      • 首页
      • 博客
      • 未分类
      • R 爬虫之爬取 CRAN 官网 R 包信息

      R 爬虫之爬取 CRAN 官网 R 包信息

      • 发布者 weinfoadmin
      • 分类 未分类, 老俊俊的生信笔记
      • 日期 2021年9月10日
      • 评论 0评论

      感谢老俊俊的大力支持。我们会每日跟新,欢迎您关注老俊俊的生信笔记。


      点击上方关注我们





      有一些公众号会时不时分享介绍一些按月份出炉的新 R 包,虽然有各个领域不同功能的,拓展我们的眼界也是挺好的。我也会时不时去 CRAN 官网里去看看最新的 R 包,看看有没有什么好玩的。

      今天去爬取一下官网的所有 R 包的信息。

      首先我们去官网找到 package,按照发表时间排序,先百度搜索:

      网址:https://cran.r-project.org/web/packages/index.html:

      然后按发表时间排序进入页面,总共收集了 17957 个 R 包:

      页面展示的是一个类似于表格的形式:

      我们右键检查元素,定位到整个表格:

      html_table 函数



      介绍一下我们的 html_table 函数,可以提取网页里的表格内容,然后输出也是一个表格形式!

      我们看看 html 结构的 table 元素组成部分:

      html <- minimal_html("
        <table>
          <tr>
            <th>x</th>
            <th>y</th>
          </tr>
          <tr>
            <td>1.5</td>
            <td>2.7</td>
          </tr>
          <tr>
            <td>4.9</td>
            <td>1.3</td>
          </tr>
          <tr>
            <td>7.2</td>
            <td>8.1</td>
          </tr>
        </table>
        "
      )

      html 格式的 table 主要由 4 个部分组成:<table>、<tr> (table 行),<th> (table 标题行),和 <td> (table 数据)。

      用上面测试提取:

      html %>%
        html_node("table") %>%
        html_table()
      #> # A tibble: 3 x 2
      #>       x     y
      #>   <dbl> <dbl>
      #> 1   1.5   2.7
      #> 2   4.9   1.3
      #> 3   7.2   8.1

      实战



      然后我们可以对官网的表格进行提取了,网页已经显示了 所有的 R 包 ,并不需要翻页:

      提取:

      # 地址
      url <- 'https://cran.r-project.org/web/packages/available_packages_by_date.html'

      # 提取表格
      r_package <- url %>% read_html() %>% html_table() %>% as.data.frame()

      # 查看结果
      nrow(r_package)
      [1] 17959
      # 查看内容
      head(r_package,3)
              Date   Package                                       Title
      1 2021-08-03   amanida         Meta-Analysis for Non-Integral Data
      2 2021-08-03 chameleon Automatic Colors for Multi-Dimensional Data
      3 2021-08-03    cleanr                   Helps You to Code Cleaner

      还可以提取每个 R 包的链接地址,需要 替换一下前缀 :

      # 查看内容
      head(r_package,3)

      r_package_link <- url %>%
        read_html() %>%
        html_nodes('a') %>%
        html_attr('href')
      # 查看数量
      length(r_package_link)
      [1] 17959

      # 查看网址内容
      head(r_package_link,3)
      [1] "../../web/packages/amanida/index.html"   "../../web/packages/chameleon/index.html"
      [3] "../../web/packages/cleanr/index.html"

      # 替换
      package_link <- gsub('../../','https://cran.r-project.org/',r_package_link)

      # 查看替换后的内容
      head(package_link,3)
      [1] "https://cran.r-project.org/web/packages/amanida/index.html"
      [2] "https://cran.r-project.org/web/packages/chameleon/index.html"
      [3] "https://cran.r-project.org/web/packages/cleanr/index.html"

      点进每个 R 包里,都会有一段对R 包的简要描述,我们也可以爬取一下:

      先拿第一个包试一下:

      # 循环每个R包网页
      summa <- c()

      for (i in 1:length(package_link[1])) {
        descrb <- package_link[i] %>%
          read_html() %>%
          html_nodes('p') %>%
          html_text()
        # 储存
        summa <- c(summa,descrb)
      }

      # 查看数量
      length(summa)
      [1] 2

      # 查看内容
      head(summa,3)
      [1] "Combination of results for meta-analysis using significance and effect size only. P-values and fold-change are combined to obtain a global significance on each metabolite. Produces a volcano plot summarising the relevant results from meta-analysis. Vote-counting reports for metabolites. And explore plot to detect discrepancies between studies at a first glance. "
      [2] "Please use the canonical formnhttps://CRAN.R-project.org/package=amanidanto link to this page."

      发现返回了两段文字,检查以后发现把最下面的也提取出来了:

      我们只需要取第一段文字就行了,再来一次,所有内容批量:

      # 循环每个R包网页
      summa <- c()

      for (i in 1:length(package_link[1:200])) {
        descrb <- package_link[i] %>%
          read_html() %>%
          html_nodes('p') %>%
          html_text()
        descrb <- descrb[1]
        # 储存
        summa <- c(summa,descrb)
      }

      Error in open.connection(x, "rb") : HTTP error 404.

      哈哈,什么鬼,网页 404 了,我换成 5 个网页试试是可以的:

      # 循环每个R包网页
      summa <- c()

      for (i in 1:length(package_link[1:5])) {
        descrb <- package_link[i] %>%
          read_html() %>%
          html_nodes('p') %>%
          html_text()
        descrb <- descrb[1]
        # 储存
        summa <- c(summa,descrb)
      }

      # 查看数量
      length(summa)
      [1] 5

      # 查看内容
      head(summa,3)
      [1] "Combination of results for meta-analysis using significance and effect size only. P-values and fold-change are combined to obtain a global significance on each metabolite. Produces a volcano plot summarising the relevant results from meta-analysis. Vote-counting reports for metabolites. And explore plot to detect discrepancies between studies at a first glance. "
      [2] "Assign distinct colors to arbitrary multi-dimensional data, considering its structure."
      [3] "Check your R code for some of the most commonn    layout flaws.  Many tried to teach us how to write code less dreadful,n    be it implicitly as B. W. Kernighan and D. M. Ritchie (1988)n    <ISBN:0-13-110362-8> in 'The C Programming Language' did, be itn    explicitly as R.C. Martin (2008) <ISBN:0-13-235088-2> in 'Clean Code:n    A Handbook of Agile Software Craftsmanship' did.  So we should checkn    our code for files too long or wide, functions with too many lines,n    too wide lines, too many arguments or too many levels of nesting.n    Note: This is not a static code analyzer like pylint or the like.n    Checkout <https://cran.r-project.org/package=lintr> instead."

      很可能服务器把 rvest 这种请求当成爬虫了,直接拒绝访问了哈!

      最后我们把网址拼一下:

      # 新增link列
      r_package$link <- package_link
      # 查看
      head(r_package)
              Date      Package                                                        Title
      1 2021-08-03      amanida                          Meta-Analysis for Non-Integral Data
      2 2021-08-03    chameleon                  Automatic Colors for Multi-Dimensional Data
      3 2021-08-03       cleanr                                    Helps You to Code Cleaner
      4 2021-08-03    contentid                   An Interface for Content-Based Identifiers
      5 2021-08-03 crctStepdown Univariate Analysis of Cluster Trials with Multiple Outcomes
      6 2021-08-03           do                                                Data Operator
                                                                              link
      1                 https://cran.r-project.org/web/packages/amanida/index.html
      2               https://cran.r-project.org/web/packages/chameleon/index.html
      3                  https://cran.r-project.org/web/packages/cleanr/index.html
      4               https://cran.r-project.org/web/packages/contentid/index.html
      5            https://cran.r-project.org/web/packages/crctStepdown/index.html
      6 https://cran.r-project.org/web/packaghttps://cran.r-project.org/index.html


      所以今天你学习了吗?

      发现更多精彩

      关注公众号

      欢迎小伙伴留言评论!

      点击我留言!

      今天的分享就到这里了,敬请期待下一篇!

      最后欢迎大家分享转发,您的点赞是对我的鼓励和肯定!

      如果觉得对您帮助很大,赏杯快乐水喝喝吧!

      推 荐 阅 读




      • circlize 之可视化基因组数据

      • circlize 之 Advanced layout

      • circlize 之 circos.heatmap()

      • circlize 之 Implement high-level circular plots

      • 怎么批量合并 data.frame ?

      • QPCRpro 正式上线!

      • circlize 之 Legends

      • QPCR数据添加 p 值和显著性一次解决!

      • circlize 之 Graphics

      • circlize 之 Introduction

      • circlize 之 Circular layout

      • 鉴定差异翻译效率基因之 deltaTE 下篇

      • 鉴定差异翻译效率基因之 deltaTE 上篇

      • 鉴定差异翻译效率基因之 Riborex

      • purrr 包之 list 处理系列函数

      • purrr 包之 map 系列函数

      • 批量绘制单基因相关性图

      • Y 叔出品:ggfun

      • 神器之 computeMatrix + 绘图

      • Deeptools 神器之 bamCoverage

      • 在线版shiny pheatmap!

      • QPCR数据快速分析和绘图 — by shiny

      • RNA-seq:Salmon 快速定量

      • RNA-seq:Salmon 定量结果差异分析

      • 用R提取代表转录本

      • 画个CNS级别火山图!

      • R Tips :split 函数

      • 什么? R 版 Hisat2

      • R Tips :match 函数

      • conda 安装软件报错

      • MetaProfile on Transcript

      • 提取代表转录本之 gencode

      • 附近含有 m6A 修饰的 Stop Codon 序列提取

      • Introduction of m6A

      • RNA-seq : Hisat2+Stringtie+DESeq2

      • shiny VennDiagram

      • shiny CountToTPM/FPKM

      • 自己模仿画个– m6A distribution on transcript

      • 怎么把 shiny App 做成 exe 文件进行分发

      • shiny-server内网穿透

      • 在线版shiny pheatmap!

      • 用shiny创作在线火山图绘制App

      • circlize 之 Create plotting regions

      • circlize 之 High-level genomic functions

      • R 爬虫之爬取文献信息

      • R 爬虫之爬取公众号图片

      • 跟着 cell 绘制条形堆叠图和分面小提琴图

      • R 爬虫之爬取 NCBI 文献

      • ggplot 分面绘图一网打尽

      • circlize 之 chordDiagram 函数

      • circlize 之 chordDiagram 函数高级用法

      • R 绘制柱形偏差图

      • R 爬虫之爬取文献影响因子

      • R爬虫之 html 简介

      请关注“恒诺新知”微信公众号,感谢“R语言“,”数据那些事儿“,”老俊俊的生信笔记“,”冷🈚️思“,“珞珈R”,“生信星球”的支持!

      • 分享:
      作者头像
      weinfoadmin

      上一篇文章

      ZhouLab 星球
      2021年9月10日

      下一篇文章

      R爬虫之 html 简介
      2021年9月10日

      你可能也喜欢

      2-1675088548
      lncRNA和miRNA生信分析系列讲座免费视频课和课件资源包,干货满满
      30 1月, 2023
      9-1675131201
      如何快速批量修改 Git 提交记录中的用户信息
      26 1月, 2023
      5-1660909989
      scanpy官方教程2022|03-scanpy包核心绘图功能
      19 8月, 2022

      留言 取消回复

      要发表评论,您必须先登录。

      搜索

      分类

      • R语言
      • TCGA数据挖掘
      • 单细胞RNA-seq测序
      • 在线会议直播预告与回放
      • 数据分析那些事儿分类
      • 未分类
      • 生信星球
      • 老俊俊的生信笔记

      投稿培训

      免费

      alphafold2培训

      免费

      群晖配置培训

      免费

      最新博文

      Nature | 单细胞技术揭示衰老细胞与肌肉再生
      301月2023
      lncRNA和miRNA生信分析系列讲座免费视频课和课件资源包,干货满满
      301月2023
      如何快速批量修改 Git 提交记录中的用户信息
      261月2023
      logo-eduma-the-best-lms-wordpress-theme

      (00) 123 456 789

      weinfoadmin@weinformatics.cn

      恒诺新知

      • 关于我们
      • 博客
      • 联系
      • 成为一名讲师

      链接

      • 课程
      • 事件
      • 展示
      • 问答

      支持

      • 文档
      • 论坛
      • 语言包
      • 发行状态

      推荐

      • iHub汉语代码托管
      • iLAB耗材管理
      • WooCommerce
      • 丁香园论坛

      weinformatics 即 恒诺新知。ICP备案号:粤ICP备19129767号

      • 关于我们
      • 博客
      • 联系
      • 成为一名讲师

      要成为一名讲师吗?

      加入数以千计的演讲者获得100%课时费!

      现在开始

      用你的站点账户登录

      忘记密码?

      还不是会员? 现在注册

      注册新帐户

      已经拥有注册账户? 现在登录

      close
      会员购买 你还没有登录,请先登录
      • ¥99 VIP-1个月
      • ¥199 VIP-半年
      • ¥299 VIP-1年
      在线支付 激活码

      立即支付
      支付宝
      微信支付
      请使用 支付宝 或 微信 扫码支付
      登录
      注册|忘记密码?