Guest User

Untitled

a guest
Jan 18th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3. import re
  4. import lxml
  5. from pathlib import Path
  6. import sys
  7. import time
  8.  
  9. url = "https://github.com/wangzhengya/allitebooks/tree/master/results"
  10. html = requests.get(url)
  11. soup = BeautifulSoup(html.content, 'lxml')
  12. sources_list = []
  13.  
  14. for link in soup.findAll('a', attrs={'href': re.compile('/wangzhengya/allitebooks/blob/master/results/')}):
  15.     source = ('https://github.com' + link.get('href'))
  16.     sources_list.append(source)
  17.  
  18.  
  19. def downloader(url, folder):
  20.    
  21.     path_to_folder = Path.home() / 'downloads' / 'pdf-books' / folder
  22.     path_to_folder.mkdir(exist_ok=True, parents=True)
  23.     pdf_file = url.split('/')[-1]
  24.  
  25.     check_for_existance_FILE = Path.joinpath(path_to_folder, pdf_file)
  26.    
  27.     if check_for_existance_FILE.is_file() is False:
  28.         try:
  29.             pdf = requests.get(url)
  30.             with open(Path.joinpath(path_to_folder, pdf_file), 'wb') as FILE:
  31.                 FILE.write(pdf.content)
  32.  
  33.             print(str(pdf_file) + ' copied to ' + str(folder))
  34.  
  35.             time.sleep(2)
  36.  
  37.         except:
  38.             e = sys.exc_info()[0]
  39.             print("An exception occurred:")
  40.             print(e)
  41.  
  42.     else:
  43.         print('File already downloaded -> ' + str(check_for_existance_FILE))
  44.  
  45. def get_file_urls_from_source(source):
  46.     folder = source.split('/')[-1].split('.')[-2]
  47.     pdf_urls = requests.get(source)
  48.     soup = BeautifulSoup(pdf_urls.content, 'lxml')
  49.  
  50.     for link in soup.findAll('td', attrs={'class': re.compile('blob-code blob-code-inner js-file-line')}):
  51.         pdf = (link.get_text())
  52.        
  53.         downloader(pdf, folder)
  54.  
  55.  
  56. for i, source in enumerate(sources_list):
  57.     print(i+1, source)
  58.     get_file_urls_from_source(source)
Advertisement
Add Comment
Please, Sign In to add comment