Python BeautifulSoup reference
1 April 2024 (Updated 1 April 2024)
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
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment