栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pubmed文献批量化下载器

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pubmed文献批量化下载器

1.代码如下

import time
import requests
import pandas as pd
import os

def getArticle(PMCID,NIHMSID,DOI,title,path):
    print(PMCID,NIHMSID,DOI,title,path)
    os.chdir(path)
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"}
    if (PMCID!="NA" and NIHMSID != "NA"):
        print("PMCID & NIHMSID")
        NIHMSID = str(NIHMSID).replace("NIHMS", "nihms")
        urls=f'https://www.ncbi.nlm.nih.gov/pmc/articles/{PMCID}/pdf/{NIHMSID}.pdf'
        r = requests.get(urls, headers=headers)
        with open(title + ".pdf", 'wb') as f:
            f.write(r.content)
    if (PMCID!="NA" and NIHMSID =="NA"):
        print("PMCID")
        DOI_after=DOI.split("/")
        urls = f'https://www.ncbi.nlm.nih.gov/pmc/articles/{PMCID}/pdf'
        r = requests.get(urls, headers=headers)
        with open(title + ".pdf", 'wb') as f:
            f.write(r.content)
    if (PMCID=="NA" and NIHMSID=="NA"):
        print("!PMCID & !NIHMSID")
        urls = f'https://sci.bban.top/pdf/{DOI}.pdf#view=FitH'
        print(DOI)
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"}
        r = requests.get(urls, headers=headers)
        with open(title+".pdf",'wb')as f:
            f.write(r.content)

def getID(path,file):
    os.chdir(path)
    Article_info=pd.read_csv(file,header=0)
    Article_info=Article_info.fillna("NA")
    print(Article_info)
    for i in range(0,Article_info.shape[0]):
    #for i in range(4, 5):
        print(Article_info.loc[i])
        title=Article_info.loc[i,"Title"]
        title=str(title).replace(":","-").replace("\","-")
        PMCID=Article_info.loc[i,"PMCID"]
        NIHMSID=Article_info.loc[i,"NIHMS ID"]
        DOI=Article_info.loc[i,"DOI"]
        print(PMCID,NIHMSID,DOI,title,path)
        try:
            getArticle(PMCID,NIHMSID,DOI,title,path)
        except:
            pass
        time.sleep(2)
def getPaperTable(path):
    allfile=os.listdir(path)
    string="title"+"t"+"downloadResult"+"n"
    os.chdir(path)
    for paper in allfile:
        if ".pdf" in paper:
            with open(paper,"rb") as fp:
                context=fp.read()
                if "404 Not Found" in str(context):
                    string=string+paper+"t"+"false"+"n"
                else:
                    string=string+paper+"t"+"true"+"n"
    with open("downloadResult.tsv",'w')as res:
        res.write(string)

print("请输入存放文献信息表格的路径:")
path=input()
print("请输入存放文献信息表格的名字:")
file=input()
getID(path,file)
getPaperTable(path)

2.使用

2.1 getArticle(PMCID,NIHMSID,DOI,title,path):

通过文章的PMCID或DOI从pubmed数据库或者SCI-hub数据库获取文章

2.2 getID(path,file):

从pubmed获得的下载文献信息的表格读取要下载的文章。

表格可以从pubmed一键导出(如下图):

2.3 getPaperTable(path):

获取文献下载情况的信息,由于文章信息的不完整,有个别文章会缺少DOI和PMCID,导致下载失败。

3.可执行文件

可通过pyinstaller自行打包。

也可通过回复文献批量下载器获取:

下面是淘宝链接:

https://item.taobao.com/item.htm?ft=t&id=675883011497

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/929565.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号