我怎样才能正确地循环一个范围,而不是让它重复结果呢?

0 人关注

我怎样才能使这个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
    
python
selenium
for-loop
selenium-webdriver
range
Houston Khanyile
Houston Khanyile
发布于 2021-10-29
2 个回答
Paul M.
Paul M.
发布于 2021-10-29
已采纳
0 人赞同

for-loop的范围是好的。问题是,在循环的每一次迭代中,你都向 song_list 追加一个新的项目,而这个项目是在循环的范围之外的。把 song_list = [] 移到循环中,使打印语句以你想要的方式工作。

然而,当循环结束时,你就不会再跟踪所有的歌曲了。你可能根本就不想在循环内打印。在循环外打印一次。

这个方法可行,但如果我在循环外打印,就不会追加到列表中。
对不起,我没说清楚--在这种情况下,你要在循环内追加(像你刚才那样),但只在循环外打印一次。
成功了,谢谢。
Pierre
Pierre
发布于 2021-10-29
0 人赞同

你应该把数据帧分配移到for循环之外。

for _ in range(20):