VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > Python基础教程 >
  • python基础教程之Python爬虫爬取异步加载的数据

本站最新发布   Python从入门到精通|Python基础教程
试听地址  
https://www.xin3721.com/eschool/pythonxin3721/


前言

爬取qq音乐歌手数据接口数据

https://y.qq.com/portal/singer_list.html
这是歌手列表的网址

分析网页

  • f12开发者选项 找到network 里面有异步加载的数据,如果你对这个还不是很懂。可以先去小编的Python交流.裙 :一久武其而而流一思(数字的谐音)转换下可以找到了,里面有最新Python教程项目可拿,多跟里面的人交流,进步更快哦!
  • 刷新看找数据在这里插入图片描述
    看他们的response

https://u.y.qq.com/cgi-bin/musicu.fcg?-=getUCGI20652690515538596&g_tk=944669952&loginUin=1638786338&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq.json&needNewCode=0&data={"comm"%3A{"ct"%3A24%2C"cv"%3A0}%2C"singerList"%3A{"module"%3A"Music.SingerListServer"%2C"method"%3A"get_singer_list"%2C"param"%3A{"area"%3A-100%2C"sex"%3A-100%2C"genre"%3A-100%2C"index"%3A-100%2C"sin"%3A0%2C"cur_page"%3A1}}}
会有一个网址的response是歌手列表就是上述网址 记住这个网址然后就是请求这个网址

  • 先贴源码
import jsonpath
import json
import requests
import csv
import time
def writecsv(content):
	with open('spider2.csv','a+',encoding='utf-8',newline='') as filecsv:
		writer = csv.writer(filecsv)
		writer.writerow(content)
writecsv(['歌手名','歌手地区','歌手id','歌手图片'])
def getHtml(url):
	response=requests.get(url)
	response.encoding='utf-8'
	html=response.text
	html=json.loads(html)
	print(html)
	singname=jsonpath.jsonpath(html,'$..singerList..singer_name')
	singcountry=jsonpath.jsonpath(html,'$..singerList..country')
	singer_mid=jsonpath.jsonpath(html,'$..singerList..singer_mid')
	singer_pic=jsonpath.jsonpath(html,'$..singerList..singer_pic')
	print(singer_mid)
	for index,i in enumerate(singname):
		writecsv([i,singcountry[index],singer_mid[index],singer_pic[index]])
index=0
for i in range(0,801,80):
	index=index+1
	getHtml('https://u.y.qq.com/cgi-bin/musicu.fcg?-=getUCGI01616088836276819&g_tk=5381&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq.json&needNewCode=0&data={"comm":{"ct":24,"cv":0},"singerList":{"module":"Music.SingerListServer","method":"get_singer_list","param":{"area":-100,"sex":-100,"genre":-100,"index":-100,"sin":%(no)d,"cur_page":%(page)d}}}'% {'no':i,'page':index})
	time.sleep(3)
# index=0
# for i in range(0,801,80):
# 	index=index+1
# print(i)
# print(index)

这里用到json jsonpath最后都保存为csv格式文件方便实用,,如果你对这个还不熟,可以先去小编的Python交流.裙 :一久武其而而流一思(数字的谐音)转换下可以找到了,里面有最新Python教程项目可拿,多跟里面的人交流,进步更快哦!
html=json.loads(html)这里是将网页数据保存为json格式然后通过jsonpath解析json文件jsonpath语法类似xpath可以看下教程在这里插入图片描述
使用jsonpath.jsonpath()解析json格式的数据$是根节点不能丢

singname=jsonpath.jsonpath(html,'$..singerList..singer_name')

{
“singerList”: {
“data”: {
“area”: -100,
“genre”: -100,
“index”: -100,
“sex”: -100,
“singerlist”: [
{
“country”: “内地”,
“singer_id”: 5062,
“singer_mid”: “002J4UUk29y8BY”,
“singer_name”: “薛之谦”,
“singer_pic”: “http://y.gtimg.cn/music/photo_new/T001R150x150M000002J4UUk29y8BY.webp”
},
{
“country”: “内地”,
“singer_id”: 1199300,
“singer_mid”: “0013RsPD3Xs0FG”,
“singer_name”: “半阳”,
“singer_pic”: “http://y.gtimg.cn/music/photo_new/T001R150x150M0000013RsPD3Xs0FG.webp”
},

这里…代表绝对哪里有就匹配到哪相当于xpath的//singlist下的singer_name很好找到可以先打印一下看错误再改依次将歌手名歌手国家等打印出来因为他们的个数都是相同的采用枚举的方式将每一个按行写入csv格式

with open('spider2.csv','a+',encoding='utf-8',newline='') as filecsv:第一个参数是文件名,第二个参数是追加读方式,第三个是编码方式避免乱码,第四个是控制空行删除空行,会自动生成这个csv文件成功后看有没有多出来的csv文件打开后
在这里插入图片描述
这样一个简单的异步加载的数据的爬取就成功了


相关教程