博客
关于我
python爬虫实践之爬取hao123音乐音乐导航
阅读量:250 次
发布时间:2019-03-01

本文共 907 字,大约阅读时间需要 3 分钟。

目录


概述

爬取hao123上的所有音乐导航链接

准备

所需模块

  • re模块
  • requests模块

涉及知识点

  • python基础
  • requests模块基础
  • re模块基础

运行效果

控制台打印:

完成爬虫

1. 分析网页

打开音乐导航,按F12分析网页

因此可以使用URL:请求该网页的HTML源代码进行数据获取。

2. 爬虫代码

import reimport requests# 音乐导航网址:http://www.hao123.com/music/wangzhi# 请求头header = {    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36"}# 请求的URLurl = "http://www.hao123.com/music/wangzhi"# 发送请求,获取响应response = requests.get(url, headers=header).text# 正则表达式:获取音乐导航链接URLpat_music_url = r''# 正则表达式:获取音乐导航名字pat_music_name = r'[\s\s]*?(.*?)[\s\s]*?'# 音乐导航URL列表music_url_list = re.findall(pat_music_url, response.strip())# 音乐导航名字列表music_name_list = re.findall(pat_music_name, response)# 循环遍历if len(music_url_list) == len(music_name_list):    for i in range(0, len(music_url_list)):        print(music_name_list[i], ":", music_url_list[i])

运行代码结果如上所示,主要的难点就是使用正则表达式提取数据。

转载地址:http://vqzx.baihongyu.com/

你可能感兴趣的文章
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>