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

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

目录


概述

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

准备

所需模块

  • re模块
  • requests模块

涉及知识点

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

运行效果

控制台打印:

完成爬虫

1. 分析网页

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

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

2. 爬虫代码

import re  import 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" } 请求的URL url = "http://www.hao123.com/music/wangzhi" 发送请求,获取响应 response = requests.get(url, headers=header).text 正则表达式:获取音乐导航链接URL pat_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/

你可能感兴趣的文章
svn访问报错500
查看>>
Orderer节点启动报错解决方案:Not bootstrapping because of 3 existing channels
查看>>
org.apache.ibatis.exceptions.PersistenceException:
查看>>
org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
查看>>
org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
查看>>
org.apache.poi.hssf.util.Region
查看>>
org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
查看>>
org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
查看>>
org.hibernate.HibernateException: Unable to get the default Bean Validation factory
查看>>
org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
查看>>
SQL-CLR 类型映射 (LINQ to SQL)
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
查看>>
org.tinygroup.serviceprocessor-服务处理器
查看>>
org/eclipse/jetty/server/Connector : Unsupported major.minor version 52.0
查看>>
org/hibernate/validator/internal/engine
查看>>
Orleans框架------基于Actor模型生成分布式Id
查看>>
SQL-36 创建一个actor_name表,将actor表中的所有first_name以及last_name导入改表。
查看>>
ORM sqlachemy学习
查看>>