这是一个很难回答的问题,但我正试着从下面的页面中抓取一些html。
http://www.ncbi.nlm.nih.gov/snp/?term=(human[Organism])+AND+GLRA3[Gene Name]在这里,我可以使用R刮取我需要的内容,但是因为浏览器只显示前20个条目,所以只有相应的html可用。这会导致一个问题,因为我想刮掉所有的条目,而不仅仅是浏览器上的页面所提供的条目。无论如何,这是我的R码
library(XML)
library(httr)
#Go to Nectar Mutation and get SNP refs
dbsnp.searchterm="(human[Organism])+AND+GLRA1[Gene Name]"
dbsnp.url=paste0("http://www.ncbi.nlm.nih.gov/snp/?term=",dbsnp.searchterm)
dbsnp.get=GET(dbsnp.url)
dbsnp.content=content(dbsnp.get, as="text")
links<-xpathSApply(htmlParse(dbsnp.content), "//a[contains(@href, 'snp_ref')]",xmlGetAttr,"href")和结果
> links
[1] "/projects/SNP/snp_ref.cgi?rs=116474260"
[2] "/projects/SNP/snp_ref.cgi?rs=121918408"
[3] "/projects/SNP/snp_ref.cgi?rs=121918409"
[4] "/projects/SNP/snp_ref.cgi?rs=121918410"
[5] "/projects/SNP/snp_ref.cgi?rs=121918411"
[6] "/projects/SNP/snp_ref.cgi?rs=121918412"
[7] "/projects/SNP/snp_ref.cgi?rs=121918413"
[8] "/projects/SNP/snp_ref.cgi?rs=121918414"
[9] "/projects/SNP/snp_ref.cgi?rs=121918415"
[10] "/projects/SNP/snp_ref.cgi?rs=121918416"
[11] "/projects/SNP/snp_ref.cgi?rs=121918417"
[12] "/projects/SNP/snp_ref.cgi?rs=121918418"
[13] "/projects/SNP/snp_ref.cgi?rs=267600494"
[14] "/projects/SNP/snp_ref.cgi?rs=267606848"
[15] "/projects/SNP/snp_ref.cgi?rs=281864912"
[16] "/projects/SNP/snp_ref.cgi?rs=281864913"
[17] "/projects/SNP/snp_ref.cgi?rs=281864914"
[18] "/projects/SNP/snp_ref.cgi?rs=281864915"
[19] "/projects/SNP/snp_ref.cgi?rs=281864916"
[20] "/projects/SNP/snp_ref.cgi?rs=281864917"您会注意到,我需要4058个条目。
发布于 2014-07-15 18:08:25
这花了我整整一个下午的时间,我仍然只有一半的解决方案(第一次使用XML )。无论如何,我发现您可以使用以下链接获得XML格式的结果;
http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=SNP&term=(human[Organism])+AND+GLRA3[Gene+Name]其中db代表要搜索的数据库,而term则相当不言自明。
在结果之上,你会看到;
<Count>4736</Count>
<RetMax>20</RetMax>在此情况下,ID列表开始并显示20个ID,它们等价于rs值;
/projects/SNP/snp_ref.cgi?rs=116474260
现在,您可以使用GET函数在R中获取该信息,如果您能够想出一种方法让R读取Count行上的数字(这是可能的结果数量),然后再次使用GET函数,但现在将&RetMax=X添加到链接的末尾,其中X是Count行中的数字。
例如;
http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=SNP&term=(human[Organism])+AND+GLRA3[Gene+Name]&RetMax=4736现在,所有的ID都是在R中导入的(同样,我缺乏从数据中很好地提取ID的技能,所以这可能是其他人想出来的)。
希望这能有所帮助!
发布于 2014-07-16 04:17:01
您将希望使用@Roost找到的api。我要补充的是,httr有一个内置的方法来添加查询参数,您应该使用它,因为它将自动为您编码查询参数。
在XML中,如果您不太习惯使用xmlToList,那么使用xPath就更容易了,但是您可以选择解析XML的方法。
library(XML)
library(httr)
# Go to api and get Count
dbsnp.searchterm <- "(human[Organism]) AND GLRA3[Gene Name]"
dbsnp.url <- "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi"
dbsnp.get <- GET(dbsnp.url, query=list(db="SNP", term=dbsnp.searchterm))
dbsnp.content <- content(dbsnp.get, as="text")
dbsnp.xml <- xmlParse(dbsnp.content)
max_count <- xmlToList(dbsnp.xml)$Count
# Use the Count to form the query that you want
dbsnp.full.get <- GET(dbsnp.url, query=list(
db="SNP",
term=dbsnp.searchterm,
RetMax=max_count))
dbsnp.full.content <- content(dbsnp.full.get, as="text")
dbsnp.full.xml <- xmlParse(dbsnp.full.content)
dbsnp.full.list <- xmlToList(dbsnp.full.xml)
prefix <- "/projects/SNP/snp_ref.cgi?rs="
dbsnp.links <- paste0(prefix, unlist(dbsnp.full.list$IdList))https://stackoverflow.com/questions/24760474
复制相似问题