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

如何使用BioPython获取PubMed文献的发表日期(以PMID 10748870为例)

用BioPython通过PubMed ID获取文献发表日期

嘿,这事儿超简单!用BioPython的Entrez模块就能轻松搞定,我给你一步步拆解:

1. 先安装BioPython

如果还没装,打开终端跑这个命令就行:

pip install biopython

2. 编写核心代码

下面是完整的可运行示例,我给你标注了关键细节:

from Bio import Entrez

# 重要提醒:NCBI要求必须填写邮箱,用来在API使用异常时联系你,随便填个有效的邮箱地址就行
Entrez.email = "your.email@example.com"

def get_publication_date(pmid):
    # 调用Entrez获取指定PMID的PubMed记录,用XML格式返回方便解析
    handle = Entrez.efetch(db="pubmed", id=pmid, retmode="xml")
    records = Entrez.read(handle)
    handle.close()
    
    # 从返回的XML结构里逐层提取发表日期字段
    pub_date = records["PubmedArticle"][0]["MedlineCitation"]["Article"]["Journal"]["JournalIssue"]["PubDate"]
    
    # 处理不同的日期格式(有的文献只有年份,有的有年月,还有的带具体日期)
    year = pub_date.get("Year")
    month = pub_date.get("Month")
    
    if year and month:
        # 直接返回「月份缩写 年份」的格式,比如Dec 1999
        return f"{month} {year}"
    elif year:
        return year
    else:
        return "无法获取发表日期"

# 测试你的示例PMID
test_pmid = "10748870"
print(get_publication_date(test_pmid))  # 输出:Dec 1999

关键细节说明

  • 邮箱必填:NCBI的API有访问限制,填写邮箱是为了避免被判定为恶意请求,完全不会泄露隐私
  • 格式兼容:不同文献的日期字段可能不全,代码里做了判断,能适配只有年份的情况
  • XML解析优势:用retmode="xml"返回的结构更规整,比纯文本好处理得多

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:27:41