collinsanele

Kumar

Aug 31st, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. import os
  2. from bs4 import BeautifulSoup
  3. import json
  4.  
  5.  
  6. def read_html(path):
  7. tank = []
  8. c=0
  9. indexes = []
  10. htmls = []
  11. files = os.listdir(path)
  12. for file in files:
  13. if file.endswith("html") or file.endswith("htm"):
  14. htmls.append(file)
  15.  
  16. with open(htmls[0]) as f:
  17. container = f.read()
  18.  
  19.  
  20. soup = BeautifulSoup(container, "html.parser")
  21. paras = soup.findAll("p", {"align":"JUSTIFY"})
  22.  
  23. content = str(paras)
  24. target = content.split("\n")
  25.  
  26. for i, item in enumerate(target):
  27. if "Sol" in item:
  28. indexes.append(int(i))
  29.  
  30. for i, item in enumerate(indexes):
  31. current = i
  32. next = i+1
  33. try:
  34. tank.append(target[indexes[current]:indexes[next]])
  35. except IndexError:
  36. pass
  37.  
  38. tank = [str(item).replace('p align="JUSTIFY"', "br").replace(",", " ").replace('<p align="CENTER"', "").replace("[", "").replace("]", "").replace("</p>", "") for item in tank]
  39.  
  40. print(tank)
Advertisement
Add Comment
Please, Sign In to add comment