我怎样才能使这个for循环不重复这个列表以前的输出,同时仍然使用一个范围。这个for循环正在重复前一个数字的输出。每次它都进入下一个数字。而不是从0-20一次。它进入0-1,0-2,0-3,0-4........ 等。我想让它从0-20开始一次,不要重复。
import time
from selenium import webdriver
import selenium
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
#class scraperdata():
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)
wait = WebDriverWait(driver,30)
wait.until(EC.element_to_be_clickable((By.ID,"onetrust-accept-btn-handler"))).click()
song_list = []
i = 1
for _ in range(20):
song_contents = driver.find_element(By.XPATH, "//li[@class='soundList__item'][{}]".format(i))
driver.execute_script("arguments[0].scrollIntoView(true);",song_contents)
search = song_contents.find_element(By.XPATH, ".//a[contains(@class,'soundTitle__username')]/span").text
search_song = song_contents.find_element(By.XPATH, ".//a[contains(@class,'soundTitle__title')]/span").text
search_date = song_contents.find_element(By.XPATH, ".//time[contains(@class,'relativeTime')]/span").text
search_plays = song_contents.find_element(By.XPATH, ".//span[contains(@class,'sc-ministats-small')]/span").text
if _ == Exception:
break
option ={
'Artist': search,
'Song_title': search_song,
'Date': search_date,
'Streams': search_plays
song_list.append(option)
df = pd.DataFrame(song_list)
print(df)
except Exception:
driver.quit()
Output
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 plays
Artist Song_title Date Streams
0 Juju Bucks Squad Too Deep Ft. Cool Prince (Outro) Posted 1 year ago 31 plays
1 Juju Bucks Tropikana ft. P-Dogg Amazing Posted 1 year ago 48 plays
Artist Song_title Date Streams
0 Juju Bucks Squad Too Deep Ft. Cool Prince (Outro) Posted 1 year ago 31 plays
1 Juju Bucks Tropikana ft. P-Dogg Amazing Posted 1 year ago 48 plays
2 Juju Bucks Party Ka Mngani Ft. X-Poll Posted 1 year ago 72 plays
Artist Song_title Date Streams
0 Juju Bucks Squad Too Deep Ft. Cool Prince (Outro) Posted 1 year ago 31 plays
1 Juju Bucks Tropikana ft. P-Dogg Amazing Posted 1 year ago 48 plays
2 Juju Bucks Party Ka Mngani Ft. X-Poll Posted 1 year ago 72 plays
3 Juju Bucks Joy Ft. Black Sushi & Gavin Bowden Posted 1 year ago 122 plays