Web从COSMIC数据库中获取突变表

塞纳尔·帕特尔(Snehal Patel)

我很难用rvest从COSMIC数据库中刮取MYC基因突变表。我只得到一个空清单。输出如下。我已经确认这些值在HTML文件中(即,不是JAVA,因为这些值在HTML文件本身中)。我还确认了我要抓取的元素是一张桌子。我尝试使用xpath和CSS。我还确认允许刮刮。请指教

R控制台输出

> library("rvest")
> library("dplyr")
> library("robotstxt")
> library("XML")
> library("RSelenium")
> library("splashr")
> library("reticulate")
> url = "https://cancer.sanger.ac.uk/cosmic/gene/analysis?ln=MYC#variants"
> paths_allowed(url)
 cancer.sanger.ac.uk                      No encoding supplied: defaulting to UTF-8.


[1] TRUE
> Xpath = "//*[@id= 'DataTables_Table_0']"
> a = read_html(url) %>% html_nodes(xpath = Xpath) %>% html_table()
> a
list()
> Selector = "#DataTables_Table_0"
> a = read_html(url) %>% html_nodes(css = Selector) %>% html_table()
> a
list()
QHarr

您可以执行网页请求以动态检索结果的相同请求(可在开发工具的网络选项卡中查看F12。)将DisplayLength参数更改为所有结果(1394)或将其设置为初始大数并检查返回值以捕获实际结果总结果计数并发出获取所有结果所需的任何其他请求。

虽然您可以执行简单的rvest请求

library(rvest)

url <- 'https://cancer.sanger.ac.uk/cosmic/gene/mutations?all_data=&coords=AA%3AAA&dr=&end=455&gd=&id=359910&ln=MYC&seqlen=455&src=gene&start=1&export=json&sEcho=2&iColumns=6&sColumns=&iDisplayStart=0&iDisplayLength=1394&mDataProp_0=0&sSearch_0=&bRegex_0=false&bSearchable_0=true&bSortable_0=true&mDataProp_1=1&sSearch_1=&bRegex_1=false&bSearchable_1=true&bSortable_1=true&mDataProp_2=2&sSearch_2=&bRegex_2=false&bSearchable_2=true&bSortable_2=true&mDataProp_3=3&sSearch_3=&bRegex_3=false&bSearchable_3=true&bSortable_3=true&mDataProp_4=4&sSearch_4=&bRegex_4=false&bSearchable_4=true&bSortable_4=true&mDataProp_5=5&sSearch_5=&bRegex_5=false&bSearchable_5=true&bSortable_5=true&sSearch=&bRegex=false&iSortCol_0=0&sSortDir_0=asc&iSortingCols=1'

r <- read_html(url) %>% html_node('p') %>% html_text()

write.table(r,file="data.txt", sep='\t', row.names = FALSE)

@Snehal Patel编辑到上面以获得所需的格式:

x = read.table("data.txt", sep = "\t", skip = 2, fill = TRUE) 
colnames(x) = c("AA_Position", "CDS_Mutation", "AA_Mutation", "COSMIC_ID", "count", "Mutation_type")

使用httr,传递各种标头,并根据响应构造一个数据帧。

library(httr)
library(purrr)
library(rvest)

headers = c(
  'X-Requested-With' = 'XMLHttpRequest',
  'User-Agent' = 'Mozilla/5.0',
  'Referer' = 'https://cancer.sanger.ac.uk/cosmic/gene/analysis?ln=MYC'
)

params = list(
  'coords' = 'AA:AA',
  'end' = '455',
  'id' = '359910',
  'ln' = 'MYC',
  'seqlen' = '455',
  'src' = 'gene',
  'start' = '1',
  'export' = 'json',
  'sEcho' = '4',
  'iColumns' = '6',
  'iDisplayStart' = '0',
  'iDisplayLength' = '1394', #for all results. You can set to number higher than you expect then check first result for actual
  'mDataProp_0' = '0',
  'bRegex_0' = 'false',
  'bSearchable_0' = 'true',
  'bSortable_0' = 'true',
  'mDataProp_1' = '1',
  'bRegex_1' = 'false',
  'bSearchable_1' = 'true',
  'bSortable_1' = 'true',
  'mDataProp_2' = '2',
  'bRegex_2' = 'false',
  'bSearchable_2' = 'true',
  'bSortable_2' = 'true',
  'mDataProp_3' = '3',
  'bRegex_3' = 'false',
  'bSearchable_3' = 'true',
  'bSortable_3' = 'true',
  'mDataProp_4' = '4',
  'bRegex_4' = 'false',
  'bSearchable_4' = 'true',
  'bSortable_4' = 'true',
  'mDataProp_5' = '5',
  'bRegex_5' = 'false',
  'bSearchable_5' = 'true',
  'bSortable_5' = 'true',
  'bRegex' = 'false',
  'iSortCol_0' = '0',
  'sSortDir_0' = 'asc',
  'iSortingCols' = '1'
)

r <- content(httr::GET(url = 'https://cancer.sanger.ac.uk/cosmic/gene/mutations', httr::add_headers(.headers=headers), query = params)) %>% 
      .$aaData

df <- map_df(r, function(i) {

  data.frame(
   `Position` = read_html(i[[1]]) %>% html_node('a') %>% html_text() %>% as.numeric() ,
   `CDS Mutation` = read_html(i[[2]]) %>% html_node('a') %>% html_text(),
    `AA Mutation` = read_html(i[[3]]) %>% html_node('a') %>% html_text(),
   `Legacy Mutation ID` = i[[4]],
      `Count` = i[[5]] ,
      `Type` = i[[6]] , 
      stringsAsFactors=FALSE)
}) 

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从数据库中的表获取数据

来自分类Dev

从zend框架中的数据库表中获取数据

来自分类Dev

从数据库中获取复杂的表数据

来自分类Dev

如何使用foreach从数据库表中获取数据-

来自分类Dev

从3个数据库表中获取数据

来自分类Dev

如何从数据库获取数据到HTML表中?

来自分类Dev

从我的数据库表中获取数据

来自分类Dev

从数据库中获取复杂的表数据

来自分类Dev

从数据库中获取数据并显示到表

来自分类Dev

从数据库的列中的表中获取值

来自分类Dev

获取数据库表以在powershell中的dataGridView中显示

来自分类Dev

获取Postgres数据库中每个表的行数

来自分类Dev

从Firebird数据库表中获取列名列表

来自分类Dev

在C#中获取数据库表名称

来自分类Dev

从数据库表中获取记录,按月进行

来自分类Dev

如何从数据库的多个表中获取公共列?

来自分类Dev

从mysql数据库多个表中获取最新记录

来自分类Dev

如何仅获取PostgreSQL中数据库的表名

来自分类Dev

从多个数据库的某个表中获取行数

来自分类Dev

在C#中获取数据库表名称

来自分类Dev

获取数据库表中的记录总数

来自分类Dev

如何从数据库表中获取哈希

来自分类Dev

从数据库表中获取记录,按月进行

来自分类Dev

尝试在Smarty中获取表和数据库的数组

来自分类Dev

如何从数据库的多个表中获取公共列?

来自分类Dev

从mysql数据库多个表中获取最新记录

来自分类Dev

从数据库中按位置(行)获取表名称

来自分类Dev

如何从特定数据库中获取表的列名?

来自分类Dev

从数据库表中获取选定的值?