下载论文py脚本

import os
import time
import requests
from bs4 import BeautifulSoup

def search_article(artName):
    '''
    搜索论文
    ---------------
    输入:论文名
    ---------------
    输出:搜索结果(如果没有返回"",否则返回PDF链接)
    '''
    url = 'https://www.sci-hub.ren/'
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0',
               'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
               'Accept-Language':'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
               'Accept-Encoding':'gzip, deflate, br',
               'Content-Type':'application/x-www-form-urlencoded',
               'Content-Length':'123',
               'Origin':'https://www.sci-hub.ren',
               'Connection':'keep-alive',
               'Upgrade-Insecure-Requests':'1'}
    data = {'sci-hub-plugin-check':'',
            'request':artName}
    res = requests.post(url, headers=headers, data=data)
    html = res.text
    soup = BeautifulSoup(html, 'html.parser')
    iframe = soup.find(id='pdf')
    if iframe == None: # 未找到相应文章
        return ''
    else:
        downUrl = iframe['src']
        if 'http' not in downUrl:
            downUrl = 'https:'+downUrl
        return downUrl
        
def download_article(downUrl):
    '''
    根据论文链接下载文章
    ----------------------
    输入:论文链接
    ----------------------
    输出:PDF文件二进制
    '''
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0',
               'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
               'Accept-Language':'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
               'Accept-Encoding':'gzip, deflate, br',
               'Connection':'keep-alive',
               'Upgrade-Insecure-Requests':'1'}
    res = requests.get(downUrl, headers=headers)
    return res.content

def welcome():
    '''
    欢迎界面
    '''
    os.system('cls')
    title = '''
               _____  _____ _____      _    _ _    _ ____  
              / ____|/ ____|_   _|    | |  | | |  | |  _ \ 
             | (___ | |      | |______| |__| | |  | | |_) |
              \___ \| |      | |______|  __  | |  | |  _ < 
              ____) | |____ _| |_     | |  | | |__| | |_) |
             |_____/ \_____|_____|    |_|  |_|\____/|____/
                

            '''
    print(title)
    
if __name__ == '__main__':
    while True:
        welcome()
        request = input('请输入URL、PMID、DOI或者论文标题:')
        print('搜索中...')
        downUrl = search_article(request)
        if downUrl == '':
            print('未找到相关论文,请重新搜索!')
        else:
            print('论文链接:%s'%downUrl)
            print('下载中...')
            pdf = download_article(downUrl)
            with open('%s.pdf'%request, 'wb') as f:
                f.write(pdf)
            print('---下载完成---')
        time.sleep(0.8)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冲冲冲冲冲冲!!!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值