""" 爬取网易云歌单里面音乐列表 """
import re
import xlwt
import xlrd
import xlutils.copy
with open('3.html', 'rb') as f:
html = f.read().decode('utf-8')
pattern = r'<tr.+?</tr>'
res_list = re.findall(pattern=pattern, string=html, flags=re.S)
print(len(res_list))
name_author_list = []
name_author_pattern = r'data-res-name="(.+?)".+?data-res-author="(.+?)"'
for item in res_list:
name_author_list.extend(re.findall(pattern=name_author_pattern, string=item, flags=re.S))
print(name_author_list[0])
data = xlrd.open_workbook('歌单.xls')
ws = xlutils.copy.copy(data)
sheet = ws.add_sheet('sheet3', cell_overwrite_ok=True)
sheet.write(0, 0, '歌名')
sheet.write(0, 1, '作者')
for index, item in enumerate(name_author_list):
sheet.write(index + 1, 0, name_author_list[index][0])
sheet.write(index + 1, 1, name_author_list[index][1])
ws.save(r'歌单.xls')