Created
April 8, 2020 15:26
-
-
Save KunoiSayami/daab3066097e616ad33c7e2cdf46dda3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from dataclasses import dataclass | |
from typing import NoReturn | |
import bs4 | |
import requests | |
@dataclass | |
class UserStatus: | |
username: str | |
rws: float | |
rate: float | |
elo: str | |
def __str__(self): | |
return f'{self.elo} {self.rate} {self.rws} {self.username}' | |
class UserNotFound(Exception): | |
def __init__(self, username: str): | |
self.username = username | |
def get_name(status: str) -> NoReturn: | |
for x in status.split('rate\n')[1].splitlines(): | |
if x == '#end': break | |
stat_group = x.split() | |
if stat_group[-3] != 'BOT' and '用户名审核中' not in stat_group[3]: | |
yield stat_group[3][1:-1] | |
def get_user_search_result(Session: requests.Session, name: str) -> str: | |
r = Session.get(f'https://www.5ewin.com/api/search?keywords={name}') | |
r.raise_for_status() | |
obj = r.json() | |
if obj['success']: | |
if obj['data']['user']['total'] == 0: | |
raise UserNotFound(name) | |
try: | |
return obj['data']['user']['list'][0]['domain'] | |
except IndexError: | |
print(obj) | |
raise | |
else: | |
raise UserNotFound(name) | |
def get_real_value(tag: bs4.Tag) -> str: | |
return tag.find(class_='val').text | |
def get_user_rating_by_username(Session: requests.Session, name: str) -> UserStatus: | |
r = Session.get(f'https://www.5ewin.com/data/player/{get_user_search_result(Session, name)}') | |
r.raise_for_status() | |
#obj = r.json() | |
soup = bs4.BeautifulSoup(r.text, 'lxml') | |
s = soup.find(class_='stat-data').select('li') | |
return UserStatus(name, float(get_real_value(s[2])), float(get_real_value(s[3])), get_real_value(s[4])) | |
def main(): | |
with requests.Session() as Session: | |
Session.headers.update({ | |
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win86; x86) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36' | |
}) | |
for name in get_name(status_string): | |
try: | |
print(str(get_user_rating_by_username(Session, name))) | |
except UserNotFound as e: | |
print(f'User {e.username} not found') | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment