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

#使用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. Codeforces 1080C 题解(思维+二维前缀和)

    题面 传送门 题目大意: 有一个黑白的棋盘,现在将棋盘上的一个子矩形全部染成黑色,另一个子矩形全部染成白色 求染完色后黑,白格子的总数 分析 我们可以发现,对于一个(1,1)到(x,y)的矩形,若xy ...

  2. hdu5857 Median(模拟)

    Median Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  3. python学习第三十天函数的形参,实参及函数文档

    python函数的形参是定义函数def 函数名 小括号里面的变量,实参是调用函数时候的值,函数文档是提供函数功能的开发文档,下面 详细说明系列方法 1,函数的形参 def chan(name): pr ...

  4. SQL 查询中not in 与 not exists 的区别

    1.in和exists in是把外表和内表作hash连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询,一直以来认为exists比in效率高的说法是不准确的.如果查询的两个表 ...

  5. python常用函数 P

    popleft(iterable) 对应pop,左侧弹出,队列适用. 例子: permutations(iterable, int) itertools的permutations方法可以产生集合的所有 ...

  6. (ACM模板)映射map

    #include<iostream> #include<cstdio> #include<map> using namespace std; int main() ...

  7. Go's Declaration Syntax

    Introduction Newcomers to Go wonder why the declaration syntax is different from the tradition estab ...

  8. AndroidStudio之Theme、colorPrimary、colorPrimaryDark、colorAccent详解

    今天就来看看在Androi5.0中常用的颜色属性. 我们可以先定义一个style,然后在这个style中设定每一个Activity或者整个App的颜色,最后在清单文件中来给某个Activity设置主题 ...

  9. HTML基础用 表格做报表

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. vue的列表交错过渡

    参考文章 https://juejin.im/post/5cccf5b0e51d453a907b4af1