爬取QQ音乐流行指数榜
warning:
这篇文章距离上次修改已过1009天,其中的内容可能已经有所变动。
import requests
import re
def Get_HTML(url):
headers={'User-Agent':'Mozilla/5.0'}
try:
r=requests.get(url,headers=headers)
r.raise_for_status()
r.encoding=r.apparent_encoding
return r.text
except:
print("error!")
def ParsePage(html):
pattern=re.compile('<div class="songlist__songname">.?<a title="(.?)" href.?>.?</a>.?<div class="songlist__artist">.?<a class.?>(.?)</a>',re.S)
items=re.findall(pattern,html)
return items
def Print(items):
name=[]
tplt="{0:^10}{1:^25}{2:^25}"
print("{0:^10}{1:^25}{2:^18}".format("排名","歌名","歌手",chr(12288)))
for i in range(20):
print(tplt.format(i+1,itemsi,itemsi),chr(12288))
def main():
url='https://y.qq.com/n/ryqq/toplist/4';
html=Get_HTML(url)
items=ParsePage(html)
Print(items)
main()