You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Selenium/BeautifulSoup提取NCBI单组装基因组的RefSeq与GenBank编号?

问题:从NCBI提取RefSeq和GenBank组装编号的统一脚本实现

我需要用Python的Selenium和BeautifulSoup从NCBI网页提取NCBI RefSeq和Submitted GenBank组装编号。参考了相关Stack Overflow帖子的逻辑,但原代码在处理仅含单个组装的基因组时失效——因为这种情况下页面会直接跳转到组装详情页,而非搜索结果列表页,导致原逻辑无法适配。


第一种尝试方案

代码如下:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException

# 定义搜索关键词
search_term = "Streptomyces anthocyanicus NBC 01687"

# 启动Chrome浏览器
driver = webdriver.Chrome()

# 构造组装搜索URL
search_url = f"https://www.ncbi.nlm.nih.gov/assembly/?term={search_term.replace(' ', '+')}"

# 访问搜索页面
driver.get(search_url)

try:
    # 等待主内容加载完成
    main_content = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "maincontent")))

    # 获取主内容文本
    assembly_info = main_content.text if main_content else "未找到组装信息"

    # 尝试从assembly-widget表格提取编号
    try:
        assembly_table = driver.find_element(By.CLASS_NAME, "assembly-widget")
        rows = assembly_table.find_elements(By.TAG_NAME, "tr")
        for row in rows:
            cells = row.find_elements(By.TAG_NAME, "td")
            if len(cells) == 3:
                label = cells[1].text.strip()
                assembly_id = cells[2].text.strip()
                if label == "NCBI RefSeq assembly":
                    print("NCBI RefSeq assembly:", assembly_id)
                elif label == "Submitted GenBank assembly":
                    print("Submitted GenBank assembly:", assembly_id)
    except NoSuchElementException:
        print("未找到assembly-widget组件。")

except TimeoutException:
    print("元素加载超时或未找到。")

# 初始化编号存储变量
genbank_assembly = None
refseq_assembly = None

# 通过解析页面文本提取编号
lines = assembly_info.split("\n")
for i in range(len(lines)):
    if "NCBI RefSeq assembly" in lines[i]:
        refseq_assembly = lines[i+1].strip()
    elif "Submitted GenBank assembly" in lines[i]:
        genbank_assembly = lines[i+1].strip()

# 输出提取到的编号
if refseq_assembly:
    print("NCBI RefSeq assembly:", refseq_assembly)
if genbank_assembly:
    print("Submitted GenBank assembly:", genbank_assembly)

# 关闭浏览器
driver.quit()

输出结果:

Assembly information widget not found.
NCBI RefSeq assembly: GCF_036226945.1
Submitted GenBank assembly: GCA_036226945.1

这种通过解析页面全文本的方式不够优雅,需要更规范的实现方法。


第二种尝试方案

代码如下:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException

# 定义搜索关键词
search_term = "Streptomyces anthocyanicus NBC 01687"

# 启动Chrome浏览器
driver = webdriver.Chrome()

try:
    # 构造组装搜索URL
    search_url = f"https://www.ncbi.nlm.nih.gov/assembly/?term={search_term.replace(' ', '+')}"

    # 访问搜索页面
    driver.get(search_url)

    # 查找包含目标文本的元素
    elements = driver.find_elements(By.XPATH, "//*[contains(text(), 'NCBI RefSeq assembly')]")

    if elements:
        print(f"页面中找到关键词 '{search_term}'。")
        # 遍历找到的元素并获取父元素文本
        for element in elements:
            parent_element = element.find_element(By.XPATH, "..")
            print("父元素内容:")
            print(parent_element.text)
            
    else:
        print(f"页面中未找到关键词 '{search_term}'。")

except Exception as e:
    print("发生错误:", e)

finally:
    # 关闭浏览器
    driver.quit()

希望能编写一个统一脚本,适配搜索结果列表页和单个组装详情页两种场景,实现RefSeq和GenBank组装编号的提取,逻辑和参考的Stack Overflow帖子一致。请提供合适的代码实现方案。

内容的提问来源于stack exchange,提问作者Umar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 10:15:58