无法使用Heroku上托管的电报Bot删除歌词

时间:2020-07-21 13:31:10

标签: python-3.x beautifulsoup telegram-bot

我最近在heroku上托管了一个电报机器人,并编写了一个函数,该函数可以使用漂亮的汤从musixmatch上获得歌手排名前5位的最受欢迎歌曲。最初,它能够,但现在却没有。我尝试在本地运行,但仍然可以返回结果。这些是我的代码:

${it.amounts[month]}

预期输出:

query = "hillsong"
   search_page = requests.get("https://www.musixmatch.com/search/{}/tracks".format(query),
                              headers={"User-Agent": "Mozilla/5.0"})
   soup = BeautifulSoup(search_page.content, 'html.parser')

   top_tracks = soup.find_all(class_="showArtist showCoverart", limit=5)
   output_top_tracks = "<b>Top search results on {}:</b>\n\n".format(query)
   for track in top_tracks:
       title = track.find("a", class_="title").get_text()
       artist = track.find("a", class_="artist").get_text()
       href = track.find("a", href=True)['href']
       # print(href)
       uuid = str(uuid4()).upper()[:4]
       output_top_tracks = output_top_tracks + "? " + title + "\n" + "? " + artist + "\n" + "/lyric3" + uuid + "\n\n"

错误:

? Faded
? Alan Walker
/lyric3A848

? Alone, Pt. II
? Alan Walker
/lyric38904

? All Falls Down
? Alan Walker
/lyric35DD4

? Sing Me to Sleep
? Alan Walker
/lyric3968D

? Lily
? Alan Walker
/lyric349BC

1 个答案:

答案 0 :(得分:0)

此脚本将打印页面上找到的前5首歌曲:

import requests
from bs4 import BeautifulSoup


url = 'https://www.musixmatch.com/search/hillsong/tracks'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0'}

soup = BeautifulSoup(requests.get(url, headers=headers).content, 'html.parser')

for t, s in list(zip(soup.select('.media-card-title'), soup.select('.media-card-subtitle')))[:5]:
    print("? " + t.text)
    print("? " + s.text)
    print(t.a['href'])
    print()

打印:

? Oceans - Where Feet May Fail
? Hillsong UNITED
/lyrics/Hillsong-UNITED/Oceans-Where-Feet-May-Fail

? Hosanna
? Hillsong UNITED
/lyrics/Hillsong-UNITED/Hosanna

? What A Beautiful Name
? Hillsong Worship
/lyrics/Hillsong-Worship/What-a-Beautiful-Name

? Awake My Soul (with Tasha Cobbs Leonard)
? Hillsong Worship, Tasha Cobbs Leonard
/lyrics/Hillsong-Worship-Tasha-Cobbs-Leonard/Awake-My-Soul-with-Tasha-Cobbs-Leonard

? Who You Say I Am
? Hillsong Worship
/lyrics/Hillsong-Worship/Who-You-Say-I-Am