【问题标题】:I'm having difficulty using Beautiful Soup to scrape data from an NCBI website我在使用 Beautiful Soup 从 NCBI 网站上抓取数据时遇到困难
【发布时间】:2021-07-17 22:34:10
【问题描述】:

我这辈子都不知道如何使用漂亮的汤从网页中刮取隔离源信息,例如: https://www.ncbi.nlm.nih.gov/nuccore/JOKX00000000.2/

我一直在尝试检查该标签是否存在,当我知道它确实存在时,它会不断返回它不存在。如果我什至无法验证它是否存在,我不知道如何抓取它。

谢谢!

【问题讨论】:

    标签: web-scraping beautifulsoup bioinformatics ncbi


    【解决方案1】:

    当有NCBI-EUtilities 网络服务时,你应该刮掉 ncbi。

    wget -q -O - "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=nuccore&id=JOKX00000000.2&rettype=gb&retmode=xml" | xmllint --xpath '//GBQualifier[GBQualifier_name="isolation_source"]/GBQualifier_value/text()' - && echo
    
    Type II sourdough
    

    【讨论】:

      【解决方案2】:

      数据是从外部 URL 加载的。要获取isolation_source,可以使用这个例子:

      import re
      import requests
      from bs4 import BeautifulSoup
      
      url = "https://www.ncbi.nlm.nih.gov/nuccore/JOKX00000000.2/"
      soup = BeautifulSoup(requests.get(url).content, "html.parser")
      ncbi_uidlist = soup.select_one('[name="ncbi_uidlist"]')["content"]
      
      api_url = "https://www.ncbi.nlm.nih.gov/sviewer/viewer.fcgi"
      
      params = {
          "id": ncbi_uidlist,
          "db": "nuccore",
          "report": "genbank",
          "extrafeat": "null",
          "conwithfeat": "on",
          "hide-cdd": "on",
          "retmode": "html",
          "withmarkup": "on",
          "tool": "portal",
          "log$": "seqview",
          "maxdownloadsize": "1000000",
      }
      
      soup = BeautifulSoup(
          requests.get(api_url, params=params).content, "html.parser"
      )
      features = soup.select_one(".feature").text
      
      isolation_source = re.search(r'isolation_source="([^"]+)"', features).group(1)
      print(features)
      print("-" * 80)
      print(isolation_source)
      

      打印:

           source          1..12
                           /organism="Limosilactobacillus reuteri"
                           /mol_type="genomic DNA"
                           /strain="TMW1.112"
                           /isolation_source="Type II sourdough"
                           /db_xref="taxon:1598"
                           /country="Germany"
                           /collection_date="1998"
      
      --------------------------------------------------------------------------------
      Type II sourdough
      

      【讨论】:

      • 天哪,终于成功了!非常感谢!!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 2021-06-28
      相关资源
      最近更新 更多