从PubMed获取数据

时间:2018-02-03 02:03:01

标签: python web-scraping pubmed rentrez

我编写了以下函数,使用Entrez从PubMed中提取数据:

def getFromPubMed(id):
    handle = Entrez.efetch(db="pubmed",rettype="medline",retmode="text", id=str(id))
    records = Medline.parse(handle)
    for record in records:
        abstract = str(record["AB"])
        mesh = str(record["MH"]).replace("'", "").replace("[", "").replace("]", "")
        pmid = str(record["PMID"])
        title = str(record["TI"]).replace("'", "").replace("[", "").replace("]", "")
        pt = str(record["PT"]).replace("'", "").replace("[", "").replace("]", "")
        au = str(record["AU"])
        dp = str(record["DP"])
        la = str(record["LA"])
        pmc = str(record["PMC"])
        si = str(record["SI"])
        try:
            doi=str(record["AID"])
        except:
            doi = str(record["SO"]).split('doi:',1)[1]
        return pmid, title, abstract, au, mesh, doi, pt, la, pmc

但是,此功能并不总是有效,因为并非所有MEDLINE记录都包含所有字段。例如,此PMID不包含任何MeSH标题。

我可以使用try-except语句包装每个项目,例如abstract

try:
  abstract = str(record["AB"])
except:
  abstract = ""

但实现这一点似乎是一种笨重的方式。什么是更优雅的解决方案?

2 个答案:

答案 0 :(得分:2)

您可以将提取字段的操作拆分为单独的方法 - 执行以下操作:

def get_record_attributes(record, attr_details):
    attributes = {}

    for attr_name, details in attr_details.items():
        value = ""
        try:
            value = record[details["key"]]

            for char in details["chars_to_remove"]:
                value = value.replace(char, "")
        except KeyError, AttributeError:
            pass

        attributes[attr_name] = value

    return attributes

def getFromPubMed(id):
    handle = Entrez.efetch(db="pubmed",rettype="medline",retmode="text", id=str(id))
    records = Medline.parse(handle)
    for record in records:
        attr_details = {
            "abstract" : {"key" : "AB"},
            "mesh" : { "key" : "MH", "chars_to_remove" : "'[]"},
            #...
            "aid" : {"key" : "AB"},
            "so" : {"key" : "SO"},
        }

        attributes = get_record_attributes(record, attr_details)

       #...

答案 1 :(得分:-1)

怎么样:

mesh = str(record["MH"] or '')

由于空字典FALSEthis post建议