本帖最后由 2079898548 于 2020-8-24 21:52 编辑
最近学了下多线程,但不知道爬点什么,然后最后就写个爬取直播人数
然后,就先弄了个双线程。
爬取的是lol和王者荣耀的直播观看人数。
2个游戏有高有低
没引战的意思
刚刚晚上爬的
中午爬的
[Python] 纯文本查看 复制代码 # -*- 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)
没引战的意思
还请各位神仙大佬,帮忙看看这多线程,有没有什么问题。
[fly]200积分是个漫长的日子[/fly]
|