frontpage.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/python3
  2. from bs4 import BeautifulSoup
  3. import requests
  4. class FrontPage():
  5. def getPage(self,url):
  6. r = requests.get(url)
  7. return r.content
  8. def getDomain(self,url):
  9. parts = url.split('//', 1)
  10. return parts[0]+'//'+parts[1].split('/', 1)[0]
  11. def fixHref(self,url,baseurl):
  12. if url.startswith("http"):
  13. return url.strip()
  14. if url.startswith("/"):
  15. domain=self.getDomain(baseurl)
  16. if domain.endswith("/") and url.startswith("/"):
  17. url=url[1:]
  18. url=domain+url
  19. return url.strip()
  20. def __init__(self,url,selector):
  21. soup = BeautifulSoup(self.getPage(url), 'html.parser')
  22. links=soup.select(selector)
  23. self.links=[]
  24. for l in links:
  25. if not l.has_attr("href"):
  26. print("PANIC")
  27. print(l)
  28. print(url)
  29. continue
  30. self.links.append({ "href": self.fixHref(l["href"],url),"title":l.get_text() })