Skip to content

Instantly share code, notes, and snippets.

  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bilalozdemir/3d89ca6b5c44c5c5515420aba7bcbc35 to your computer and use it in GitHub Desktop.
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('https://example.org/')
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