Python爬虫之旅之豆瓣音乐

前言

  • Python是一门计算机程序语言,目前人工智能科学领域应用广泛,应用广泛就表明各种库,各种相关联的框架都是以Python作为主要语言开发出来的。
  • 谷歌的TensorFlow大部分代码都是Python
  • Python虽然是脚本语言,但是因为容易学,迅速成为科学家的工具,从而积累了大量的工具库、架构,人工智能涉及大量的数据计算,用Python是很自然的,简单高效。
  • Python有非常多优秀的深度学习库可用,现在大部分深度学习框架都支持Python,不用Python用谁?

环境以及工具

pyCharm 2018.3.3
Python 3.7.2
Google Chrome 版本 71.0.3578.98(正式版本)(64 位)

效果展示

..........热门音乐人才分类..........
['豆瓣摄影', '流行', '轻音乐', '摇滚', '古典', '电子', '世界音乐', '民谣', '说唱', '爵士', '原声']
..........热门音乐URL..........
['https://ypy.douban.com', '/artists/genre_page/6/', '/artists/genre_page/2/', '/artists/genre_page/8/', '/artists/genre_page/1/', '/artists/genre_page/3/', '/artists/genre_page/10/', '/artists/genre_page/4/', '/artists/genre_page/7/', '/artists/genre_page/5/', '/artists/genre_page/9/']
..........热门音乐URL进行自定义拼接..........
https://music.douban.com/artists/genre_page/6/
https://music.douban.com/artists/genre_page/2/
https://music.douban.com/artists/genre_page/8/
https://music.douban.com/artists/genre_page/1/
https://music.douban.com/artists/genre_page/3/
https://music.douban.com/artists/genre_page/10/
https://music.douban.com/artists/genre_page/4/
https://music.douban.com/artists/genre_page/7/
https://music.douban.com/artists/genre_page/5/
https://music.douban.com/artists/genre_page/9/

整体步骤

1. Requests 发送网络请求
2. etree解析html
3. 爬虫分析处理

核心步骤解读

1. Requests 发送网络请求
from lxml import etree
import requests

url = 'https://music.douban.com/'  # 需要爬的网址
page = requests.Session().get(url)
2. etree解析html
htmlString = etree.HTML(page.text)
3. 爬虫分析处理

网页选中流行,右击

用xpath匹配指定的标签

musicType = htmlString.xpath("//tr//a/text()")
musicUrl = htmlString.xpath("//tr//a/@href")

源码展示

# 爬取 豆瓣音乐音乐人分类 https://music.douban.com
from lxml import etree
import requests

url = 'https://music.douban.com/'  # 需要爬的网址
page = requests.Session().get(url)
htmlString = etree.HTML(page.text)
# print("page.text \n" + htmlString)
musicType = htmlString.xpath("//tr//a/text()")
musicUrl = htmlString.xpath("//tr//a/@href")
print("..........热门音乐人才分类..........")
print(musicType)
print("..........热门音乐URL..........")
print(musicUrl)
print("..........热门音乐URL进行自定义拼接..........")
for urlIndex in range(len(musicUrl)):
    if urlIndex >= 1:
        print(musicUrl[0].replace("ypy", "music") + musicUrl[urlIndex])

源码位置

请关注公众号并在后台回复:Python

Python爬虫系列分享教程

Python爬虫之旅之Selenium库的使用

Kotlin重构系列分享教程

Kotlin环境搭建
Kotlin重构初体验之告别FindViewById
Kotlin重构如何兼容原先的ButterKnife、EventBus3.1
当ButterKnife8.8.1碰到AndroidX怎么办

请关注我(分享日常开发)

image

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值