一个函数抓取代谢组学权威数据库HMDB的所有表格数据
2018-01-26 18:27
756 查看
欢迎关注生信宝典微信公众号:http://mp.weixin.qq.com/s/rYjcsfHrbcAhaFpQI5Yc6g
爬虫是都不陌生的一个概念,比如百度、谷歌都有自己的爬虫工具去抓取网站、分析、索引,方便我们的查询使用。
在我们浏览网站、查询信息时,如果想做一些批量的处理,也可以去分析网站的结构、抓取网页、提取信息,然后就完成了一个小爬虫的写作。
网页爬虫需要我们了解
HMDB (人类代谢组数据库)收录了很多代谢组的数据,用于代谢组学、临床化学、生物标志物开啊和基本教育等。数据联通化学、临床、分子生物学3个层次,共有114,099个代谢物。
网站提供了多种浏览和查询功能,可以关注不同的疾病、通路、BMI、年龄、性别相关代谢组学。
下图展示的是BMI相关代谢物的数据。
如果我们想把这个表格下载下来,一个办法是一页页的拷贝,大约拷贝十几次,工作量不算太大,但有些无趣。另外一个办法就是这次要说的抓取网页。
R的
这样我们就获得了第一页的表格,如果想获得随后的页的呢?鼠标移动经过分页的标签,可以看到URL的规律。
这样就获得了所有的表格。
有两点需要注意
为了给被抓取的网站带去较大的访问压力,每抓取一次,最后间歇一段时间。这需要我们自定义一个函数,封装下
HMDB数据库提供了全数据下载功能,相比于抓取,下载下来数据,自己筛选合并是更好的方式。
VIM正则表达式获取生信宝典公众号的文章列表
R Error using readHTMLTable
Extract web pages
爬虫是都不陌生的一个概念,比如百度、谷歌都有自己的爬虫工具去抓取网站、分析、索引,方便我们的查询使用。
在我们浏览网站、查询信息时,如果想做一些批量的处理,也可以去分析网站的结构、抓取网页、提取信息,然后就完成了一个小爬虫的写作。
网页爬虫需要我们了解
URL的结构、
HTML语法特征和结构,以及使用合适的抓取、解析工具。我们这篇先看一个简单的处理,给一个直观的感受:一个函数抓取网页的表格。以后再慢慢解析如何更加定制的获取信息。
HMDB (人类代谢组数据库)收录了很多代谢组的数据,用于代谢组学、临床化学、生物标志物开啊和基本教育等。数据联通化学、临床、分子生物学3个层次,共有114,099个代谢物。
网站提供了多种浏览和查询功能,可以关注不同的疾病、通路、BMI、年龄、性别相关代谢组学。
下图展示的是BMI相关代谢物的数据。
如果我们想把这个表格下载下来,一个办法是一页页的拷贝,大约拷贝十几次,工作量不算太大,但有些无趣。另外一个办法就是这次要说的抓取网页。
R的
XML包中有个函数
readHTMLTable专用于识别HTML中的表格 (table标签),从而提取元素。具体使用如下:
# Load the package required to read website library(XML) # wegpage address url <- "http://www.hmdb.ca/bmi_metabolomics" # header=T, 使第一行或thead属性的内容为标题 df1 <- readHTMLTable(url, header=T, stringsAsFactors = F) # 初次使用,不了解输出格式时可使用str查看 str(df1)
> str(df1) List of 1 $ NULL:'data.frame': 25 obs. of 7 variables: ..$ V1: chr [1:25] "Butyrylcarnitine (HMDB0002013)" "Alpha-ketoisovaleric acid (HMDB0000019)" "2-Hydroxy-3-methylbutyric acid (HMDB0000407)" "3-Methyl-2-oxovaleric acid (HMDB0000491)" ... ..$ V2: chr [1:25] "" "" "" "" ... ..$ V3: chr [1:25] "Increase" "Increase" "Increase" "Increase" ... ..$ V4: chr [1:25] "Blood" "Blood" "Blood" "Blood" ... ..$ V5: chr [1:25] "9.95e-10" "2.87e-08" "1.19e-05" "1.68e-05" ... ..$ V6: chr [1:25] "25254000" "25254000" "25254000" "25254000" ... ..$ V7: chr [1:25] "details" "details" "details" "details" ...
# The readHTMLTable returns list, we need to extract our data frame. In this example, the first element is our data frame, so we can extract it like this: head(df1[[1]]) # extract the first element of list #df1[["NULL"]] # extract list element based on element names (第一个元素的名字是NULL)
1 Butyrylcarnitine (HMDB0002013) Increase Blood 9.95e-10 2 Alpha-ketoisovaleric acid (HMDB0000019) Increase Blood 2.87e-08 3 2-Hydroxy-3-methylbutyric acid (HMDB0000407) Increase Blood 1.19e-05 4 3-Methyl-2-oxovaleric acid (HMDB0000491) Increase Blood 1.68e-05 5 Ketoleucine (HMDB0000695) Increase Blood 6.05e-05 6 (S)-3-Hydroxyisobutyric acid (HMDB0000023) Increase Blood 6.88e-05 V6 V7 1 25254000 details 2 25254000 details 3 25254000 details 4 25254000 details 5 25254000 details 6 25254000 details
这样我们就获得了第一页的表格,如果想获得随后的页的呢?鼠标移动经过分页的标签,可以看到URL的规律。
http://www.hmdb.ca/bmi_metabolomics?page=num,每一页就是变换下num;对首页来说,可以写
page=1也可以省略,为了批量,一般写上。
# 294是在网页直接看到的总条数,25是每页显示的条数。(也是可以自动解析判断的) pages = 1:ceiling(294 / 25) url <- "http://www.hmdb.ca/bmi_metabolomics?page=" # 获得URL集合 url_all <- paste(url, pages, sep="") a = sapply(url, readHTMLTable, header=T, stringsAsFactors=F) # 合并获得的结果 b = do.call("rbind",a) # 重命名行 rownames(b) <- 1:nrow(b)
这样就获得了所有的表格。
有两点需要注意
为了给被抓取的网站带去较大的访问压力,每抓取一次,最后间歇一段时间。这需要我们自定义一个函数,封装下
readHTMLTable。
HMDB数据库提供了全数据下载功能,相比于抓取,下载下来数据,自己筛选合并是更好的方式。
问题解决
可能是因为网速或其它问题,有时直接把url提供给
readHTMLTable不一定可以获取结果,下面提供了2额外的方式,供使用。
# Load the package required to read website library(XML) # wegpage address url <- "http://www.hmdb.ca/bmi_metabolomics" # method one: for people who is luckiest (not me, so sad) df1 <- readHTMLTable(url, header=T, stringsAsFactors = F) # Error: failed to load external entity "url" # method two: use RCurl package, for people who is much luckier (only work on my laptop, not the computer in the office, crying) library(RCurl) xmldoc <- getURL(url) df2 <- readHTMLTable(xmldoc, stringsAsFactors = F) # method three: use httr package, for people who is not lucky library(httr) tabs <- GET(url) df3 <- readHTMLTable(rawToChar(tabs$content), as.data.frame = T, stringsAsFactors = F)
便捷链接
R语言学习 - 基础概念和矩阵操作VIM正则表达式获取生信宝典公众号的文章列表
R Error using readHTMLTable
Extract web pages
相关文章推荐
- 如何将一个表格的所有数据存到数据库的一个字段里,并如何将这一个数据字段里的数据填充到前端表格里
- 在Mysql中怎样返回一个数据库的所有表名,列名数据类型备注
- sqlserver 函数里并返回一个表格数据拼接的字符串
- 把数据库中所有的数据复制到一个文件中,值之间放置“,”,每一行数据都另起一行,并且把所有的值都放在“"”之间
- ASP.NET基础教程-Web 自定义控件的使用-根据属性值从数据库中提取数据并在页面上自动生成一个表格
- 将一个数据库中所有表的数据全部删除的命令
- php新闻发布系统发布成功从数据库查询所有数据用表格显示出来03
- 反转一个字符串和删除N层级目录的所有文件或者删除数据库所有表保留数据库;清除数据库所有表的数据
- 遍历查询一个数据库中所有表,列出表名和每个表中的数据行数
- 要将一个数据库的所有表格使用批通信程序取出(BCP OUT),有何较快的方法?
- onethink如何在新增时一条数据的时候运行一个函数或进行某数据库的操作
- 怎样将一个数据库里的所有数据表的名字绑定到ComboBox中
- 一个带游标的遍历数据的函数|数据库操作|软件开发
- 清空一个数据库中所有表的数据的sql语句
- 一个带游标的遍历数据的函数|数据库操作|软件开发
- 请问如何在Web页面中点击一个button之后,用jsp从数据库中读取数据显示到表格里
- 实现给定一个数据库连接得到数据库下所有的数据表
- 有一个数据表,但是我只知道一个字段的名称。现在我想查询数据库里面的除去这个字段的所有数据!
- 在Mysql中怎样返回一个数据库的所有表名,列名数据类型备注
- 输出一个数据库中所有表的数据量