博客
关于我
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/

你可能感兴趣的文章
nginx+tomcat单个域名及多个域名配置
查看>>
Nginx+Tomcat实现动静分离
查看>>
nginx+Tomcat性能监控
查看>>
nginx+uwsgi+django
查看>>
nginx+vsftp搭建图片服务器
查看>>
Nginx-http-flv-module流媒体服务器搭建+模拟推流+flv.js在前端html和Vue中播放HTTP-FLV视频流
查看>>
nginx-vts + prometheus 监控nginx
查看>>
Nginx/Apache反向代理
查看>>
Nginx: 413 – Request Entity Too Large Error and Solution
查看>>
nginx: [emerg] getpwnam(“www”) failed 错误处理方法
查看>>
nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:
查看>>
nginx: [error] open() “/usr/local/nginx/logs/nginx.pid“ failed (2: No such file or directory)
查看>>
nginx:Error ./configure: error: the HTTP rewrite module requires the PCRE library
查看>>
Nginx:objs/Makefile:432: recipe for target ‘objs/src/core/ngx_murmurhash.o‘解决方法
查看>>
nginxWebUI runCmd RCE漏洞复现
查看>>
nginx_rtmp
查看>>
Nginx、HAProxy、LVS
查看>>
nginx一些重要配置说明
查看>>
Nginx下配置codeigniter框架方法
查看>>
Nginx与Tengine安装和使用以及配置健康节点检测
查看>>