arxiv.orgからpdfファイルをダウンロードするプログラムを作成しました。それはダウンロードボットではありませんが、私はプログラムを稼働させるために現時点で使用しています。私はそれが一般的なコードになることができます動作するようになる場合。コードを使用する場合は、arxiv.orgのボット警告をお読みください。彼らは一括ダウンロードのためにAmazonを参照しています。Python3.6 - インターネットからダウンロードする前にファイルがローカルフォルダにあるかどうかを確認します。
私は同じファイルを2度ダウンロードしたくないので、python-fileのフォルダにあるコンピュータに既にあるかどうかを確認します。問題は、プログラムがハードドライブ上のファイルを認識し、ダウンロードしないことがありますが、ファイルを再度ダウンロードすることがあるため、正常に動作しないことです。
私は何かをするコードを信じていますが、何か他のことをしています。
以下のコード:
import requests
from bs4 import BeautifulSoup
import re
import os
import os.path
axx= '.pdf'
# specify the URL of the archive here
archive_url = "https://arxiv.org/list/quant-ph/new"
def get_links():
# create response object
r = requests.get(archive_url)
# create beautiful-soup object
soup = BeautifulSoup(r.content,'html5lib')
# find all links on web-page
links = soup.findAll('a', attrs = {'title' : 'Download PDF'})
#take out the numbering of pdf-files on arxiv.
#links = re.findall(('\d+'+'.'+'\d+'), links)
# filter the link sending with .mp4 or other file type
''''
source 1
https://arxiv.org/list/math/new
html = a href="/pdf/1705.04681" title="Download PDF">pdf</a>
source 2
'''
#_links = [archive_url + link['href'] for link in links if link['href'].endswith('mp4')]
file_links = ['https://arxiv.org'+link['href'] for link in links if link['href']+axx]
#rinse duplicates fomr list if file already is on harddrive
file_links = list(set(file_links))
for link in file_links:
if os.path.isfile(os.getcwd()+'/'+link.split('/')[-1]+axx) == True:
file_links.remove(link)
else:
return file_links
def download_series(file_links):
for link in file_links:
# obtain filename by splitting url and getting
# last string
file_name = (link.split('/')[-1]+axx)
print("Downloading file:%s"%file_name)
# create response object
r = requests.get(link, stream = True)
# download started
with open(file_name, 'wb') as f:
for chunk in r.iter_content(chunk_size = 2048*2048):
if chunk:
f.write(chunk)
print("%s downloaded!\n"%file_name)
print("All files downloaded!")
return
if __name__ == "__main__":
# getting all pdf links
file_links = get_links()
# download all pdfs
download_series(file_links)