我運行此代碼嘗試使用 selenium 抓取動態網站。而不是按照我的代碼的指示運行 for 回圈,并在共享相同類名的其他元素中為我提供更多資料。它只重復第一個元素的資料。
代碼
import time
from selenium import webdriver
from selenium.webdriver.chrome import service
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd
ser= Service("C:\Program Files (x86)\chromedriver.exe")
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(options=options,service=ser)
driver.get('https://soundcloud.com/jujubucks')
print(driver.title)
song_contents = driver.find_elements(By.CLASS_NAME, 'soundList__item')
song_list = []
for song in song_contents:
search = driver.find_element(By.CLASS_NAME, 'soundTitle__usernameText').text
search_song = driver.find_element(By.XPATH, '//span[@]').text
search_date = driver.find_element(By.CLASS_NAME, 'sc-visuallyhidden').text
search_plays = driver.find_element(By.XPATH, '//*[@id="content"]/div/div[4]/div[1]/div/div[2]/div/div[2]/ul/li[1]/div/div/div/div[2]/div[4]/div[2]/div/ul/li/span/span[2]').text
song ={
'Artist': search,
'Song_title': search_song,
'Date': search_date,
'Streams': search_plays
}
song_list.append(song)
df = pd.DataFrame(song_list)
print(df)
driver.quit()
這是它給出的輸出。只有一組資料,而不是移動到其他組
輸出
Stream Juju Bucks music | Listen to songs, albums, playlists for free on SoundCloud
Artist Song_title Date Streams
0 Juju Bucks Squad Too Deep Ft. Cool Prince (Outro) Posted 1 year ago 31
1 Juju Bucks Squad Too Deep Ft. Cool Prince (Outro) Posted 1 year ago 31
2 Juju Bucks Squad Too Deep Ft. Cool Prince (Outro) Posted 1 year ago 31
3 Juju Bucks Squad Too Deep Ft. Cool Prince (Outro) Posted 1 year ago 31
4 Juju Bucks Squad Too Deep Ft. Cool Prince (Outro) Posted 1 year ago 31
uj5u.com熱心網友回復:
要在元素中查找元素,請在 xpath 中使用一個點,如下所示:
driver.get("https://soundcloud.com/jujubucks")
wait = WebDriverWait(driver,30)
# Close Cookie pop-up
wait.until(EC.element_to_be_clickable((By.ID,"onetrust-accept-btn-handler"))).click()
song_contents = driver.find_elements(By.CLASS_NAME, 'soundList__item')
for option in song_contents:
title = option.find_element_by_xpath(".//a[contains(@class,'soundTitle__title')]/span").text # Extract title from that particular song.
print(title)
Squad Too Deep Ft. Cool Prince (Outro)
Tropikana ft. P-Dogg Amazing
Party Ka Mngani Ft. X-Poll
Joy Ft. Black Sushi & Gavin Bowden
Amazing ft. X-Poll
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/339595.html
下一篇:無標簽元素的xpath
