You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
1.0 KiB
36 lines
1.0 KiB
import requests
|
|
from bs4 import BeautifulSoup
|
|
|
|
headers = {
|
|
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
|
'(KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36'
|
|
}
|
|
|
|
|
|
def get_data(url_):
|
|
data = requests.get(url=url_, headers=headers)
|
|
soup = BeautifulSoup(data.text, "html.parser")
|
|
ranks = soup.select('span.pc_temp_num') # 排名
|
|
titles = soup.select('div.pc_temp_songlist > ul > li > a') # 歌手名和歌曲名
|
|
ls = []
|
|
for rank, title in zip(ranks, titles):
|
|
rank = rank.get_text().strip()
|
|
song = title.get_text().split('-')[::-1]
|
|
data = {'rank': rank, 'song': '-'.join(song)}
|
|
ls.append(data)
|
|
return ls
|
|
|
|
|
|
def main(url_):
|
|
ls = []
|
|
urls_ = [url_.format(str(i)) for i in range(1, 6)]
|
|
for u in urls_:
|
|
ls.extend(get_data(u))
|
|
return ls
|
|
|
|
|
|
if __name__ == '__main__':
|
|
urls = ['http://www.kugou.com/yy/rank/home/{}-31308.html'.format(str(i)) for i in range(1, 20)]
|
|
for url in urls:
|
|
print(get_data(url))
|