Web Scraping
- Use textwrap.dedent().
- Use response.raise_for_status(), but add helpful msg if 404.
from textwrap import dedent import requests from bs4 import BeautifulSoup def _extract(username): response = requests.get(f'https://www.codewars.com/users/{username}') try: response.raise_for_status() except requests.exceptions.HTTPError as e: if response.status_code == 404: raise Exception(f"username is case-sensitive.", e) else: raise else: soup = BeautifulSoup(response.text, 'html.parser') stat_info = soup.findAll('div', class_='stat') return stat_info def _transform(username, stat_info): name, member, rank, honor, position, percentage, katas = (stat_info[0], stat_info[3], stat_info[9], stat_info[10], stat_info[11], stat_info[12], stat_info[13]) return dedent(f"""\ {username}'s Codewars stats: {member.text} {rank.text} {honor.text} {position.text} {percentage.text} {katas.text}""") def get_codewars_stats(username): """Scraps, and retrieves Codewars stats of given username.""" stat_info = _extract(username) return _transform(username, stat_info)
from bs4 import BeautifulSoup- from textwrap import dedent
- import requests
- from bs4 import BeautifulSoup
def get_codewars_stats(username):"""Scraps, and retrieves Codewars stats of given username."""source = requests.get(f'https://www.codewars.com/users/{username}')# Verify request status:if source.status_code == 404:return 'Something went wrong, enter a valid Codewars username.'- def _extract(username):
- response = requests.get(f'https://www.codewars.com/users/{username}')
- try:
- response.raise_for_status()
- except requests.exceptions.HTTPError as e:
- if response.status_code == 404:
- raise Exception(f"username is case-sensitive.", e)
- else:
- raise
- else:
soup = BeautifulSoup(source.text, 'html.parser')- soup = BeautifulSoup(response.text, 'html.parser')
- stat_info = soup.findAll('div', class_='stat')
name, member, rank, honor, position, percentage, katas = (stat_info[0],- return stat_info
- def _transform(username, stat_info):
- name, member, rank, honor, position, percentage, katas = (stat_info[0],
- stat_info[3],
- stat_info[9],
- stat_info[10],
- stat_info[11],
- stat_info[12],
- stat_info[13])
return f"{username}'s Codewars stats:\n\t{member.text}\n\t{rank.text}\n\t{honor.text}\n\t{position.text}\n\t{percentage.text}\n\t{katas.text}"- return dedent(f"""\
- {username}'s Codewars stats:
- {member.text}
- {rank.text}
- {honor.text}
- {position.text}
- {percentage.text}
- {katas.text}""")
- def get_codewars_stats(username):
- """Scraps, and retrieves Codewars stats of given username."""
- stat_info = _extract(username)
- return _transform(username, stat_info)
import unittest from solution import get_codewars_stats from bs4 import BeautifulSoup import requests class TestGetCodeWarsStats(unittest.TestCase): """Setup testing variables.""" def setUp(self) -> None: # Feel free to use your username as a test sample instead: self.username_sample = 'seraph776' self.expected = get_codewars_stats(self.username_sample) def test_get_codewars_stats(self): """Tests get_codewars stats function.""" self.assertEqual(get_codewars_stats(self.username_sample), self.expected) with self.assertRaises(Exception): get_codewars_stats('invalid_username') if __name__ == '__main__': unittest.main()
- import unittest
- from solution import get_codewars_stats
- from bs4 import BeautifulSoup
- import requests
- class TestGetCodeWarsStats(unittest.TestCase):
- """Setup testing variables."""
- def setUp(self) -> None:
- # Feel free to use your username as a test sample instead:
- self.username_sample = 'seraph776'
- self.expected = get_codewars_stats(self.username_sample)
- def test_get_codewars_stats(self):
- """Tests get_codewars stats function."""
- self.assertEqual(get_codewars_stats(self.username_sample), self.expected)
self.assertEqual(get_codewars_stats('invalid_username'), 'Something went wrong, enter a valid Codewars username.')- with self.assertRaises(Exception):
- get_codewars_stats('invalid_username')
- if __name__ == '__main__':
- unittest.main()
For me, this is easier to read.
def is_leap_year(year: str) -> bool: """Returns True if year is a Leap Year else False.""" if year % 400 == 0: return True elif year % 100 == 0: return False else: return year % 4 == 0
- def is_leap_year(year: str) -> bool:
- """Returns True if year is a Leap Year else False."""
case1: bool = year % 4 == 0case2: bool = year % 100 == 0case3: bool = year % 400 == 0return case1 and not case2 or case3- if year % 400 == 0:
- return True
- elif year % 100 == 0:
- return False
- else:
- return year % 4 == 0