Skip to content

Instantly share code, notes, and snippets.

@FelixChop
Created September 12, 2019 05:48
Show Gist options
  • Select an option

  • Save FelixChop/952d5988ba70102db9e3e140b3893d39 to your computer and use it in GitHub Desktop.

Select an option

Save FelixChop/952d5988ba70102db9e3e140b3893d39 to your computer and use it in GitHub Desktop.
import sys
from selenium import webdriver
import string
import time
import os
def main(letter1):
browser = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver')
browser.get(url="https://accounts.spotify.com/fr/login")
time.sleep(3)
browser.find_element_by_xpath("//input[@ng-model='form.username']").send_keys('your_email_here')
browser.find_element_by_xpath("//input[@ng-model='form.password']").send_keys('your_password_here')
browser.find_element_by_id("login-button").click()
time.sleep(3)
browser.get(url='https://open.spotify.com/search')
def scroll_down(browser):
search_content = browser.find_element_by_xpath('//div[@class="Search__content"]')
last_artist = search_content.find_elements_by_class_name('media-object')[-1]
browser.execute_script("arguments[0].scrollIntoView();", last_artist)
def get_attributes(info_artists):
artists = {}
for info in info_artists:
artists[info.get_attribute('href').replace('https://open.spotify.com/artist/','')] = info.get_attribute('title')
return artists
max_scroll = 100
for letter2 in string.ascii_lowercase:
print(letter1, letter2)
if os.path.exists('artists_'+letter1+letter2+'.txt'):
print(letter1, letter2, 'already exists')
continue
counter_scroll = 0
browser.get(url='https://open.spotify.com/search/'+letter1+letter2+'/artists')
time.sleep(1)
while counter_scroll < max_scroll:
print('\t'+str(counter_scroll))
scroll_down(browser)
time.sleep(1)
counter_scroll += 1
artists = get_attributes(
browser.find_element_by_xpath('//div[@class="Search__content"]')
.find_elements_by_xpath('//a[@class="mo-info-name"]')
)
with open('artists_'+letter1+letter2+'.txt', 'a') as f:
f.write(str(artists))
if __name__ == '__main__':
main(sys.argv[1])
@Danielsesva

Copy link
Copy Markdown

Great work! I see that the Spotify scheme has changed. Is it possible to update the links in your code? I cannot find the search page you scrape.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment