练习1-爬取歌曲列表

任务:通过两个案例,练习使用Selenium操作网页、爬取数据。
使用无头模式,爬取网易云的内容。

'''
任务:通过两个案例,练习使用Selenium操作网页、爬取数据。
使用无头模式,爬取网易云的内容。
'''
from selenium import webdriver # 无头模式:隐身地启动浏览器,但是并没有窗口展现
from selenium.webdriver.chrome.options import Options opts = Options()
opts.add_argument('--headless')
opts.add_argument('--disable-gpu') bw = webdriver.Chrome(options=opts); # bw = webdriver.Chrome();
url = 'https://music.163.com/#/discover/toplist?id=3779629'
bw.get(url); bw.switch_to.frame('g_iframe') # 如果页面中有iframe,说明有内嵌页面
# 要爬取元素时,先切换到对应的内嵌页面中,然后再爬 ss = bw.find_elements_by_css_selector('.m-table-rank tbody tr .txt a b');
print(len(ss)) # 100 authors = bw.find_elements_by_css_selector('.m-table-rank tbody tr .text');
print(len(authors)) # 100 for i, s in enumerate(ss):
print(s.get_attribute('title'), ':', authors[i].get_attribute('title')); bw.close();

练习2-爬取歌曲文件mp3

网易云:能不能爬取音乐???可以!能不能爬歌词???可以!

网易云音乐,歌曲通用下载地址:http://music.163.com/song/media/outer/url?id= [ id后面拼接歌曲编号 ]

 

'''
尝试下载,requests访问,得到二进制数据,保存到本地即可
爬取网易云音乐的歌曲mp3文件(单个歌曲下载)
《初恋》歌曲id: 1873049720
《清醒》歌曲id:1909660296
《星辰大海》歌曲id:1811921555
'''
import requests as req # hds:伪装成浏览器
hds = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'} common_url = 'http://music.163.com/song/media/outer/url?id={}'; # 通用下载路径 resp = req.get(common_url.format('1909660296'), headers=hds); ct = resp.content; # 响应内容
print(len(ct)) # 响应内容长度
print(resp.status_code); # 200正常;302重定向,需要继续获取重定向后的路径 # print(resp.headers)
# u2 = resp.headers['Location'];
# print(u2) # 继续爬取u2路径,来下载音乐 if resp.status_code == 200:
with open(r'C:\Users\lwx\Desktop\网易云\清醒.mp3', 'wb') as f: # as f取别名,简写
f.write(ct);
# 上述两行代码(简写),在效果上等于下面三行代码。
# f = open(r'C:\Users\lwx\Desktop\网易云\清醒.mp3', 'wb')
# f.write(ct)
# f.close()
print('over!')

练习3-下载飙升榜中的歌曲

结合上午的代码和刚才下载音乐的办法,请尝试:将飙升榜中的前20首歌曲下载(尝试下载)。
https://music.163.com/#/discover/toplist   15分钟时间

import requests as req
from selenium import webdriver
from selenium.webdriver.chrome.options import Options hds = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'} def wydown(songname, songid):
common_url = 'http://music.163.com/song/media/outer/url?id={}';
resp = req.get(common_url.format(songid), headers=hds);
ct = resp.content;
# print(len(ct))
# print(resp.status_code); #200正常 302重定向,需要继续获取重定向后的路径
if resp.status_code == 200:
f = open(r'C:\Users\qx\Desktop\网易云\{}.mp3'.format(songname), 'wb')
f.write(ct);
f.close();
print('已下载:', songname); # 无头模式 : 隐身的启动浏览器,但是并没有窗口展现
opts = Options()
opts.add_argument('--headless')
opts.add_argument('--disable-gpu') bw = webdriver.Chrome(options=opts); url = 'https://music.163.com/#/discover/toplist'
bw.get(url);
bw.switch_to.frame('g_iframe'); ss = bw.find_elements_by_css_selector('.m-table-rank tbody tr .txt a b');
ids = bw.find_elements_by_css_selector('.m-table-rank tbody tr .txt a'); songinfo = {}; # 歌曲名:歌曲id
for i, s in enumerate(ss):
songinfo[s.get_attribute('title')] = ids[i].get_attribute('href').split("=")[1]; bw.close(); # print(songinfo); # 遍历字典,下载所有歌曲
for k, v in songinfo.items():
wydown(k, v);

Python实训day07pm【Selenium操作网页、爬取数据-下载歌曲】的更多相关文章

  1. Python-异常处理 使用selenium库自动爬取数据

    异常处理 处理程序的报错 语法 捕捉万能异常: try: print(a) except Exception as e: print("你的代码有问题") print(" ...

  2. [Python爬虫] 之三:Selenium 调用IEDriverServer 抓取数据

    接着上一遍,在用Selenium+phantomjs 抓取数据过程中发现,有时候抓取不到,所以又测试了用Selenium+浏览器驱动的方式:具体代码如下: #coding=utf-8import os ...

  3. Python爬虫学习——使用selenium和phantomjs爬取js动态加载的网页

    1.安装selenium pip install selenium Collecting selenium Downloading selenium-3.4.1-py2.py3-none-any.wh ...

  4. python 网页爬取数据生成文字云图

    1. 需要的三个包: from wordcloud import WordCloud #词云库 import matplotlib.pyplot as plt #数学绘图库 import jieba; ...

  5. 动态网页爬取例子(WebCollector+selenium+phantomjs)

    目标:动态网页爬取 说明:这里的动态网页指几种可能:1)需要用户交互,如常见的登录操作:2)网页通过JS / AJAX动态生成,如一个html里有<div id="test" ...

  6. Python使用urllib,urllib3,requests库+beautifulsoup爬取网页

    Python使用urllib/urllib3/requests库+beautifulsoup爬取网页 urllib urllib3 requests 笔者在爬取时遇到的问题 1.结果不全 2.'抓取失 ...

  7. Python和BeautifulSoup进行网页爬取

    在大数据.人工智能时代,我们通常需要从网站中收集我们所需的数据,网络信息的爬取技术已经成为多个行业所需的技能之一.而Python则是目前数据科学项目中最常用的编程语言之一.使用Python与Beaut ...

  8. [Python爬虫] 使用 Beautiful Soup 4 快速爬取所需的网页信息

    [Python爬虫] 使用 Beautiful Soup 4 快速爬取所需的网页信息 2018-07-21 23:53:02 larger5 阅读数 4123更多 分类专栏: 网络爬虫   版权声明: ...

  9. 利用selenium和ffmpeg爬取m3u8 ts视频《进击的巨人》

    需求 想看下动漫<进击的巨人>,发现到处被和谐,找不到资源,但是在一个视频网站找到了在线播放,https://www.55cc.cc/dongman/17890/player-2-1.ht ...

随机推荐

  1. CF748A Santa Claus and a Place in a Class 题解

    Content 圣诞老人坐在一个桌子被摆成 \(m\) 行 \(n\) 列的教室里.每个桌子有两个座位,每个位置从左往右都有编号,依次为 \(1,2,3,...,2\times n\times m\) ...

  2. CF1547B Alphabetical Strings 题解

    Content 我们有一个空的字符串,第 \(i\) 次操作我们可以将字母表中第 \(i\) 个字母加入字符串的最前面或最后面.我们称一个长度为 \(n\) 的字符串是合法的,当且仅当这个字符串可以通 ...

  3. 卸载zabbix

    1.首先停止zabbix运行 可以用官方命令 systemctl stop zabbix-server zabbix-agent httpd rh-php72-php-fpm 也可以直接kill -9 ...

  4. 在JSP页面里,时间控件的JS位置在下面然后就显示不出来

    在JSP页面里,时间空间的JS位置在下面然后就显示不出来,放到前面然后就显示出来了, 情何以堪啊,开始还以为是什么错误.

  5. uniapp+nvue实现仿微信App聊天应用 —— 成功实现好友聊天+语音视频通话功能

    基于uniapp + nvue实现的uniapp仿微信App聊天应用 txim 实例项目,实现了以下功能. 1: 聊天会话管理 2: 好友列表 3: 文字.语音.视频.表情.位置等聊天消息收发 4: ...

  6. [WPF] 实现 WPF 的 Inner Shadow

    在 WPF 中,我们通常用 DropShadow 做阴影效果,但都是做外阴影.内阴影(Inner Shadow)的话其实也不是不可以,就是有些曲折.这篇文章介绍几种做内引用的做法. 文章涉及到以下概念 ...

  7. table中tr、td标签设置只读,不能修改(readonly属性)

    在不能修改的位置加上代码:onselectstart="return false" οnselect="document.selection.empty()" ...

  8. 【LeetCode】231. Power of Two 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二进制 位运算 判断是不是最大2的幂的因数 判断因子 ...

  9. 【LeetCode】278. First Bad Version 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 二分查找 日期 题目地址:https://leetcode.c ...

  10. RXD and math

    RXD and math 题目链接 思路 \(u\)函数是莫比乌斯函数,这个不影响做题,这个式子算的是\([1,n^k]\)中能够写成\(a*b^2\)的数的个数,\(u(a)!=0\).然后我们可以 ...