Используя BeautifulSoup и Selenium в Python, я пытаюсь прокрутить список песен в плейлисте вниз, чтобы проанализировать названия песен. Однако код не пройдет дальше первых 30 песен и прокрутит дальше. Почему?
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
import csv
from bs4 import BeautifulSoup
# Initialize the WebDriver
driver = webdriver.Chrome()
# Open the website
driver.get("SPOTIFY PLAYLIST URL")
# Scroll to the bottom of the page using PAGE_DOWN key
body = driver.find_element(By.TAG_NAME, "body")
for _ in range(30): # Adjust the range as needed
body.send_keys(Keys.PAGE_DOWN)
time.sleep(1) # Adjust the sleep time as needed
# Parse the page source with BeautifulSoup
soup = BeautifulSoup(driver.page_source, 'html.parser')
# Find song elements
songs = soup.find_all("div", class_="btE2c3IKaOXZ4VNAb8WQ")
# Check if songs are found
if not songs:
print("No songs found. Please check the class name or the page structure.")
else:
print(f"Found {len(songs)} songs.")
# Write to CSV
with open('songs.csv', 'w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(['SONGS'])
for song in songs:
song_text = song.get_text(strip=True)
print(song_text)
writer.writerow([song_text])
# Close the WebDriver
driver.quit()
Подробнее здесь: https://stackoverflow.com/questions/792 ... song-names
Код не будет прокручивать плейлист вниз для анализа названий песен. ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Как получить доступ к списку песен в функции Pixels «Сейчас исполняется»?
Anonymous » » в форуме Android - 0 Ответы
- 14 Просмотры
-
Последнее сообщение Anonymous
-