【问题标题】:Speed up HTML scraping in R加速 R 中的 HTML 抓取
【发布时间】:2021-10-28 14:12:50
【问题描述】:

我目前正在使用readlines() 将 HTML 代码拉入 R 中的临时对象,但发现速度很慢。对于执行大量迭代,人们会推荐哪种工具更快?

作为背景,我正在查看一些祖先 DNA 结果,这些结果有 600,000 多个潜在变体可供查看。

当前代码:

   for (rs in seq(1,nrow(ancest_DNA)))
      temp_rs <- ancest_DNA[rs,"rsid"]
      print(paste0("Iteration ",which(ancest_DNA$rsid == temp_rs)))
      print(paste0("RSID ",temp_rs))
      #Search for any clinical significance on Clinvar
      NCBI <- readLines(paste("https://www.ncbi.nlm.nih.gov/snp/",temp_rs,sep = ""),n = 800)

使用 reutils 更新代码

for(rs in seq(1, nrow(ancest_DNA))) {
  temp_rs <- ancest_DNA[rs,"rsid"]
  info <- efetch(temp_rs, db = "snp", strand = 1)
  info2 <- xmlToDataFrame(info$`.->content`)
  print(paste0("Interation: ", rs))
  ancest_DNA[rs,"ClinSig"] <- info2["1","CLINICAL_SIGNIFICANCE"]
  ancest_DNA[rs,"Gene"] <- info2["1","GENES"]
  temp_allele <- info2$SPDI
  temp_allele2 <- strsplit(temp_allele, ",")
  for (nt in seq(1,length(temp_allele2[[1]]))) {
    temp_allele2[[1]][nt] <- str_extract(temp_allele2[[1]][nt], "(?<=:\\D:).*")
  }
  if (length(temp_allele2[[1]]) == 1) {
    ancest_DNA[rs,"PathAllele"] <- temp_allele2[[1]]
  }
  if (length(temp_allele2[[1]]) == 2) {
    ancest_DNA[rs,"PathAllele"] <- temp_allele2[[1]][1]
    ancest_DNA[rs,"PathAllele2"] <- temp_allele2[[1]][2]
  }
}

【问题讨论】:

    标签: r readlines doparallel ncbi


    【解决方案1】:

    您是否考虑过并行运行您的请求,而不是在for-loop 中?这样您就可以同时发出多个请求。但是,您应该始终检查您正在抓取的页面的服务条款。

    此外,您最好使用他们的 API。 对于NCBI,看看他们的E-utility tool

    【讨论】:

    • 谢谢 mhovd 我曾尝试让 E-utility 工具工作,但之前遇到了困难,现在我已经让 efetch 工作,这大大减少了每次迭代的时间,我无法获得 doparellel 包工作我将在下面发布更新的代码,有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    相关资源
    最近更新 更多