1
import csv
from email import header
from fileinput import filename
from tokenize import Name
import requests
from bs4 import BeautifulSoup

url = "https://www.azlyrics.com/l/linkinpark.html"

r=requests.get(url)
htmlContent = r.content
soup = BeautifulSoup(htmlContent,"html.parser")

albumList=[]
table = soup.find('div', id="listAlbum")
for row in table.find_all('div', class_="album"):
    albumName = {}
    albumName['Name'] = row.b.text
    albumList.append(albumName)
    print(albumName)

noOfAlbum = len(albumList)
print(noOfAlbum)

with open('lpalbumr6.csv','w',encoding='utf8',newline='') as f:
    thewriter = csv.writer(f)
    header=['Name']
    thewriter.writerow(header)
    for i in albumList:
        thewriter.writerow(albumName)

Hello,

I was trying to get the list of album on artist page on azlyrics.com. When I export the list in csv, I am getting exported list as follows:

enter image description here

print(albumName) works perfectly, but exported csv looks like above image.

1 Answers1

1

albumList contains all the information you need, so the issue is just with the part where you write the csv at the end.

You have:

for i in albumList:
     thewriter.writerow(albumName)

but albumName is not referring to the elements of albumList - it's the temporary variable you used when creating that list. You need to refer to the loop variable i in the loop. You also need to specify that you need the value of the Name key in each dictionary:

for i in albumList:
    thewriter.writerow([i['Name']])

This is all inside an extra [] because of the way csvwriter handles strings (see this question)

With this change the generated csv looks like this:

enter image description here

ljdyer
  • 1,946
  • 1
  • 3
  • 11
  • Thanks brother. It works perfectly. Also, I did not know the string issue. The thread you shared is very insightful. – Inward Locus Oct 31 '22 at 06:10
  • @InwardLocus, it seems this answer solved your problem. Please accept it by clicking the check mark near the top left of the answer. Thank you :) – Zach Young Oct 31 '22 at 06:38