selenium和phantomjs,完成豆瓣音乐排行榜的内容爬取
代码要多敲 注释要清晰 哪怕再简单
#使用selenium和phantomjs,完成豆瓣音乐排行榜的内容爬取
#地址:https://music.douban.com/chart #导入需要的模块
from selenium import webdriver
import os
from lxml import etree
import time
from bs4 import BeautifulSoup
#生成浏览器对象
driver = webdriver.PhantomJS() #创建文件夹存储music信息
root_dir = 'douban_music'
if not os.path.exists(root_dir):
os.mkdir(root_dir) #抓取页面函数
def spider():
base_url = 'https://music.douban.com/chart'
#用浏览器获取网页
driver.get(base_url)
#等待页面加载
time.sleep(4)
# 获取页面资源
content = driver.page_source
# print(content) #在解析函数中解析页面元素
content_parser(content) def content_parser(content):
# 用xpath解析得到的页面资源 将content生成树结构
tree = etree.HTML(content)
# print(tree)
#拿取所有的歌曲列表
music_list = tree.xpath('//ul[@class="col5"]/li[@class="clearfix"]')
#遍历所有的歌曲列表
for music in music_list: #拿取歌曲排名
music_ranking = music.xpath('.//span[@class="green-num-box"]')
#判断歌曲排名是否是非空
if music_ranking != []:
#获取歌曲排名文本信息
music_ranking = music_ranking[0].text #拿取图片信息链接
music_src = music.xpath('./a/img/@src')
if music_src != []:
music_src = music_src[0] #拿取歌曲名称
music_name = music.xpath('./div/h3/a')
if music_name != []:
music_name = music_name[0].text
else:
#有一部分歌曲名的xpath路径不一样
music_name = music.xpath('./div/p/a')[0].text #拿取singer歌手名称以及播放次数
singer = music.xpath('./div/p')
# print(list(singer))
if singer != []:
#歌手没有全部拿取
singer = singer[0].text #拿取上榜天数
music_days = music.xpath('./span') # 趋势
music_trend = music_days[2].text
if music_days != []:
music_days = music_days[1].text #打印所有拿取到的歌曲信息
print(music_ranking,music_src,music_name,singer,music_days,music_trend) #主进程
if __name__ == '__main__':
spider() '''
music:
<ul class="col5"> <li class="clearfix">
<span class="green-num-box">1</span>
<a class="face" href="https://site.douban.com/baishui/" target="_blank">
<img src="https://img3.doubanio.com/view/site/small/public/1aa014ef81b271d.jpg">
</a>
<div class="intro">
<h3 class="icon-play" data-sid="721211">
<a href="javascript:;">另一首情歌 (feat. 袁田)</a>
</h3> <p>白水 / 5748次播放</p>
</div>
<span class="days">(上榜11天)</span>
<span class="trend arrow-stay"> 0 </span>
</li>
</ul>
'''
selenium和phantomjs,完成豆瓣音乐排行榜的内容爬取的更多相关文章
- Pyhton网络爬虫实例_豆瓣电影排行榜_Xpath方法爬取
-----------------------------------------------------------学无止境------------------------------------- ...
- Pyhton网络爬虫实例_豆瓣电影排行榜_BeautifulSoup4方法爬取
-----------------------------------------------------------学无止境------------------------------------- ...
- selenium在爬虫中的应用之动态数据爬取
一.selenium概念 selenium 是一个基于浏览器自动化的模块 selenium爬虫之间的关联: 1.便捷的获取动态加载的数据 2.实现模拟登录 基本使用 pip install selen ...
- selenium配合phantomjs实现爬虫功能,并把抓取的数据写入excel
# -*- coding: UTF-8 -*- ''' Created on 2016年5月13日 @author: csxie ''' import datetime from Base impor ...
- 豆瓣电影排行简单数据爬取_pyhton
先安装一下requests和bs4库: cmd下面:python -m pip install bs4 -i https://pypi.douban.com/simple 代码: import req ...
- Python爬取豆瓣音乐存储MongoDB数据库(Python爬虫实战1)
1. 爬虫设计的技术 1)数据获取,通过http获取网站的数据,如urllib,urllib2,requests等模块: 2)数据提取,将web站点所获取的数据进行处理,获取所需要的数据,常使用的技 ...
- Python selenium+phantomjs的js动态爬取
Selenium是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE.Mozilla Firefox.Chrome等.Phanto ...
- python爬虫:了解JS加密爬取网易云音乐
python爬虫:了解JS加密爬取网易云音乐 前言 大家好,我是"持之以恒_liu",之所以起这个名字,就是希望我自己无论做什么事,只要一开始选择了,那么就要坚持到底,不管结果如何 ...
- python+selenium+xpath 爬取天眼查工商基本信息
# -*- coding:utf-8 -*-# author: kevin# CreateTime: 2018/8/16# software-version: python 3.7 import ti ...
随机推荐
- 实例之跑马灯,函数创建、通过ID获取标签及内部的值,字符串的获取与拼接、定时器的使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Java 8实战之读书笔记四:高效Java 8编程
三.高效Java 8编程 第8章 重构.测试和调试 Java 8的新特性也可以帮助提升代码的可读性: 使用Java 8,你可以减少冗长的代码,让代码更易于理解 通过方法引用和S ...
- Git --05 Gitlab使用
目录 Gitlab使用 01. 外观配置 02. Gitlab汉化配置 03. 注册限制 04. 创建用户及组 05. 创建用户 06. 把用户添加到组 07. 创建项目 08. 推送代码到Gitla ...
- 变量管理 dotenv 的 使用
python-dotenv 安装 pip install python-dotenv 或 pipenv install python-dotenv --skip-lock 创建目标文件 在项目根目录下 ...
- CS184.1X 计算机图形学导论(第五讲)
一.观察:正交投影 1.特性:保持平行线在投影后仍然是平行的 2.一个长方体,对处在只有深度不同的位置上的同一物体来说,它的大小不会改变. 3.透视投影:平行线在远处会相交(例如铁轨) 4.glOrt ...
- NotePad++安装 html.css.js智能提示【转】
https://www.cnblogs.com/alirong/archive/2012/04/12/2443971.html
- condition简单示例
在concurrent包中提供了condition接口,通过该接口可唤醒指定的某个线程,而不是采用随机唤醒的形式. import java.util.concurrent.locks.Conditio ...
- spring中引入多个quertz 注意事项
每一个独立的调取任务 需起不同的名字,否则只有最后一个调度起作用其他不起作用
- No enclosing instance of type TestGson is accessible. Must qualify the allocation with an enclosing instance of type TestGson (e.g. x.new A() where x is an instance of TestGson).
main方法中实例化内部类报错: public class TestGson { public static void main(String[] args) { Gson gson=new Gson ...
- Using If/Truth Statements with pandas
pandas follows the numpy convention of raising an error when you try to convert something to a bool. ...