# -*- coding: utf-8 -*-
#原博客:http://python.jobbole.com/89091/#article-comment。本人做了部分错误的修改,下面是在Anaconda的spyder下运行通过的代码,elenium2.48.0版本,Selenium最新版本不再支持PhantomJS
#动态网页爬虫
"""
Created on Tue Jan 8 08:46:03 2019
'''http://python.jobbole.com/89091/'''
@author: T
"""
from selenium import webdriver
import csv
#网易云音乐歌单第一页的url
#http://jr.jd.com
url = 'https://music.163.com/#/discover/playlist/?order=hot&cat=%E5%85%A8%E9%83%A8&limit=35&offset=0'
#用PhantomJs创建一个Selenium的WebDRriver
#driver = webdriver.PhantomJS(executable_path='D:\DELL Program Files\Anaconda-python3.6\Scripts\phantomjs.exe')
driver = webdriver.PhantomJS()
#准备好存储歌单的csv文件
csv_file = open("playlist.csv","w",newline='',encoding='utf-8-sig')
writer = csv.writer(csv_file)
writer.writerow(['标题','播放数','链接'])
#解析每一页,直到下一页为空
while url != 'javascript:void(0)':
#用webDriver加载页面
driver.get(url)
#切换到内容的iframe
driver.switch_to.frame('contentFrame')
#定位歌单标签
data = driver.find_element_by_id("m-pl-container").find_elements_by_tag_name("li")
#解析一页中的所有歌单
for i in range(len(data)):
#获取播放数
nb = data[i].find_element_by_class_name("nb").text
#print nb
if '万' in nb and int(nb.split("万")[0])>500:
#获取播放量大于500万的歌单的封面
msk = data[i].find_element_by_css_selector("a.msk")
#把封面上的标题和链接连同播放数一起写到文件中
writer.writerow([msk.get_attribute('title'),nb,msk.get_attribute('href')])
#定位下一页
url = driver.find_element_by_css_selector("a.zbtn.znxt").get_attribute('href')
csv_file.close()