sajad torkamani

BeautifulSoup is a popular Python library for pulling data out of HTML / XML files. Here’s an example usage:

from urllib.request import urlopen
from bs4 import BeautifulSoup
import re


def get_honor(username: str):
    try:
        html = urlopen(f"https://www.codewars.com/users/{username}").read()
        soup = BeautifulSoup(html, 'html.parser')
        honor = soup.select('.stat-container .stat:nth-of-type(2)')[0].text.replace('Honor:', '')
        honor = re.sub("[^\d]", "", honor)

        return int(honor)
    except:
        raise "Failed to find honour for the username: {username}"


if __name__ == '__main__':
    honour = get_honor('sajadtorkamani')
    print(honour)
Tagged: Python

Leave a comment

Your email address will not be published. Required fields are marked *