Advertisement
WSzP

Springer grabber function

Apr 8th, 2020
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.94 KB | None | 0 0
  1. import os
  2. import requests
  3. import pandas as pd
  4. from tqdm import tqdm
  5. DIR = './books/'
  6. books = pd.read_excel('https://resource-cms.springernature.com/springer-cms/rest/v1/content/17858272/data/v4')
  7. books.to_excel(DIR + 'table.xlsx')
  8. books['English Package Name'].value_counts()
  9. def get_the_books(books=books):
  10.     for url, title, author, pk_name in tqdm(books[['OpenURL', 'Book Title', 'Author', 'English Package Name']].values):
  11.         new_folder = DIR + pk_name + '/'
  12.         if not os.path.exists(new_folder):
  13.             os.mkdir(new_folder)
  14.         r = requests.get(url)
  15.         new_url = r.url
  16.         new_url = new_url.replace('/book/','/content/pdf/')
  17.         new_url = new_url.replace('%2F','/')
  18.         new_url = new_url + '.pdf'
  19.         final = new_url.split('/')[-1]
  20.         final = title.replace(',','-').replace('.','').replace('/',' ') + ' - ' + author.replace(',','-').replace('.','').replace('/',' ') + ' - ' + final
  21.         myfile = requests.get(new_url, allow_redirects=True)
  22.         open(new_folder+final, 'wb').write(myfile.content)
  23.         new_url = r.url
  24.         new_url = new_url.replace('/book/','/download/epub/')
  25.         new_url = new_url.replace('%2F','/')
  26.         new_url = new_url + '.epub'
  27.         final = new_url.split('/')[-1]
  28.         final = title.replace(',','-').replace('.','').replace('/',' ') + ' - ' + author.replace(',','-').replace('.','').replace('/',' ') + ' - ' + final  
  29.         request = requests.get(new_url)
  30.         if request.status_code == 200:
  31.             myfile = requests.get(new_url, allow_redirects=True)
  32.             open(new_folder+final, 'wb').write(myfile.content)
  33.     return 'I got all the books from the specified category. Cheers!'
  34. get_the_books(books[books['English Package Name'] == 'Mathematics and Statistics'])
  35. get_the_books(books[books['English Package Name'] == 'Behavioral Science and Psychology'])
  36. get_the_books(books[books['English Package Name'] == 'Business and Economics'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement