Python爬蟲爬取異步加載的數據


前言

爬取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文件打開后
在這里插入圖片描述
這樣一個簡單的異步加載的數據的爬取就成功了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM