I tried to web scraping from this website https://circlechart.kr/page_chart/album.circle?serviceGbn=year&termGbn=year&hitYear=2012&targetTime=2012&nationGbn=T&year_time=3 to get the artist song, name, and the sales. But from the codes i wrote below, i do get the artist name and song, but the sales didn’t show up in my csv file. This file is important for me to complete my midterm. I hope anyone can help me
here is my codes
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import csv
url = 'https://circlechart.kr/page_chart/album.circle?nationGbn=T&targetTime=2012&hitYear=2012&termGbn=year&yearTime=3'
# Initialize WebDriver
driver = webdriver.Chrome()
driver.get(url)
# Wait until the table is loaded
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'ChartTablePC')))
# Locate the table
table = driver.find_element(By.CLASS_NAME, 'ChartTablePC')
rows = table.find_elements(By.TAG_NAME, 'tr')
# Print structure of each row to verify
for row in rows[:5]: # Print the first 5 rows for inspection
cells = row.find_elements(By.TAG_NAME, 'td')
print([cell.text for cell in cells])
# Define headers
HEADERS = ['Rank', 'Album Artist', 'Sales']
# Open CSV file for writing
with open('EXOalbumSales2012.csv', 'w', newline='', encoding='utf-8') as outfile:
writer = csv.writer(outfile)
writer.writerow(HEADERS)
# Iterate over rows
for row in rows[1:]: # Skip the header row
cells = row.find_elements(By.TAG_NAME, 'td')
if len(cells) >= 5: # Ensure there are enough columns
rank = cells[0].text.strip()
album_artist = cells[2].text.strip()
sales = cells[4].text.strip()
data_out = [rank, album_artist, sales]
writer.writerow(data_out)
print(data_out)
# Close the WebDriver
driver.quit()
the expected output should be artist name, song title and album sales inside csv file
josephine kwok is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.