#! /usr/bin/env python # -*- coding: utf-8 -*- """ A small script to dump all Maths Exercice LaTeX sources from some website. Eavily dependant on the website being used. Doc: http://www.crummy.com/software/BeautifulSoup/bs3/documentation.html#Printing%20a%20Document And an example: http://mp.cpgedupuydelome.fr/mesexos.php?idTeX=1485 - Author: Lilian BESSON - Email: Lilian.BESSON[AT]ens-cachan[DOT]fr - Web version: http://perso.crans.org/besson/bin/dump_dD.py - Web version (2): https://bitbucket.org/lbesson/bin/src/master/dump_dD.py - Date: 24-07-2013 """ from __future__ import print_function # Python 2/3 compatibility ! import sys try: from ansicolortags import printc as print except ImportError as e: print("ansicolortags not available, using regular print.") print(" You can install it with : 'pip install ansicolortags' (or sudo pip)...") print(".: Lilian Besson presents :.") print("Maths exercice LaTeX sources dumper, v0.1") # Quicker if you keep this value up-to-date here # nbExos=\$(wget -O - --quiet "http://mp.cpgedupuydelome.fr/index.php" | html2text | grep Exercice | grep -o [0-9]*) # nbExos = 3994 # print("Choix aléatoire parmi %i exercices de maths (niveau MPSI et MP)..." % nbExos) # from random import randint # numexo = randint(1, nbExos) # FIXME ! numexo = int(sys.argv[1]) if len(sys.argv) > 1 else 1485 chapter = str(sys.argv[2]) if len(sys.argv) > 2 else "" urlToGo = "http://mp.cpgedupuydelome.fr/mesexos.php?idTeX=%i" % numexo print("Numéro %i. On va vers \"%s\"" % (numexo, urlToGo)) # On récupère la page (la partie la plus lente du coup) import urllib2 response = urllib2.urlopen(urlToGo) html = response.read() # BeautifulSoup v3 (et pas v4, attention !) from BeautifulSoup import BeautifulSoup # On l'analyse parsed_html = BeautifulSoup(html, fromEncoding='utf-8') print("Encodage original : %s\n\n" % parsed_html.originalEncoding) # On cherche la section
..
contenu = parsed_html.body.find('section', attrs={'id': 'contenu'}) # Et on prend le contenu de la première