import requests
import io,os
from bs4 import BeautifulSoup
import json

json_data = []
jdata = {}

URL = "https://www.tutorialspoint.com/tutorialslibrary.htm"
r = requests.get(URL)
 
soup = BeautifulSoup(r.content, 'html5lib') # If this line causes an error, run 'pip install html5lib' 

toc = soup.find_all("div", class_="featured-box")

for toc_elements in toc:
    li_elements = toc_elements.find_all("a",href=True)   
    for a in li_elements:
        title = a.text
        title = title.replace(" - Home", "")
        if "Learn" in title:
            title = title.replace("Learn ", "")
            title = title + " Tutorial"
      
        ahref = a['href']
        url = "https://www.tutorialspoint.com"+ahref
        obj = {
               'name' : title,
               'url'  : url
            }
        json_data.append(obj)  

jdata['data'] = json_data
json_object = json.dumps(jdata, indent = 4)
with open("tp_library_titles.json", "w") as outfile:
   outfile.write(json_object)