python3爬取网易云音乐歌单里的歌词(含源码)
一些注意事项:
1.看了Coursera上面的python教程,里面给了一个非常简单的示例,只传一个url就获取到响应报文。然而很多网站是不能只通过一个url就响应请求的,你还需要填写报文头部也就是header部分。
2.得到的报文里面中文字符都是bytes,十六进制的格式,需要用utf-8解码
3.巧用控制台找到页面内容真实的网址
其余的注意点我都写在注释里了,完整源码如下:
#爬取网易云音乐我的歌单里面所有歌曲的歌词
import json
import requests
import re
import urllib
from bs4 import *
url = "http://music.163.com/playlist?id=129816983"
headers = {"Host":" music.163.com",
"User-Agent":" Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0",
#不必要的header属性可能会影响响应报文的编码方式,所以把它们注释掉
#"Accept":" text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
#"Accept-Language":" zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
#"Referer":"http://music.163.com/",
#"Cookie": "JSESSIONID-WYYY=k52%2FPjMyNbX0v38jH2efUXwEIZpw2NagEUzwTX%2FgifMsoMswU6yo3NN%5C%2Bb9jCpsRFZIc6lvPUK9wEjgBzwM%2B1T%2FRyvRGHhqyWbdvEcugCbNqTihfxHK1el66fk%2BNntcSwGVOBMEwlcFDBusingcH76NIeAQwbC6h%5CcipxCdO8T5IfBVO%3A1510825875526; _iuqxldmzr_=32; _ntes_nnid=e5ec3ba6b841b9d3eadcb910066f4dcb,1510815153893; _ntes_nuid=e5ec3ba6b841b9d3eadcb910066f4dcb; __utma=94650624.1386008069.1510815154.1510815154.1510824076.2; __utmz=94650624.1510815154.1.1.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; __utmb=94650624.2.10.1510824076; __utmc=94650624",
#"Connection": "keep-alive",
#"Upgrade-Insecure-Requests": "1"
}
#只传url不能获得响应,需要传header
request = urllib.request.Request(url,headers=headers)
response = urllib.request.urlopen(request)
#不decode的话text是十六进制,不是中文
html = response.read().decode('utf-8','ignore')
soup = BeautifulSoup(html)
#打开1.txt 把歌单中的歌词写入
f=open('C:/Users/liuxu/Desktop/myfavoritesong.txt','w',encoding='utf-8')
for item in soup.ul.children:
#取出歌单里歌曲的id 形式为:/song?id=11111111
song_id = item('a')[0].get("href",None)
#利用正则表达式提取出song_id的数字部分sid
pat = re.compile(r'[0-9].*$')
sid = re.findall(pat,song_id)[0]
#这里的url是真实的歌词页面
url = "http://music.163.com/api/song/lyric?"+"id="+str(sid)+"&lv=1&kv=1&tv=-1"
html = requests.post(url)
json_obj = html.text
#歌词是一个json对象 解析它
j = json.loads(json_obj)
try:
lyric = j['lrc']['lyric']
except KeyError:
lyric = "无歌词"
pat = re.compile(r'\[.*\]')
lrc = re.sub(pat,"",lyric)
lrc = lrc.strip()
#print(lrc)
f.write(lrc)
f.close()