-3

Every week I have to gather a bunch of links and put them in a .txt file so I want to make a script that will go to each website and scrape the links for me. One of these websites is Spotify, I need to grab the share link of the latest episode of a podcast that is on Spotify. Does anyone know how I could do that ?

I already tried writing a script that ended up not working, I inspected the Spotify website source code and found the hyperlink to the share button, it is the one from the latest episode but in a week it won't be. So how can I make sure that every week it will grab the newest one ?

Here's the script:

import requests
from bs4 import BeautifulSoup

links_list = []

url = 'https://open.spotify.com/episode/5KFfAfI3udBlTYxmZF4YUe?si=74aee969910d4577&nd=1'
response = requests.get(url)

if response.ok:

    soup = BeautifulSoup(response.text, 'lxml')
    links = soup.findAll('link')

    for link in links:

        a = links.find('href')
        result = a['href']
        links_list.append(link)

print(len(links_list))

And here's the error I'm getting:

Traceback (most recent call last):
  File "test_url.py", line 18, in <module>
    a = links.find('href')
  File "/Users/theo.wizman/Library/Python/3.8/lib/python/site-packages/bs4/element.py", line 2253, in __getattr__
    raise AttributeError(
AttributeError: ResultSet object has no attribute 'find'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?

1 Answers1

0

AS @SergeyK mentioned, Spotify has public APIs to get a list of podcast episodes: https://developer.spotify.com/console/get-show-episodes/

In Python, you can use Spotipy for easy integration with the APIs:

import spotipy
from spotipy.oauth2 import SpotifyOAuth

scope = "user-read-playback-position"

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope))
episodes = sp.show_episodes("2F1OEswwpsZ60DDQTucPWe",market="US")

This snippet should retrieve the latest 50 episodes for the "On The Ledger" podcast.

rob_med
  • 496
  • 3
  • 7