Files
mlwerke2pdf/mlwerke2pdf.py
T

54 lines
1.6 KiB
Python

import requests
from bs4 import BeautifulSoup
import urllib.parse
from weasyprint import HTML
class MLWerk:
class SubSite:
def __init__(self, url):
self.url = url
self.getsoup()
def getsoup(self):
r = requests.get(self.url)
r.encoding = 'UTF-8'
self.soup = BeautifulSoup(r.text, 'html.parser')
def __init__(self, url):
self.url = url
self.baseurl = url[:url.rfind('/')] + '/'
self.subsites = []
self.getsoup()
def getsoup(self):
r = requests.get(self.url)
r.encoding = 'UTF-8'
self.soup = BeautifulSoup(r.text, 'html.parser')
def getsubsites(self, limit = None):
i = 0
for link in self.soup.find_all('a'):
if limit != None and i > limit:
break
link = link.get('href')
if not link == None and not ("#" in link or "/" in link):
self.subsites.append(self.SubSite(urllib.parse.urljoin(self.baseurl, link)))
i += 1
def concatsubsites(self):
for subsite in self.subsites:
for tag in subsite.soup.body:
if (tag != "\n" and tag.name != "a"):
self.soup.body.append(tag)
def genpdf(self, path):
HTML(string=str(self.soup)).write_pdf(path)
def text2pdf(self, path, dry=False):
print("gettings subsites")
self.getsubsites()
print("concatting subsites")
self.concatsubsites()
if not dry:
print("generating pdf")
self.genpdf(path)