LDShadowLord

Web Strip

Aug 17th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. #Begin Imports
  2.  
  3. import os
  4. import urllib.request
  5. from bs4 import BeautifulSoup
  6.  
  7.  
  8. #Begin Variables
  9.  
  10. Blank_Target_URL = "http://www.talesofmu.com/book0x/"
  11. Tags_To_Remove = ["strong","b"]
  12. Allow_Characters = ('_','-')
  13. File_Directory = "Directory\\Path\\"
  14. File_Header = "Test_Header\n\nTestAuthor\n\nBEGIN\n\n"
  15.  
  16. #Begin Main Loop
  17.  
  18. for i in range(405, 497):
  19.   soup = BeautifulSoup( urllib.request.urlopen( Blank_Target_URL+str(i) ).read() , "html5lib")
  20.   blank_slate = [s.extract() for s in soup(Tags_To_Remove)]
  21.   blank_slate = ""
  22.   #The below line generates the filename - This could use vars, but I couldn't work out a clean way to do it without rewriting everything.
  23.   currentlistitem = str([s.extract() for s in soup(["h2"])][1]).replace("<h2>","").replace("</h2>","").replace(": ","-").replace(" ","_")
  24.   currentlistitem = "".join(c for c in currentlistitem if c.isalnum() or c in Allow_Characters ).rstrip()
  25.   print(currentlistitem)
  26.  
  27.   f = open(File_Directory+currentlistitem+".txt", mode="w", errors="ignore")
  28.   f.write(File_Header)
  29.  
  30.   for row in soup.find_all('div',attrs={"class" : "entry"}):
  31.     f.write(str(row.text).strip().replace("\n","\n\n"))
  32.  
  33.   f.close()
Advertisement
Add Comment
Please, Sign In to add comment