import csv
import requests
import time
from bs4 import BeautifulSoup
from random import randint

# Author: Md Md Farid

# Paste a webpage URL here.
raw = input("Try copying pasting a link here: ")
time.sleep(randint(2, 6))

# Get HTML content.
r = requests.get(raw)

# Parse entire HTML content.
soup = BeautifulSoup(r.content, 'html.parser')

# Search for book categories and extract them.
s = soup.find('div', class_='side_categories')
content = s.find_all('a')

# Sample counter
counter = 0
header = []
categories = []
filename = 'sampleCategories.csv'

# Separate header from book categories.
for a in content:
    instanceCat = a.text
    if counter == 0:
        stripped = instanceCat.strip()
        header.append(stripped)
    else:
        stripped2 = instanceCat.strip()
        categories.append(stripped2)

    counter += 1

# Structure book categories into a useful format.
with open(filename, "w", newline='') as csvfile:
    fieldnames = ["Categories"]
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()
    for b in categories:
        writer.writerow({'Categories': b})
