Learning by doing: Reading books and trying to understand the (code) examples
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

20 lines
529 B

  1. from urllib.request import urlopen
  2. import time
  3. class WebPage:
  4. def __init__(self, url):
  5. self.url = url
  6. self.an_old_time = 0
  7. self._content = None
  8. @property
  9. def content(self):
  10. now = time.time()
  11. if not self._content or (now - self.an_old_time > 10):
  12. self.an_old_time = now
  13. print("Retrieving new page...")
  14. self._content = urlopen(self.url).read()
  15. print("Got page in {} seconds".format(time.time() - now))
  16. return self._content