-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf_download.py
More file actions
41 lines (29 loc) · 1.18 KB
/
Copy pathpdf_download.py
File metadata and controls
41 lines (29 loc) · 1.18 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
"""Webscraping: download pdf from CSEP 590B Explainable AI (Su-In Lee, et al.)
https://sites.google.com/cs.washington.edu/csep590b
"""
#%%
import os
import requests
from urllib.request import urlopen
from urllib.parse import urljoin
from bs4 import BeautifulSoup
import ssl
#%%
url_is = 'https://sites.google.com/cs.washington.edu/csep590b'
#If there is no such folder, the script will create one automatically
folder_location = r'outputs/pdf_download/'
if not os.path.exists(folder_location):os.mkdir(folder_location)
context = ssl._create_unverified_context()
def get_pdf_v1(url_is, folder_location):
response = urlopen(url_is, context=context)
soup= BeautifulSoup(response, "html.parser")
for link in soup.find_all('a'):
current_link = link.get('href')
if current_link.endswith('pdf'):
print(f'current pdf file: {current_link}')
filename = os.path.join(folder_location,link['href'].split('/')[-1]) # the file name is the pdf name in the url
with open(filename, 'wb') as f:
f.write(requests.get(urljoin(url_is,link['href']), verify=False).content)
return print('done')
get_pdf_v1(url_is,folder_location)
#%%