本文作者: Code皮皮蝦,CSDN、掘金等各大平臺同名,有興趣的小伙伴可以點一波關(guān)注??,感謝您的支持!
爬蟲僅供學(xué)習使用
公眾號:JavaCodes
前言
本次爬取為App爬蟲入門案例,不進行過多復(fù)雜操作,旨在快速入門?。?!
爬取目標: 王者榮耀全英雄的名稱、類型、熱度、勝率、登場率、Ban率
部分截圖如下:
在這里插入圖片描述
數(shù)據(jù)分析
打開App

在這里插入圖片描述
進入首頁(需要登陸)
在這里插入圖片描述
選擇英雄,點擊全部
在這里插入圖片描述
在這里插入圖片描述
請求頭
在這里插入圖片描述
請求頭信息詳解
在這里插入圖片描述
請求體
在這里插入圖片描述
對JSON數(shù)據(jù)進行在線解析
在這里插入圖片描述
所需全部數(shù)據(jù)在data下的list中
在這里插入圖片描述
英雄的名稱、類型、熱度、勝率、登場率、Ban率
在這里插入圖片描述
可見數(shù)據(jù)是我們想要的
在這里插入圖片描述
完整代碼
import requests
import json
import xlsxwriter as xw
import os
headers = {
"Host": "ssl.kohsocialapp.qq.com:10001",
"Connection": "keep-alive",
"Content-Length": "1068",
"Origin": "https://camp.qq.com",
"User-Agent": "Mozilla/5.0 (Linux; Android 5.1.1; TAS-AN00 Build/TAS-AN00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/74.0.3729.136 Mobile Safari/537.36;GameHelper; smobagamehelper; Brand: HUAWEI TAS-AN00$",
"X-Client-Proto": "https",
"Accept": "application/json, text/plain, */*",
"noencrypt": "1",
"Content-Type": "application/x-www-form-urlencoded",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
"X-Requested-With": "com.tencent.gamehelper.smoba"
}
url = "https://ssl.kohsocialapp.qq.com:10001/hero/getdetailranklistbyid"
data = {
"userId": "1835412780",
"openid": "oFhrws9p-nFsxqRsGu94Lwhp0xck",
"source": "smoba_zhushou",
"msdkToken": "" #自己查看填寫
}
response = requests.post(url=url, headers=headers, data=data)
details = json.loads(response.text)
lists = details['data']['list']
work = xw.Workbook("./res.xlsx") # 不存在就創(chuàng)建,存在就報錯
# 新建工作表
sheet = work.add_worksheet("one")
sheet.write(0, 0, "名稱")
sheet.write(0, 1, "類型")
sheet.write(0, 2, "熱度")
sheet.write(0, 3, "勝率")
sheet.write(0, 4, "登場率")
sheet.write(0, 5, "Ban率")
cur = 0
for i in lists:
name = i['heroInfo'][0]['heroName']
type = i['heroInfo'][0]['heroCareer']
winRate = format(float(i['winRate']) * 100, '.2f') + "%"
showRate = format(float(i['showRate']) * 100, '.2f') + "%"
banRate = format(float(i['banRate']) * 100, '.2f') + "%"
tRank = i['tRank']
sheet.write(cur, 0, name)
sheet.write(cur, 1, type)
sheet.write(cur, 2, tRank)
sheet.write(cur, 3, winRate)
sheet.write(cur, 4, showRate)
sheet.write(cur, 5, banRate)
cur += 1
# 關(guān)閉
work.close()
最后
我是 Code皮皮蝦,一個熱愛分享知識的 皮皮蝦愛好者,未來的日子里會不斷更新出對大家有益的博文,期待大家的關(guān)注?。?!
創(chuàng)作不易,如果這篇博文對各位有幫助,希望各位小伙伴可以==一鍵三連哦!==,感謝支持,我們下次再見~~~
==分享大綱==</font>