首先导入的库当然是selenium,为了设置超时,需要导入time

from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys

导入之后可以打开网易主页

driver = webdriver.Chrome()
driver.get(u"http://music.163.com/")
print "正在打开网页",driver.title

等待页面加载,选择爬陈粒的网易歌词第一页

time.sleep(2)
name = u"陈粒"
driver.find_element_by_css_selector("input[class=\"txt j-flag\"]").send_keys(name)
driver.find_element_by_css_selector("input[class=\"txt j-flag\"]").send_keys(Keys.ENTER)

转到名称为contengframe的框架上来

driver.switch_to_frame("contentFrame")

爬前三十首歌,定义一下fun函数

def fun(a):print "正在打开歌曲页面"time.sleep(2)try:driver.find_element_by_css_selector("a[href^='javascript:void(0)']").click()except:   js="var q=document.body.scrollTop=500"driver.execute_script(js)time.sleep(1)driver.find_element_by_css_selector("a[href^='javascript:void(0)']").click()else:driver.implicitly_wait(10) # secondsdriver.execute_script('window.stop()')print "正在获取歌词"text = driver.find_element_by_id("lyric-content").texttext_all = text[:-2]name = driver.find_element_by_class_name("tit").textprint "正在新建文件保存"f=open('C:/Users/user/Desktop/lyric/%s.txt'%name.replace("\n", "@@").split("@@")[0],'w')f.write(text_all.encode('utf8'))print "正在保存歌词",namef.close()driver.back()driver.switch_to_frame("contentFrame")time.sleep(2)

此函数为打开歌曲页面之后的操作,然而在打开之前会有很多其他的操作

首先确认该歌曲歌手是不是我们搜索的名字,试图点击是在界面刷新之后的界面,当歌曲链接不在此界面上,则下拉,另外还会出现网易的登录界面如下,此时必须回到主窗口关闭掉再回到刚才的contentframe窗口继续爬取,具体代码如下,另外中间可能会有一些错误出现,可能是由于页面加载不及时等原因,此时重新运行即可

for i in range(0,30):a=i+1print "第",a,"首"if name in driver.find_element_by_css_selector('div#m-search>div:nth-child(3)>div>div>div:nth-child(%d)>div:nth-child(4)'%a).text:try:print "试图点击"driver.find_element_by_css_selector('div#m-search>div:nth-child(3)>div>div>div:nth-child(%d)>div:nth-child(2)>div>div>a'%a).click()fun(a)except:try:print "试图下拉点击"js="var q=document.body.scrollTop=600"driver.execute_script(js)time.sleep(1)driver.find_element_by_css_selector('div#m-search>div:nth-child(3)>div>div>div:nth-child(%d)>div:nth-child(2)>div>div>a'%a).click()fun(a)except:try:print "试图二次下拉点击"js="var q=document.body.scrollTop=1000"driver.execute_script(js)time.sleep(1)driver.find_element_by_css_selector('div#m-search>div:nth-child(3)>div>div>div:nth-child(%d)>div:nth-child(2)>div>div>a'%a).click()fun(a)except:print "试图回到主页面关闭窗口"driver.switch_to.default_content()driver.find_element_by_class_name('zcls').click()driver.switch_to_frame("contentFrame")js="var q=document.body.scrollTop=1000"driver.execute_script(js)time.sleep(1)driver.find_element_by_css_selector('div#m-search>div:nth-child(3)>div>div>div:nth-child(%d)>div:nth-child(2)>div>div>a'%a).click()fun(a)                   else:pass

python爬网易歌词相关推荐

  1. python爬取歌词_Python如何爬取qq音乐歌词到本地

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 闲来无事听听歌,听到无聊唠唠嗑,你有没有特别喜欢的音乐,你有没有思考或者尝 ...

  2. python爬取歌词生成词云图_爬取毛不易歌词作词云展示

    爬取毛不易歌词作词云展示 今天我们做一个数据可视化的项目,爬取毛不易的歌词做词云展示. 1.爬取数据 我们主要使用 Python 爬虫获取 HTML,用 XPath 对歌曲的 ID.名称进行解析,然后 ...

  3. python爬网易新闻_Python爬虫实战教程:爬取网易新闻

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: Amauri PS:如有需要Python学习资料的小伙伴可以加点击 ...

  4. python爬网易新闻_Python爬虫实战教程:爬取网易新闻;爬虫精选 高手技巧

    Python爬虫实战教程:爬取网易新闻:爬虫精选 高手技巧 发布时间:2020-02-21 17:42:43 前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有, ...

  5. python爬取歌词_利用Python网络爬虫抓取网易云音乐歌词

    今天小编给大家分享网易云音乐歌词爬取方法. 本文的总体思路如下: 找到正确的URL,获取源码: 利用bs4解析源码,获取歌曲名和歌曲ID: 调用网易云歌曲API,获取歌词: 将歌词写入文件,并存入本地 ...

  6. python爬网易歌单_Python爬取网易云歌单

    1. 关键点 使用单线程爬取,未登录,爬取网易云歌单主要有三个关键点: url为https://music.163.com/discover/playlist/?order=hot&cat=% ...

  7. python爬取歌词并生成词云图

    第一部分:爬取数据 # 第一部分:爬取数据 import requests import re import os import json from bs4 import BeautifulSoup# ...

  8. python爬网易新闻_爬虫基本介绍 python3 爬虫爬取网易新闻排行榜

    爬虫基本介绍 1. 什么是爬虫? 爬虫是请求⽹网站并提取数据的⾃自动化程序 2. 爬虫的基本流程 发起请求 通过HTTP库向目标站点发起请求,即发送一个Request,请求可以包含额外的headers ...

  9. python爬歌词生成词云图_Python爬虫摇滚网易云音乐歌词生成词云图

    我相信经过前三篇文章,大家已经学会了怎么下载歌词和歌曲了.看了我的文章后开始行动起来的应该都享受到音乐的福利了.问一个问题,当你疲惫时,你想提起精神来去工作或者看书,你会选择听什么歌曲让自己兴奋起来呢 ...

最新文章

  1. 如何给VirtualBox虚拟机的ubuntu LVM分区扩容
  2. JEECG开源团队招募成员计划
  3. 空间平面,空间直线及它们的方程
  4. 【C/C++】知识点
  5. DATASNAP数据序列之FIREDAC的TFDJSONDataSets
  6. web使用js调用摄像头扫码、拍照、录像
  7. Linux下安装lingo教程-centos6 64位
  8. 用ntsd -c q -p PID 杀进程
  9. java定时器表达式_Java Quartz表达式每分钟执行1次
  10. AIX系统中 .toc文件是做什么用的
  11. Word怎么撤销到上一步操作
  12. webots controller API(C++)
  13. 一号店主页静态页面(HTML)
  14. 服务器winsxs文件夹怎么清理工具,win7如何使用WinSxS工具安全删除WinSxS文件夹垃圾...
  15. 售价占激光电视价格一半 抗光幕为何这么贵?
  16. DeferredShading
  17. [时间序列分析][1]--平稳性,白噪声的检验
  18. 怎么将几个QQ对话窗口合并和分离
  19. esp32,基于阿里云平台,配置实现Node.js SDK接入示例出现description: ‘Failed to authenticate: 4‘ BUG
  20. ESP8266物联网台灯

热门文章

  1. 最大网络流Ford-Fulkerson算法
  2. 2013年新年祝福视频奖表彰
  3. springboot新建webapp
  4. 【错误总结】expected specifier-qualifier-list before sth
  5. C语言:将一个文本文件按字符送上屏幕(动画效果)
  6. java cast是什么意思啊_Java Class.cast()与cast操作符
  7. Android开发框架Collection,Android社招面经分享
  8. 算法实验三 Problem F木乃伊迷宫
  9. 一致性hash算法虚拟节点_一致性hash和虚拟节点
  10. php 分批处理,PHP 遍历元素并分批处理实现代码