Python批量下载sci-hub文献

2023-05-16

# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
import os,re

path = ".\\Downloaded\\"
if os.path.exists(path) == False:
 os.mkdir(path)
if os.path.exists("error.txt") ==True:
 os.remove("error.txt")
f = open("doi.txt", "r", encoding="utf-8")  #存放DOI码的.txt文件中,每行存放一篇参考文献
# (endnote导出APA6th,自行调整第一作者之后以et al代替),完毕须换行(最后一个也须换行!)
head = {\
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36'\
            }  #20210607更新,防止HTTP403错误
err_num = 0

def Download(url,author_date):
 file = path + author_date + "pdf"
 if os.path.exists(file) == False:
  r = requests.get(url, headers=head)
  r.raise_for_status()
  r.encoding = r.apparent_encoding
  soup = BeautifulSoup(r.text, "html.parser")
  download_url = soup.iframe.attrs["src"]
  download_r = requests.get(download_url, headers=head)
  download_r.raise_for_status()
  with open(file, "wb+") as temp:
   temp.write(download_r.content)
   print(author_date + "pdf downloaded!")
 else:
  print(author_date + "pdf already exists!")

for line in f.readlines():
 line = line[:-1] #去换行符
 author_date = line.split(').')[0] +")."
 doi_pattern = 'doi:.+'
 if (len(re.findall(doi_pattern, line)) != 0):
  doi = line.split('doi:')[1]
  url = "https://www.sci-hub.ren/doi:" +doi + "#"
  try:
   Download(url,author_date)
  except:
   err_num = err_num + 1
   with open("error.txt", "a+", encoding="utf-8") as error:
    error.write(str(err_num) + "." + line + " occurs error!\n --download_url may be:\n" + url + "\n")
   print(line + "\n" + "Failed to download!!!")
 else:
  print(line + "\n"+"doi not found,failed to download!!!")
  err_num = err_num + 1
  with open("error.txt", "a+", encoding="utf-8") as error:
   error.write(str(err_num) + "." + line + " occurs error!\n")
f.close()

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python批量下载sci-hub文献 的相关文章

随机推荐

  • GNURadio中的PMTs(Polymorphic Types)数据类型

    目录 1 整体概述 2 使用方法的举例说明 3 对于PMT类型的补充说明 1 整体概述 PMTs在GNURadio中代表多态类型 xff08 Polymorphic Types xff09 xff0c 这种类型不像float int一样是严
  • STM32F103C8T6初学笔记

    STM32F103C8T6初学笔记 ST官网链接 xff1a http www stmicroelectronics com cn ST MCU网站链接 xff1a http www stmcu com cn 初识STM32 STM32是3
  • STM32F103 72MHz时钟设置

    将系统时钟初始化到72MHz的函数 根据数据手册和库函数 xff0c 设置STM32时钟为72MHz 这是 c文件 span class token macro property span class token directive key
  • C++ 类和对象学习 —— 继承

    1 6 继承 利用继承技术 xff0c 可以减少重复代码 1 6 1 继承的基本语法 普通实现 span class token macro property span class token directive keyword inclu
  • 解决 VS 无法打开包括文件: “XXX.h”: No such file or directory问题

    每次封装管理 xff0c 当 Visual Studio 包含多个 h 文件和 c 文件 xff0c 运行时总会发生如下错误 错误 C1083 无法打开包括文件 XXX h No such file or directory test1 0
  • C++ 多态深入学习总结笔记

    多态和虚函数 1 通过案例理解多态 案例 xff1a 父类Animal xff0c 2个子类Dog和Cat xff0c 实现speak方法 未使用虚函数 virtual 声明 main h 文件 span class token keywo
  • LaTeX 报错! Missing $ inserted. <inserted text>$ l.44 问题解决

    学习LaTeX编辑器编辑数学公式时 xff0c 输入如下 xff1a 编译报错如下 xff1a 搜索方法 xff0c 并未得到有效解决 xff0c 机缘巧合把空行删除 xff0c 如下图所示 xff1a 再次编译未报错 xff0c 成功运行
  • 在 Microsoft Word 插入代码块(无需下载任何软件)

    Step 1 打开 CSDN Markdown 编辑器 xff0c 点击菜单栏上方代码块 xff0c 选择自己的代码语言 Step 2 插入代码如下图所示 xff0c 之后将代码复制 Step 3 打开 Microsoft Word xff
  • MATLAB 利用YALMIP+Gurobi 求解线性规划 -多无人机扫描覆盖

    使用要点 创建决策变量设置目标函数添加约束条件参数配置求解问题 问题描述 假设M个无人机的任务是尽快覆盖一组由 P 顶点表示的多边形凸区域 xff0c 假设每架无人机的最大飞行时间是有限的 xff0c 并且是预先知道的 每架无人机的都配备了
  • 毕业论文格式系列1 Word 图片交叉引用其题注

    图表论文自动编号 自动编号可以通过 Word 的 题注 功能实现 按论文格式要求 xff0c 第一章的图编号格式为 图1 X xff0c 具体做法如下 xff1a 将图插入文档中后 xff0c 选中新插入的图 xff0c 在 引用 菜单选
  • Visual Studio 2022 编译新版 Mission Planner 地面站

    下载安装VS 2022 安装时 xff0c 注意勾选 安装成功后 xff0c 从Visual Studio官方SDKs下载net461开发包 xff0c 网址 xff1a https dotnet microsoft com en us d
  • GNU Radio中的流标签(Stream Tags)

    目录 0 GR 中常用术语的官方解释 1 定义概述 2 在数据流中添加标签 3 添加标签的demo举例 4 从数据流中的获取标签 5 提取标签的demo举例 0 GR 中常用术语的官方解释 直接吧官方的解释抄过来 xff0c 直接看英文更容
  • 飞控学习随记

    常见指令 编译Arduplane程序 span class token builtin class name cd span ardupilot waf plane 进入 Tools autotest 文件夹中 xff0c 启动3D fli
  • 【无标题】

    apm飞控飞行模式详解 1 稳定模式Stabilize 稳定模式是使用得最多的飞行模式 xff0c 也是最基本的飞行模式 xff0c 起飞和降落都应该使用此模式 此模式下 xff0c 飞控会让飞行器保持稳定 xff0c 是初学者进行一般飞行
  • C# CustomMessageBox.Show() 输出多个变量调试

    Mission Planner 地面站调试中会遇到输出多个变量问题 xff0c 这里采用CustomMessageBox Show来输出调试多个变量 xff0c 用到string Format方法 span class token clas
  • MapReduce实验——学生总成绩报表,学生平均成绩

    学生总成绩报表 Map类 span class token keyword package span span class token class name StudentScore 06 span span class token pun
  • 【Docker操作必看,原来这才是正确打开Docker的新方式】

    前言 一 Docker操作镜像 首先镜像名称一般分为两个部分 xff1a repository tag xff0c 前者是镜像名 xff0c 后者是版本号 在没有指定tag的情况下 xff0c 默认是latest 代表的是最新版本 1 拉取
  • 第五章 FreeRTOS 任务基础知识

    5 1 什么是多任务系统 在使用 51 AVR STM32 单片机裸机 未使用系统 的时候一般都是在main 函数里面用 while 1 做一个大循环来完成所有的处理 xff0c 即应用程序是一个无限的循环 xff0c 循环中调用相应的函数
  • C语言for循环详解

    for 循环的使用更加灵活 xff0c 在日常的程序开发过程中我们会使用的更多一些 使用 while 循环来计算1加到100的值 xff0c 代码如下 xff1a include span class token generics func
  • Python批量下载sci-hub文献

    coding utf 8 import requests from bs4 import BeautifulSoup import os re path 61 34 Downloaded 34 if os path exists path