Repository with sources and generator of https://larlet.fr/david/ https://larlet.fr/david/
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

4 лет назад
1234567891011121314151617181920212223242526272829303132333435363738
  1. title: Python et cache
  2. J’ai eu plusieurs demandes techniques concernant [la mise en cache](/david/stream/2015/01/05/) des articles liés cette année. J’utilise la bibliothèque [readability-lxml](https://pypi.python.org/pypi/readability-lxml) de manière assez standard :
  3. :::python
  4. import requests
  5. from readability.readability import Document
  6. def extract_page(url):
  7. """From an URL, extract title and content.
  8. The title is shortened through the `short_title` method.
  9. The content doesn't contain `<body>` tags to be directly
  10. embeddable in the template and rendered as is.
  11. """
  12. # Retrieves the resource and turns it into a doc.
  13. response = requests.get(url)
  14. document = Document(response.text, debug=True)
  15. # The short title is more concise and readable.
  16. title = document.short_title()
  17. content = document.summary(html_partial=True)
  18. # Removing the added <div> and spaces.
  19. content = content[5:-6].strip()
  20. return title, content
  21. La seule chose à prendre en compte, ce sont les erreurs particulières qui peuvent être remontées que je gère ainsi :
  22. :::python
  23. try:
  24. title, content = extract_page(url)
  25. except (requests.adapters.SSLError,
  26. lxml.etree.XMLSyntaxError,
  27. requests.exceptions.ConnectionError), e:
  28. print('WARNING: {error}'.format(error=e))
  29. Dans ces cas je récupère la page à la main. J’avais pas mal de bugs lors du téléchargement des pages mais je viens de me rendre compte des nombreuses versions qui ont été publiées depuis ma dernière mise à jour. J’espère avoir moins à retravailler le *markup* maintenant… ce qui me permettrait d’automatiser la récupération des vieux articles.