python – Not getting an answer from an URL request-ThrowExceptions

Exception or error:

I downloaded a bunch of songs from Newgrounds.com. After downloading them I noticed that instead of the song name, the file’s name was the song ID. Because I downloaded around 200 songs I decided to make a program to rename all of them instead of going one by one. Here’s the code:

import urllib.request
import urllib.parse
import re
import os

path = os.listdir('C:\\Users\\axeld\\Desktop\\Music\\NG  Trial')

for item in path:
    url = f'https://www.newgrounds.com/audio/listen/{item.replace(".mp3", "")}'

    values = {'s': 'basics',
              'submit': 'search'}
    data = urllib.parse.urlencode(values)
    data = data.encode('utf-8')
    req = urllib.request.Request(url, data)
    resp = urllib.request.urlopen(req)
    respData = resp.read()

    text = re.findall(r'<h2 itemprop="name"(.*?)</h2>', str(respData))
    name = str(text)

    n1 = name.replace('[', '')
    n2 = n1.replace(']', '')
    n3 = n2.replace('<', '')
    n4 = n3.replace('>', '')
    Song_name = n4.replace("'", '')

    values = {'s': 'basics',
              'submit': 'search'}
    Author_data = urllib.parse.urlencode(values)
    Author_data = Author_data.encode('utf-8')
    request = urllib.request.Request(url, data)
    response = urllib.request.urlopen(req)
    responseData = response.read()

    text = re.findall(r'<a herf="//(.*?).newgrounds.com>(.*?)</a>"', str(respData))
    author = str(text)

    print(Song_name)
    print(author)

It works halfway but I have a problem. Here’s the output:

Forsaken Neon
[]
The Prototype
[]
Guitar Vs. Piano 1.2
[]
Surface
[]

As you can see I get the song name just fine. But when trying to get the author’s name I just received “[]” as an answer. Why is this and how can I fix it?

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *