Guest User

StandardEbooks scraper

a guest
Mar 30th, 2021
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. from bs4 import BeautifulSoup
  2. import requests as req
  3.  
  4. import os
  5.  
  6. BASE_URL = "https://standardebooks.org"
  7.  
  8. for i in range(1, 43):
  9.     r = req.get(BASE_URL + "/ebooks?page=" + str(i))
  10.  
  11.     soup = BeautifulSoup(r.text, "html.parser")
  12.  
  13.     urls = [link.get('href') for link in soup.find_all('a')]
  14.  
  15.     book_urls = [url for url in urls[:-49] if type(url) is str and "ebooks" in url and url.count('/') >= 2]
  16.  
  17.     unique_book_urls = book_urls[::3]
  18.  
  19.     for working_url in unique_book_urls:
  20.         rr = req.get(BASE_URL + working_url)
  21.  
  22.         new_soup = BeautifulSoup(rr.text, "html.parser")
  23.  
  24.         page_urls = [link.get('href') for link in new_soup.find_all('a')]
  25.  
  26.         try:
  27.             epub_link = [url for url in page_urls if ".epub" in url][0]
  28.         except Exception:
  29.             break
  30.  
  31.         wget_string = "wget --no-clobber {}".format(BASE_URL + epub_link)
  32.  
  33.         os.system(wget_string)
Advertisement
Add Comment
Please, Sign In to add comment