代码要多敲 注释要清晰 哪怕再简单

#使用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>白水&nbsp;/&nbsp;5748次播放</p>
</div>
<span class="days">(上榜11天)</span>
<span class="trend arrow-stay"> 0 </span>
</li>
</ul>
'''

selenium和phantomjs,完成豆瓣音乐排行榜的内容爬取的更多相关文章

  1. Pyhton网络爬虫实例_豆瓣电影排行榜_Xpath方法爬取

    -----------------------------------------------------------学无止境------------------------------------- ...

  2. Pyhton网络爬虫实例_豆瓣电影排行榜_BeautifulSoup4方法爬取

    -----------------------------------------------------------学无止境------------------------------------- ...

  3. selenium在爬虫中的应用之动态数据爬取

    一.selenium概念 selenium 是一个基于浏览器自动化的模块 selenium爬虫之间的关联: 1.便捷的获取动态加载的数据 2.实现模拟登录 基本使用 pip install selen ...

  4. selenium配合phantomjs实现爬虫功能,并把抓取的数据写入excel

    # -*- coding: UTF-8 -*- ''' Created on 2016年5月13日 @author: csxie ''' import datetime from Base impor ...

  5. 豆瓣电影排行简单数据爬取_pyhton

    先安装一下requests和bs4库: cmd下面:python -m pip install bs4 -i https://pypi.douban.com/simple 代码: import req ...

  6. Python爬取豆瓣音乐存储MongoDB数据库(Python爬虫实战1)

    1.  爬虫设计的技术 1)数据获取,通过http获取网站的数据,如urllib,urllib2,requests等模块: 2)数据提取,将web站点所获取的数据进行处理,获取所需要的数据,常使用的技 ...

  7. Python selenium+phantomjs的js动态爬取

    Selenium是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE.Mozilla Firefox.Chrome等.Phanto ...

  8. python爬虫:了解JS加密爬取网易云音乐

    python爬虫:了解JS加密爬取网易云音乐 前言 大家好,我是"持之以恒_liu",之所以起这个名字,就是希望我自己无论做什么事,只要一开始选择了,那么就要坚持到底,不管结果如何 ...

  9. python+selenium+xpath 爬取天眼查工商基本信息

    # -*- coding:utf-8 -*-# author: kevin# CreateTime: 2018/8/16# software-version: python 3.7 import ti ...

随机推荐

  1. 二维码生成器,基于python,segno库

    import segno temp = input("Please enter value:") qr = segno.make(temp) qr.save("qrcod ...

  2. js提交map类型参数

    方式一:使用 xmlHttpRequest 对象发送数据 function sendMapPost(map, url){ var xmlHttpRequest = createXMLHttpReque ...

  3. HRESULT是什么类型

    HRESULT 是一种简单的数据类型,可以判断函数执行的结果.HRESULT 常被用作COM调用的返回值.充分利用HRESULT返回信息可以帮助提高我们的代码质量,提供程序的健壮性. HRESULT ...

  4. 9、numpy——数组操作

    Numpy 中包含了一些函数用于处理数组,大概可分为以下几类: (1)修改数组形状 (2)翻转数组 (3)修改数组维度 (4)连接数组 (5)分割数组 (6)数组元素的添加与删除 1.修改数组形状 函 ...

  5. Using text search in Web page with Sikuli

    在網頁中如何使用Sikuli找特定字串呢? 原理: 我們可以使用 組合鍵 ctrl + 來放大網頁的比例,使得sikuli的OCR功能找的更清準 實作: for i in range(4): type ...

  6. HDU Stealing Harry Potter's Precious(状压BFS)

    状压BFS 注意在用二维字符数组时,要把空格.换行处理好. #include<stdio.h> #include<algorithm> #include<string.h ...

  7. 1. ZooKeeper简介

    1. ZooKeeper是什么 ZooKeeper致力于提供一个高性能.高可用,且具备严格的顺序访问控制能力的分布式协调服务,是雅虎公司创建,是Google的Chubby一个开源的实现,也是Hadoo ...

  8. PHP中的魔术方法总结:__construct,__destruct ,__call,__callStatic,__get,__set,__isset, __unset ,__sleep,__wakeup,__toString,__set_state,__clone,__autoload

    1.__get.__set这两个方法是为在类和他们的父类中没有声明的属性而设计的__get( $property ) 当调用一个未定义的属性时访问此方法__set( $property, $value ...

  9. 使用macOS苹方替换Windows 10微软雅黑

    关于微软雅黑 Windows从Vista开始用到现在的”微软雅黑”十多年以来基本没什么大改动,而大家的显示器从CRT进化到了IPS高分屏,十年前看着还OK的字体现在在绝大多数屏幕上可能就是这个样子的: ...

  10. 【LeetCode】队列 queue(共8题)

    [346]Moving Average from Data Stream [353]Design Snake Game [363]Max Sum of Rectangle No Larger Than ...