Skip to content

Instantly share code, notes, and snippets.

@sayinserdar
Created March 12, 2018 09:27
Show Gist options
  • Save sayinserdar/7a19d2e93103835f1f772324df6b7841 to your computer and use it in GitHub Desktop.
Save sayinserdar/7a19d2e93103835f1f772324df6b7841 to your computer and use it in GitHub Desktop.
Tiny python script for downloading pdf file through web site link
from bs4 import BeautifulSoup
import urllib.request
import requests
import os
with urllib.request.urlopen('Your URL') as response:
html_doc = response.read()
def download_file(url):
local_filename = url.split('/')[-1]
r = requests.get(url, stream=True)
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
return local_filename
base_url = 'Your URL'
soup = BeautifulSoup(html_doc, 'html.parser')
list = []
for html in soup.find_all('a'):
length = len(html.get('href'))
if html.get('href')[length-3:length].lower() == "pdf":
list.append(base_url + html.get('href'))
for link in list:
print(link)
download_file(link)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment