Repository navigation
Expand file tree
/
Copy pathgetpdf.py
More file actions
119 lines (96 loc) · 3.45 KB
/
Copy pathgetpdf.py
File metadata and controls
119 lines (96 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import os
import sys
import threading
import time
from urllib.parse import urljoin
import urllib.request
import requests
from bs4 import BeautifulSoup
def extractor(sites):
'''Extract pdfs from a single or a list of urls or html filenames passed.'''
if_file=os.getcwd()
os.makedirs('DownloadedPdfs', exist_ok=True)
os.chdir('DownloadedPdfs')
return_dir= os.getcwd()
header = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36",
"X-Requested-With": "XMLHttpRequest"}
for i in range(len(sites)):
site = sites[i].strip()
isAFile=False
if (len(site.split('.'))==2) and ('.html' in site):
isAFile=True
os.chdir(if_file)
try:
with open(site, 'r', encoding='utf8') as f:
content= f.read()
except Exception as e:
print(e)
print('Place file: ' + site + ' in ' + os.getcwd())
if len(site.split('//'))>1:
fname = site.split('//')[1]
else:
fname = site
site = 'http://'+site
print('Extracting pdfs from: ' + site)
try:
if isAFile==False:
req=urllib.request.Request(site, headers=header)
content = urllib.request.urlopen(req).read()
soup = BeautifulSoup(content, 'html.parser')
else:
soup = BeautifulSoup(content, 'html.parser')
except Exception as e:
print(e)
os.chdir(return_dir)
os.makedirs(fname, exist_ok=True)
os.chdir(fname)
links = soup.find_all('a')
pdflist=[]
for link in links:
z= link.get('href')
if '.pdf' in z:
pdflist.append(urljoin(site,z))
total_files=len(pdflist)
print(str(total_files) + ' pdf files are available in ' + site)
print('')
j=0
fourlists=[[],[],[],[]]
for k in range(len(pdflist)):
rem=k%4
fourlists[rem].append(pdflist[k])
fourthreads=[]
for m in range(4):
arg=fourlists[m]
if len(arg)!=0:
threadname='athread'+str(m)
threadname =threading.Thread(target= downloader, args=[arg])
fourthreads.append(threadname)
for thread in fourthreads:
thread.start()
for thread in fourthreads:
thread.join()
print('')
print(str(total_files) + ' pdf files extraced from ' + site+ ' at '+ os.getcwd())
def downloader(threadlist):
for j in range(len(threadlist)):
filename= threadlist[j].split('/')[-1]
try:
response =requests.get(threadlist[j])
print('Downloading... ' + filename)
with open(filename, 'wb') as file:
file.write(response.content)
print('Downloaded ' + filename)
print('')
except:
print('Failed to download ' +filename )
if len(sys.argv)>1:
sites = sys.argv[1:]
else:
print('Enter URL (you can also pass list of urls using comma as seperator) OR You can also give filename of htmlfile: ' )
sites=input()
sites= sites.split(',')
starttime=time.time()
if __name__ == '__main__':
extractor(sites)
endtime= time.time()
print('Total time taken by download is', endtime-starttime)