【问题标题】:How to click links onto the next page using RCurl?如何使用 RCurl 点击链接到下一页?
【发布时间】:2013-12-06 14:37:04
【问题描述】:

我正在尝试使用 RCurl 从this website 抓取此表。我能够做到这一点并使用代码将其放入一个不错的数据框中:

clinVar <- getURL("http://www.ncbi.nlm.nih.gov/clinvar/?term=BRCA1")
docForm2 <- htmlTreeParse(clinVar,useInternalNodes = T) 

xp_expr = "//table[@class= 'jig-ncbigrid docsum_table\']/tbody/tr"
nodes = getNodeSet(docForm2, xp_expr)

extractedData <- xmlToDataFrame(nodes)
colnames(extractedData) <- c("Info","Gene", "Variation","Freq", "Phenotype","Clinical significance","Status", "Chr","Location")

但是,我只能提取第一页的数据,而且表格跨越了多个页面。您如何访问下一页上的数据?我查看了网站的 HTML 代码,“下一步”按钮所在的区域在这里(我相信!):

<a name="EntrezSystem2.PEntrez.clinVar.clinVar_Entrez_ResultsPanel.Entrez_Pager.Page" title="Next page of results" class="active page_link next" href="#" sid="3" page="3" accesskey="k" id="EntrezSystem2.PEntrez.clinVar.clinVar_Entrez_ResultsPanel.Entrez_Pager.Page">Next &gt;</a>

我想知道如何使用getURLpostForm 等访问此链接。我想我应该这样做,从第二页获取数据,但它仍然只是给我第一页:

url <- "http://www.ncbi.nlm.nih.gov/clinvar/?term=BRCA1"
clinVar <- postForm(url,
                "EntrezSystem2.PEntrez.clinVar.clinVar_Entrez_ResultsPanel.Entrez_Pager.cPage" ="2")
docForm2 <- htmlTreeParse(clinVar,useInternalNodes = T) 

xp_expr = "//table[@class= 'jig-ncbigrid docsum_table\']/tbody/tr"
nodes = getNodeSet(docForm2, xp_expr)

extractedData <- xmlToDataFrame(nodes)
colnames(extractedData) <- c("Info","Gene", "Variation","Freq","Phenotype","Clinical significance","Status", "Chr","Location")

感谢任何可以提供帮助的人。

【问题讨论】:

    标签: html r web-scraping rcurl


    【解决方案1】:

    我会改用 E-utilities 来访问 NCBI 的数据。

    url <- "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=clinvar&term=brca1"
    readLines(url)
    [1] "<?xml version=\"1.0\" ?>"                                                                                                                                                                                                                                                                       
    [2] "<!DOCTYPE eSearchResult PUBLIC \"-//NLM//DTD eSearchResult, 11 May 2002//EN\" \"http://www.ncbi.nlm.nih.gov/entrez/query/DTD/eSearch_020511.dtd\">"                                                                                                                                             
    [3] "<eSearchResult><Count>1080</Count><RetMax>20</RetMax><RetStart>0</RetStart><QueryKey>1</QueryKey><WebEnv>NCID_1_36649974_130.14.18.34_9001_1386348760_356908530</WebEnv><IdList>"                                                                                                               
    

    将 QueryKey 和 WebEnv 传递给 esummary 并获取 XML 摘要(每次搜索都会更改,因此将新键复制并粘贴到下面的 url 中)

    url2 <-  "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=clinvar&query_key=1&WebEnv=NCID_1_36649974_130.14.18.34_9001_1386348760_356908530"
    brca1 <- xmlParse(url2)
    

    接下来,查看单个记录,然后提取您需要的字段。如果为标签分配了 0 到多个值,您可能需要遍历该集合。其他像临床意义描述总是有1个值。

    getNodeSet(brca1, "//DocumentSummary")[[1]]
    table(xpathSApply(brca1, "//clinical_significance/description", xmlValue) )
    
                          Benign conflicting data from submitters                     not provided                            other 
                             129                               22                                6                                1 
                      Pathogenic          probably not pathogenic              probably pathogenic                      risk factor 
                             508                               68                               19                               43 
          Uncertain significance 
                             284 
    

    此外,在 github 和 BioC 上有许多带有 E-utilities 的软件包(rentrez、reutils、基因组等)。使用 BioC 上的基因组包,这可以简化为

    brca1 <- esummary( esearch("brca1", db="clinvar"), parse=FALSE )
    

    【讨论】:

    • 嗨,克里斯,非常感谢您的回复。我查看了 eUtilities api 并碰了壁,但是您的 getNodeSet() 使用完全帮助了我。这个已经解决了,我会发布我的答案。再次感谢!
    【解决方案2】:

    使用 NCBI 数据库中的电子实用程序功能,请参阅http://www.ncbi.nlm.nih.gov/books/NBK25500/ 了解更多详情。

    ## use eSearch feature in eUtilities to search NCBI for ids corresponding to each row of data.
    ## note to see all ids, not not just top 10 set retmax to a high number
    ## to get query id and web env info, set usehistory=y
    
    library(RCurl)
    library(XML)
    
    baseSearch <- ("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=") ## eSearch
    db <- "clinvar" ## database to query
    gene <- "BRCA1" ## gene of interest
    query <-  paste('[gene]+AND+"','clinsig pathogenic"','[Properties]+AND+"','single nucleotide     variant"','[Type of variation]&usehistory=y&retmax=1110',sep="") ## query, see below for details
    baseFetch <- "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=" ## base fetch
    
    searchURL <- paste(baseSearch,db, "&term=",gene,query,sep="")
    getSearch <- getURL(searchURL)
    searchHTML <- htmlTreeParse(searchURL, useInternalNodes =T)
    nodes <- getNodeSet(searchHTML,"//querykey") ## this name "querykey" was extracted from the HTML source code for this page
    querykey <- xmlToDataFrame(nodes)
    nodes <- getNodeSet(searchHTML,"//webenv") ## this name "webenv" was extracted from the HTML source code for this page
    webenv <- xmlToDataFrame(nodes)
    fetchURL <-     paste(baseFetch,db,"&query_key=",querykey,"&WebEnv=",webenv[[1]],"&rettype=docsum",sep="")
    getFetch <- getURL(fetchURL)
    fetchHTML <- htmlTreeParse(getFetch, useInternalNodes =T)
    nodes <- getNodeSet(fetchHTML, "//position")
    extractedDataAll <- xmlToDataFrame(nodes)
    colnames(extractedDataAll) <- c("pathogenicSNPs")
    print(extractedDataAll)
    

    请注意,我通过转到http://www.ncbi.nlm.nih.gov/clinvar/?term=BRCA1 选择我的过滤器(致病等)然后单击高级按钮来找到查询信息。最近应用的过滤器应该出现在主框中,我将其用于查询。

    【讨论】:

      【解决方案3】:

      ClinVar 现在提供整个数据库的 XML 下载,因此无需进行网络抓取。

      【讨论】:

        猜你喜欢
        • 2015-05-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多