溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用Python怎么編寫一個論文下載器

發布時間:2021-01-18 14:38:40 來源:億速云 閱讀:174 作者:Leah 欄目:開發技術

使用Python怎么編寫一個論文下載器?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

1. 搜索論文

通過論文的URL、PMID、DOI號或者論文標題等搜索到對應的論文,并通過bs4庫找出PDF原文的鏈接地址,代碼如下:

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

2. 下載論文

得到了論文的鏈接地址之后,只需要通過requests發送一個請求,即可將其下載:

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

二、完整代碼

將上述兩個函數整合之后,我的完整代碼如下:

# -*- coding: utf-8 -*-
"""
Created on Tue Jan 5 16:32:22 2021
@author: kimol_love
"""
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)

關于使用Python怎么編寫一個論文下載器問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女