0

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?

Ritesh Khandekar
  • 3,885
  • 3
  • 15
  • 30
Maypher
  • 121
  • 1
  • 2
  • 10
  • 3
    You might want to tag this with the correct language for more / better assistance. – Mech Feb 22 '20 at 03:08
  • 1
    `a herf`? Check your regex. And a mandatory "don't parse HTML with a regex" admonishment. Consider using XPath to search HTML instead. https://stackoverflow.com/questions/8692/how-to-use-xpath-in-python#27974 – Schwern Feb 22 '20 at 05:17

0 Answers0