python爬虫实例——爬取歌单

时间:2023-03-09 06:45:51
python爬虫实例——爬取歌单

学习自<<从零开始学python网络爬虫>>

爬取酷狗歌单,保存入csv文件

直接上源代码:(含注释)

import requests                     #用于请求网页获取网页数据
from bs4 import BeautifulSoup #解析网页数据
import time #time库中的sleep()方法可以让程序暂停
import csv '''
爬虫测试
酷狗top500数据
写入csv文件
'''
fp = open('D://kugou.csv','wt',newline='',encoding='utf-8')#创建csv
writer = csv.writer(fp)
writer.writerow(('rank','singer','song','time'))
#加入请求头
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
} #定义获取信息的函数
def get_info(url):
wb_data = requests.get(url,headers=headers)#get方法加入请求头
soup = BeautifulSoup(wb_data.text,'html.parser')#对返回结果进行解析
#定位元素位置并通过selector方法获取
ranks = soup.select('span.pc_temp_num')
titles = soup.select('div.pc_temp_songlist > ul > li > a')
times = soup.select('span.pc_temp_tips_r > span')
for rank,title,time in zip(ranks,titles,times):
data = {
'rank':rank.get_text().strip(),
'singer':title.get_text().split('-')[0],
'song':title.get_text().split('-')[0],#通过split获取歌手和歌曲信息
'time':time.get_text().strip()#get_text()获取文本内容
}
writer.writerow((rank.get_text().strip(),title.get_text().split('-')[0],title.get_text().split('-')[0],time.get_text().strip()))
# 获取爬取信息并按字典格式打印
#print(data) #程序主入口
if __name__ == '__main__':
urls = ['http://www.kugou.com/yy/rank/home/{}-8888.html'.format(str(i)) for i in range(1,4)]#构造多页url
for url in urls:
get_info(url)#循环调用
time.sleep(1)#每循环一次,睡眠1秒,防止网页浏览频率过快导致爬虫失败

爬虫实例

浏览器:Chrome

请求头获取方法:

python爬虫实例——爬取歌单

网站爬取:

python爬虫实例——爬取歌单