|
爬取的是lol和王者荣耀的直播观看人数。
2个游戏有高有低
没引战的意思- # -*- coding: utf-8 -*-
-
- import requests
- import threading
- from lxml import etree
-
- lolURL={
- "huya":"https://www.huya.com/g/lol",
- "qier":"https://egame.qq.com/livelist?layoutid=lol",
- "douyu":"https://www.douyu.com/g_LOL"
- }
- wzURL={
- "huya":"https://www.huya.com/g/2336",
- "qier":"https://egame.qq.com/livelist?layoutid=1104466820",
- "douyu":"https://www.douyu.com/g_wzry"
- }
-
- headers={
- 'accept':'text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01',
- 'accept-encoding':'gzip, deflate, br',
- 'accept-language':'zh-CN,zh;q=0.9',
- 'sec-fetch-dest':'empty',
- 'sec-fetch-mode':'cors',
- 'sec-fetch-site':'same-origin',
- 'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'
- }
-
- lol_people = 0
- wz_people = 0
-
- def game_clear(people,game):
- if game == 'lol':
- global lol_people
- lol_people += people
- elif game == 'wz':
- global wz_people
- wz_people += people
-
- def clean(people_list,game):
- people = 0.0
- for r in people_list:
- r=float(r.replace('万','').replace(',','').strip())
- people += r
- game_clear(people,game)
-
-
- def game_people(URL,game):
- for url,name in zip(URL.values(),URL):
- req = requests.get(url=url,headers=headers).text
- req_xp = etree.HTML(req)
- if name == 'huya':
- people_list = req_xp.xpath('//*[@class="js-num"]/text()')
- clean(people_list,game)
- elif name == 'qier':
- people_list = req_xp.xpath('//*[@class="popular"]/text()')
- clean(people_list,game)
- elif name == 'douyu':
- people_list = req_xp.xpath('//*[@class="DyListCover-hot is-template"]/text()')
- clean(people_list,game)
- else:
- print('程序出错')
-
-
- if __name__ == '__main__':
- t1 = threading.Thread(game_people(lolURL,"lol"))
- t2 = threading.Thread(game_people(wzURL,"wz"))
- t1.start()
- t2.start()
- print('英雄联盟:%.2f万人'%lol_people)
- print('王者荣耀:%.2f万人'%wz_people)
复制代码
|
|