Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Begin Imports
- import os
- import urllib.request
- from bs4 import BeautifulSoup
- #Begin Variables
- Blank_Target_URL = "http://www.talesofmu.com/book0x/"
- Tags_To_Remove = ["strong","b"]
- Allow_Characters = ('_','-')
- File_Directory = "Directory\\Path\\"
- File_Header = "Test_Header\n\nTestAuthor\n\nBEGIN\n\n"
- #Begin Main Loop
- for i in range(405, 497):
- soup = BeautifulSoup( urllib.request.urlopen( Blank_Target_URL+str(i) ).read() , "html5lib")
- blank_slate = [s.extract() for s in soup(Tags_To_Remove)]
- blank_slate = ""
- #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.
- currentlistitem = str([s.extract() for s in soup(["h2"])][1]).replace("<h2>","").replace("</h2>","").replace(": ","-").replace(" ","_")
- currentlistitem = "".join(c for c in currentlistitem if c.isalnum() or c in Allow_Characters ).rstrip()
- print(currentlistitem)
- f = open(File_Directory+currentlistitem+".txt", mode="w", errors="ignore")
- f.write(File_Header)
- for row in soup.find_all('div',attrs={"class" : "entry"}):
- f.write(str(row.text).strip().replace("\n","\n\n"))
- f.close()
Advertisement
Add Comment
Please, Sign In to add comment