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

#使用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. luoguP1965 转圈游戏(NOIP2013)(快速幂)

    luogu P1965 转圈游戏 题目 #include<iostream> #include<cstdlib> #include<cstdio> #include ...

  2. 关联查询总结,left join 和 inner join 区别和优化

    left join 是做左外关联,主表内容都会显示:符合关联条件的附表内容才会显示出来. inner join 是内关联,没有主表附表的概念:两个表中,同时符合关联条件的数据才会显示出来. left ...

  3. 在pythonanywhere部署你的第一个应用

    pythonanywhere是一个免费的托管python的代码,可以测试你的web应用,用起来还是比较方便的,现在就来介绍如何在pythonanywhere部署你的应用. 下载你的代码 我的代码是托管 ...

  4. rpm - RPM 软件包管理器

    SYNOPSIS 查询和校验软件包: rpm {-q|--query} [select-options] [query-options] rpm {-V|--verify} [select-optio ...

  5. smbrun - smbd和外部程序间的接口程序。

    总览 SYNOPSIS smbrun shell-command 描述 DESCRIPTION 此程序是samba套件的一部分. smbrun是个非常小的“粘合”程序,用于为smbd守护程序smbd( ...

  6. oracle-SQL语句执行原理和完整过程详解

    SQL语句执行过程详解 一条sql,plsql的执行到底是怎样执行的呢? 一.SQL语句执行原理 第一步:客户端吧语句发个服务端执行 当我们在客户端执行select语句时,客户端会把这条SQL语句发送 ...

  7. PL SQL安装

    首先,在官网下载PL SQL 的对应版本,本机是64位的就下载64位的,网址:https://www.allroundautomations.com/downloads.html#PLS 点击应用程序 ...

  8. py2 一键转换成py3

    找到python自带的2to3-3.7 文件,在终端里输入 python 2to3-3.7 -w /Users/v_fanjiexiong/PycharmProjects/HugeGraph_fan/ ...

  9. BZOJ2213 & LOJ2161 「POI2011 R2 Day1」Difference 最大子段和

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2213 https://loj.ac/problem/2161 题解 做一道简单题来放松一下. ...

  10. CentOS7.2安装Oracle12.1.0.2

    Centos7.2环境安装(安装桌面) #安装界面 #查看版本 cat /etc/redhat-release #查看连接 ifconfig eth0 echo "127.0.0.1 tes ...