Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to Profile a Python Program - Block 3
import timeit
import requests
from bs4 import BeautifulSoup
def get_word_count_of(url: str) -> int:
res = requests.get(url)
if res.status_code == 200:
_data = res.text
return count_words(_data)
return 0
def count_words(data: str) -> int:
_parsed_html = BeautifulSoup(data)
_paragraphs = [p.text for p in _parsed_html.body.find_all('p')]
return sum([p.count(' ') for p in _paragraphs])
if __name__ == '__main__':
start_time = timeit.default_timer()
word_count = get_word_count_of('')
end_time = timeit.default_timer() - start_time
print(f"{word_count} words counted in {end_time:.3f} seconds.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment