#!/usr/bin/python3

# This Python 3 file takes the URL for a tracklist from the website
# www.hikarinoakariost.info, scrapes the tracklist (removing leading/trailing
# whitespace and leading numbering), and prints the tracklist to the console
# with each track separated by a newline.
#
# Usage: ./url_scrape_div.py URL_TO_TRACKLIST

import lxml.html
import requests
import sys

def validate_url(url):
    """Ensure the URL is non-empty and uses the HTTP protocol.
    """

    if not url:
        raise SystemError("validate_url() was given an empty URL")

    protocol = "http://"
    protocol_error_message = ValueError("A URL beginning with " \
        "'http://' is required")

    if len(url) < len(protocol):
        raise protocol_error_message

    if url[:len(protocol)] != protocol:
        raise protocol_error_message

def scrape_div(url, div_id):
    """Return the content of the div at the given URL.
    """

    div_id_lookup_string = '//div[contains(@id, "' + div_id + '")]'

    try:
        html_page = requests.get(url)
    except:
        e = sys.exc_info()[0]
        raise ValueError("Request could not be completed. Perhaps the " \
            "URL provided was invalid?")

    html_page.raise_for_status()

    html_tree = lxml.html.fromstring(html_page.content)
    content = html_tree.xpath(div_id_lookup_string)

    if len(content) < 1:
        raise LookupError("The requested div could not be found")
    elif len(content) > 1:
        raise LookupError("More than one of the requested divs were found")

    return str(content[0].text_content())

def extract_tracklist_begin_num(content):
    """Return list of track names extracted from messy web content.

    The name of a track is defined as a line which begins with a number
    (excluding whitespace).
    """

    tracklist = []
    for line in content.splitlines():

        # Empty line
        if not line:
            continue

        # Strip leading and trailing whitespace
        line.lstrip()
        line.rstrip()

        if line[0].isdigit():
            tracklist.append(line)

    return tracklist

def strip_leading_index(tracklist):
    """Remove the leading numbers/whitespace for each track.
    """

    tracklist_new = []
    for track in tracklist:

        for i in range(len(track)):
            if track[i].isdigit() or track[i] == " ":
                i += 1
            else:
                tracklist_new.append(track[i:])
                tracklist[tracklist.index(track)] = track[i:]
                break

if len(sys.argv) < 2:
    raise RuntimeError("Please provide the URL to the page with "\
        "the target tracklist")

url = sys.argv[1]  # sys.argv[0] is the name of this script
validate_url(url)

div_id = "stcpDiv"
content = scrape_div(url, div_id)

tracklist = extract_tracklist_begin_num(content)
strip_leading_index(tracklist)

for track in tracklist:
    print(track)

@@ -2197,39 +2197,122 @@
 ck.%0A
+%0A
     
-%22%22%22%0A%0A    tracklist_new = %5B%5D
+A consecutive sequence of digits are removed, then a consecutive sequence%0A    of spaces are removed.%0A    %22%22%22%0A
 %0A   
@@ -2349,35 +2349,13 @@
     
-for i in range(len(track)):
+i = 0
 %0A   
@@ -2359,23 +2359,22 @@
         
-    if 
+while(
 track%5Bi%5D
@@ -2387,33 +2387,11 @@
 it()
- or track%5Bi%5D == %22 %22:%0A    
+):%0A
     
@@ -2417,65 +2417,31 @@
     
-    else:%0A                tracklist_new.append(track%5Bi:%5D)
+while(track%5Bi%5D == %22 %22):
 %0A   
@@ -2437,32 +2437,44 @@
 = %22 %22):%0A        
+    i += 1%0A%0A
         tracklis
@@ -2514,30 +2514,8 @@
 %5Bi:%5D
-%0A                break
 %0A%0Aif

Change strip-leading-index to not remove title