查看: 687|回复: 1

70行代码爬取斗鱼虎牙企鹅直播观看人数

[复制链接]
发表于 2020-8-25 00:06 | 显示全部楼层 |阅读模式
非法程序、 2020-8-25 00:06 687 1 显示全部楼层
爬取的是lol和王者荣耀的直播观看人数。
2个游戏有高有低
没引战的意思
  1. # -*- coding: utf-8 -*-

  2. import requests
  3. import threading
  4. from lxml import etree

  5. lolURL={
  6.     "huya":"https://www.huya.com/g/lol",
  7.     "qier":"https://egame.qq.com/livelist?layoutid=lol",
  8.     "douyu":"https://www.douyu.com/g_LOL"
  9. }
  10. wzURL={
  11.     "huya":"https://www.huya.com/g/2336",
  12.     "qier":"https://egame.qq.com/livelist?layoutid=1104466820",
  13.     "douyu":"https://www.douyu.com/g_wzry"
  14. }

  15. headers={
  16.     'accept':'text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01',
  17.     'accept-encoding':'gzip, deflate, br',
  18.     'accept-language':'zh-CN,zh;q=0.9',
  19.     'sec-fetch-dest':'empty',
  20.     'sec-fetch-mode':'cors',
  21.     'sec-fetch-site':'same-origin',
  22.     'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'
  23. }

  24. lol_people = 0
  25. wz_people = 0

  26. def game_clear(people,game):
  27.     if game == 'lol':
  28.         global lol_people
  29.         lol_people += people
  30.     elif game == 'wz':
  31.         global wz_people
  32.         wz_people += people

  33. def clean(people_list,game):
  34.     people = 0.0
  35.     for r in people_list:
  36.         r=float(r.replace('万','').replace(',','').strip())
  37.         people += r
  38.     game_clear(people,game)


  39. def game_people(URL,game):
  40.     for url,name in zip(URL.values(),URL):
  41.         req = requests.get(url=url,headers=headers).text
  42.         req_xp = etree.HTML(req)
  43.         if name == 'huya':
  44.             people_list = req_xp.xpath('//*[@class="js-num"]/text()')
  45.             clean(people_list,game)
  46.         elif name == 'qier':
  47.             people_list = req_xp.xpath('//*[@class="popular"]/text()')
  48.             clean(people_list,game)
  49.         elif name == 'douyu':
  50.             people_list = req_xp.xpath('//*[@class="DyListCover-hot is-template"]/text()')
  51.             clean(people_list,game)
  52.         else:
  53.             print('程序出错')


  54. if __name__ == '__main__':
  55.     t1 = threading.Thread(game_people(lolURL,"lol"))
  56.     t2 = threading.Thread(game_people(wzURL,"wz"))
  57.     t1.start()
  58.     t2.start()
  59.     print('英雄联盟:%.2f万人'%lol_people)
  60.     print('王者荣耀:%.2f万人'%wz_people)
复制代码

发表于 2022-11-5 02:37 | 显示全部楼层
3175402486 2022-11-5 02:37 显示全部楼层
药不能停 / 加大药量
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则 返回列表 发新帖

快速回复 返回顶部 返回列表