如何与Python在Spotify中刮擦共享链接?

发布于 2025-01-17 18:51:33 字数 1223 浏览 3 评论 0 原文

每周我都必须收集一堆链接并将它们放入 .txt 文件中,因此我想制作一个脚本来访问每个网站并为我抓取链接。其中一个网站是 Spotify,我需要获取 Spotify 上最新一集播客的共享链接。有谁知道我该怎么做?

我已经尝试编写一个脚本,但最终无法正常工作,我检查了 Spotify 网站源代码,找到了共享按钮的超链接,这是最新一集的链接,但一周后就不会了。那么我怎样才能确保每周它都会获取最新的呢?

这是脚本:

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))

这是我收到的错误:

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()?

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()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-小熊_ 2025-01-24 18:51:33

正如@sergeyk所提到的,Spotify拥有公共API,可以获取播客列表:

在Python中,您可以使用 Spotipy 以便于与API集成:

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")

此片段应检索“在分类帐”播客的最新50集。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文