• 主页
  • 课程

    关于课程

    • 课程归档
    • 成为一名讲师
    • 讲师信息
    同等学历教学

    同等学历教学

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

      关于课程

      • 课程归档
      • 成为一名讲师
      • 讲师信息
      同等学历教学

      同等学历教学

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

      未分类

      • 首页
      • 博客
      • 未分类
      • R 爬虫之爬取文献信息

      R 爬虫之爬取文献信息

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

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




      开始


      今天学习了一些关于 R 爬虫的知识,后续会陆续写一些笔记,当然对于爬虫有更好的一些工具来进行爬取数据,作为入门小白,我自己先从 R 语言尝试开始吧。

      我们在浏览器中看到的网页很多都是有 HTML(Hyper Text Markup Language)超文本标记语言构成的树形结构,包括一系列标签,HTML 是一类标记语言而不是编程语言,当然要爬虫的话最好去了解一些最基本的 HTML 和 XML(Extensible Markup Language) 语言知识比较好。html 和 xml 有着类似的树形结构,都是一种标记语言。

      今天学习了一下怎么爬取 NCBI 上的文献和基本信息,分享给大家。


      试水


      我们主要是使用 rvest 这个 R 包来爬取,这个主要应用于静态网页的数据爬取会实用一些,安装:

      install.packages('rvest')

      我们的目的是搜索感兴趣的关键词,然后对搜索的结果进行爬取。假如我想搜索 2021 年 m6a 相关的所有文章,获取文章的标题,作者,网页链接和摘要内容。

      首先我们打开 NCBI 的 pubmed 网页,筛选 2021 年的 m6a 文章:

      可以看到总共有 811 篇文章,我这设置了每页显示 200 篇文献,大家可以自行设置,我们需要获取的该页面的文章标题和作者信息,首先我们爬取的网址就是当前页面的网址,因为显示的限制,所以需要对每个页面的数据进行爬取:

      # 加载R包
      library(xml2)
      library(rvest)
      library(tidyverse)

      # 爬取2021年m6a文献,总共有811篇,显示200篇文献,爬取5次即可
      url <- c('https://pubmed.ncbi.nlm.nih.gov/?term=m6a&filter=years.2021-2021&size=200',
               'https://pubmed.ncbi.nlm.nih.gov/?term=m6a&filter=years.2021-2021&size=200&page=2',
               'https://pubmed.ncbi.nlm.nih.gov/?term=m6a&filter=years.2021-2021&size=200&page=3',
               'https://pubmed.ncbi.nlm.nih.gov/?term=m6a&filter=years.2021-2021&size=200&page=4',
               'https://pubmed.ncbi.nlm.nih.gov/?term=m6a&filter=years.2021-2021&size=200&page=5')

      # 或者这样也行
      root <- 'https://pubmed.ncbi.nlm.nih.gov/?term=m6a&filter=years.2021-2021&size=200&page='
      url <- paste(root,1:5,sep = '')

      接下来使用 read_html 函数解析网址的内容,html 主要有 head 和 body 两个成分组成:

      # 示例,read_html 返回html格式的内容
      read_html(url[1],encoding = 'utf-8')
      ## {html_document}
      ## <html lang="en">
      ## [1] <head itemscope itemtype="http://schema.org/WebPage" prefix="og: http://ogp.me/ns#">n<meta http-equiv ...
      ## [2] <body>nn  n  n    <noscript>n  <div class="no-script-banner" id="no-script-banner">n    <div cla ...

      获取网址的 html 信息后就需要提取指定节点元素的内容了,需要使用 html_nodes/html_node 来获取节点信息,该函数只需要输入节点名称或者节点的路径(绝对路径或者相对路径)或者节点选择器。

      我们可以在网页上右键点击检查就可看到网页 html 格式的树形结构信息,再点击左上角箭头即可选中在网页中特定内容,右边就会自动定位到该内容的节点位置处:

      选中页面特定内容:

      接下来我们需要获取该节点的节点名称或者节点路径来提取该节点信息,首先点击我们选中的内容,然后在 3 位置上鼠标右键点击复制选项:

      可以看到复制 selector、复制 XPath 和复制完整的 XPath 三个选项,分别是节点选择器,节点相对路径,节点绝对路径,我们把复制的内容传到 html_nodes/html_node 函数里就可以得到节点的内容了。

      这里我们尝试第一篇文章的标题节点信息获取:

      # 节点选择器
      read_html(url[1],encoding = 'utf-8') %>%
        html_nodes('#search-results > section.search-results-list > div.search-results-chunks > div > article:nth-child(2) > div.docsum-wrap > div.docsum-content > a')
      ## {xml_nodeset (1)}
      ## [1] <a class="docsum-title" href="/32749190/" ref="linksrc=docsum_link&amp;article_id=32749190&amp;ordinalpos=1&amp;page=1" data-ga-category="result_ ...

      可以看到返回的是完整的该位置处的节点信息,可以使用 html_text 函数从里面提取文本信息,去除 html 格式的标签等不必要信息:

      read_html(url[1],encoding = 'utf-8') %>%
        html_nodes('#search-results > section.search-results-list > div.search-results-chunks > div > article:nth-child(2) > div.docsum-wrap > div.docsum-content > a') %>%
        html_text()
      ## [1] "n                Comprehensive analysis of the transcriptome-wide m6A methylome in colorectal cancer by MeRIP sequencing.n              "
      # 加上trim = T 参数简洁化内容
      read_html(url[1],encoding = 'utf-8') %>%
        html_nodes('#search-results > section.search-results-list > div.search-results-chunks > div > article:nth-child(2) > div.docsum-wrap > div.docsum-content > a') %>%
        html_text(trim = T)
      ## [1] "Comprehensive analysis of the transcriptome-wide m6A methylome in colorectal cancer by MeRIP sequencing."

      同样的我们使用节点的相对路径和绝对路径也能得到相同的结果,此时需要用 xpath 参数指明:

      # 相对路径
      read_html(url[1],encoding = 'utf-8') %>%
        html_nodes(xpath = '//*[@id="search-results"]/section[1]/div[1]/div/article[1]/div[2]/div[1]/a') %>%
        html_text(trim = T)
      ## [1] "Comprehensive analysis of the transcriptome-wide m6A methylome in colorectal cancer by MeRIP sequencing."

      # 绝对路径
      read_html(url[1],encoding = 'utf-8') %>%
        html_nodes(xpath = '/html/body/main/div[9]/div[2]/section[1]/div[1]/div/article[1]/div[2]/div[1]/a') %>%
        html_text(trim = T)
      ## [1] "Comprehensive analysis of the transcriptome-wide m6A methylome in colorectal cancer by MeRIP sequencing."

      此外我们可以使用 SelectorGadget 网页插件来获取节点名称,插件安装直接去官网:https://selectorgadget.com/,拉到最下面,把 SelectorGadget 拉到收藏夹就可以使用了:

      来到我们爬取的网页,点击 SelectorGadget,选择特定要获取的网页内容,复制节点名称就可以了,这个也可以复制 xpath 相对路径:

      尝试一下:

      # 节点名称
      read_html(url[1],encoding = 'utf-8') %>%
        html_nodes('.docsum-title') %>%
        html_text(trim = T)
      ## [1] "Comprehensive analysis of the transcriptome-wide m6A methylome in colorectal cancer by MeRIP sequencing."
      ## [2] "RNA m6A methylation promotes the formation of vasculogenic mimicry in hepatocellular carcinoma via Hippo pathway."
      ...
      ## [199] "Main N6-Methyladenosine Readers: YTH Family Proteins in Cancers."
      ## [200] "FTO overexpression inhibits apoptosis of hypoxia/reoxygenation-treated myocardial cells by regulating m6A modification of Mhrt."

      # 批量提取文章标题
      title <- c()
      for (i in url) {
        title <- c(title,read_html(i,encoding = 'utf-8') %>% html_nodes(".docsum-title") %>% html_text(trim = T))
      }
      # 查看数量
      length(title)
      ## [1] 813

      可以看到 .docsum-title 节点名称把该当前网页的所有文章标题都提取了出来,非常的方便。





      接下来提取每篇文章的作者信息,同样的操作:

      # 2、爬取文章作者
      author <- c()
      for (i in url) {
        author <- c(author,read_html(i,encoding = 'utf-8') %>%
                      html_nodes('.full-authors') %>%
                      html_text())
      }
      # 查看数量
      length(author)
      ## [1] 813




      爬取文章地址,其实每篇文章的标题都是一个链接,我们点击标题就可以进入另一个网址,所以只需要获取该标题的超链接地址就可以了,也就是这篇文章的地址,这时我们使用 html_attr 函数来提取标题节点的属性。

      在 html 元素中可以看到 href 标识,就是链接地址的 id,我们进入该文章后,这篇文章的地址只是在上级网页地址后加了这个 id:

      网址地址:

      我们用 html_attrs 获取所有属性:

      read_html(url[1],encoding = 'utf-8') %>%
        html_nodes('.docsum-title') %>%
        html_attrs() %>% head()
      [[1]]
                                                                                 class
                                                                        "docsum-title"
                                                                                  href
                                                                          "/32749190/"
                                                                                   ref
                         "linksrc=docsum_link&article_id=32749190&ordinalpos=1&page=1"
                                                                      data-ga-category
                                                                        "result_click"
                                                                        data-ga-action
                                                                                   "1"
                                                                         data-ga-label
                                                                            "32749190"
                                                                 data-full-article-url
      "from_term=m6a&from_filter=years.2021-2021&from_size=200&from_page=1&from_pos=1"
                                                                       data-article-id
                                                                            "32749190"
      ...

      可以使用 html_attr 指定 name 参数来获取指定属性的内容:

      read_html(url[1],encoding = 'utf-8') %>%
        html_nodes('.docsum-title') %>%
        html_attr(name = 'href') %>% head()
      ## [1] "/32749190/" "/32920668/" "/32821938/" "/33314339/" "/33070036/" "/33156926/"

      ## 批量获取文章链接
      # 3、爬取文章地址,地址是https://pubmed.ncbi.nlm.nih.gov/加上爬取的编号
      web <- c()
      for (i in url) {
        web <- c(web,read_html(i,encoding = 'utf-8') %>% html_nodes('.docsum-title') %>% html_attr(name = 'href'))
      }
      # 查看数量
      length(web)
      ## [1] 813
      # 连接成网址
      web_link <- paste('https://pubmed.ncbi.nlm.nih.gov',web,sep = '')
      web_link
      ## [1] "https://pubmed.ncbi.nlm.nih.gov/32749190/" "https://pubmed.ncbi.nlm.nih.gov/32920668/" "https://pubmed.ncbi.nlm.nih.gov/32821938/"
      ## [4] "https://pubmed.ncbi.nlm.nih.gov/33314339/" "https://pubmed.ncbi.nlm.nih.gov/33070036/" "https://pubmed.ncbi.nlm.nih.gov/33156926/"




      要获取文章的摘要就得进入该网址,在上一步我们已经获取了每篇文章的网址,就可以再次对其解析,然后获取摘要的内容了,基本上是同样的操作:

      循环每篇文章,稍微花点时间,R 语言爬虫确实慢,哈哈:

      # 4、爬取文章摘要
      abstract <- list()
      for (i in web_link) {
        # 根据节点相对路径提取
        # abstract[[i]] <- read_html(i,encoding = 'utf-8') %>% html_nodes(xpath = '//*[@id="enc-abstract"]/p') %>% html_text(trim = T)
        # 根据节点绝对路径提取
        # abstract[[i]] <- read_html(i,encoding = 'utf-8') %>% html_nodes(xpath = '/html/body/div[5]/main/div[2]/div[2]/p') %>% html_text(trim = T)
        # 根据节点名称提取
        abstract[[i]] <- read_html(i,encoding = 'utf-8') %>% html_nodes("#enc-abstract > p") %>% html_text(trim = T)
      }
      # 查看数量
      length(abstract)
      ## [1] 813

      爬取的摘要会有 n 字符,此外有些文章的摘要并不是一个完整的部分,分类了好几点,这样的情况也需要处理一下:

      # 去除n
      abstract_clean <- lapply(abstract, gsub,pattern = 'n',replacement = '')

      # 连接为一个字符串对于多个部分的摘要
      abs_res <- c()
      for(i in 1:807){
        # 判断元素长度
        len = length(abstract_clean[[i]])
        if(len == 1){
          # 如果只有一个摘要就保存
          abs_res <- c(abs_res,abstract_clean[[i]])
        }else{
          # 如果摘要格式有多个,连接成一个
          abs_res <- c(abs_res,paste(abstract_clean[[i]],sep = '-',collapse = ' '))
        }
      }
      # 查看数量
      length(abs_res)
      ## [1] 813




      最后我们把所有爬取的内容整理保存为一个表格里并保存输出:

      # 5、保存爬取数据
      final_res <- data.frame(Title = title,Author = author,web = web_link,Abstract = abs_res)
      # 6、保存爬取的数据
      write.csv(final_res,file = 'C:/Users/admin/Desktop/m6a_Articles.csv',row.names = F)

      我们打开看看:

      可以可以,所以今天你学习了吗?



      发现更多精彩

      关注公众号

      欢迎小伙伴留言评论!

      点击我留言!

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

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

      如果觉得对您帮助很大,打赏一下吧!

      推 荐 阅 读




      • 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”,“生信星球”的支持!

      • 分享:
      作者头像
      weinfoadmin

      上一篇文章

      R 爬虫之爬取公众号图片
      2021年9月10日

      下一篇文章

      circlize 之 High-level genomic functions
      2021年9月10日

      你可能也喜欢

      2-1675088548
      lncRNA和miRNA生信分析系列讲座免费视频课和课件资源包,干货满满
      30 1月, 2023
      9-1675131201
      如何快速批量修改 Git 提交记录中的用户信息
      26 1月, 2023
      8-1678501786
      肿瘤细胞通过改变CD8+ T细胞中的丙酮酸利用和琥珀酸信号来调控抗肿瘤免疫应答。
      7 12月, 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年
      在线支付 激活码

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