Created
June 21, 2019 20:56
-
-
Save fxcoudert/48ab71e13d6844b7a57ecc5635a1dbf2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# | |
import itertools | |
import time | |
import requests | |
from bs4 import BeautifulSoup | |
BASE_URL = 'http://intersection.dsi.cnrs.fr/intersection/resultats-cc-fr.do' | |
GRADES = {'CRCN': '223', 'CRHC': '233', 'DR2': '15', 'DR1': '16'} | |
CAMPAGNE = '84' | |
def get_sections(): | |
r = requests.get(BASE_URL, params={'campagne': CAMPAGNE}) | |
s = BeautifulSoup(r.content, features='lxml') | |
sec = s.find('select', attrs={'name': 'section'}) | |
sec = [i.string.strip() for i in sec.find_all('option') if i.contents] | |
return sec | |
def get_concours(sections): | |
res = [] | |
for sec in sections: | |
for grade, grade_num in GRADES.items(): | |
r = requests.get(BASE_URL, params={'campagne': CAMPAGNE, 'section': sec, 'grade': grade_num}) | |
s = BeautifulSoup(r.content, features='lxml') | |
s = s.find('select', attrs={'name': 'numconc'}) | |
for i in s.find_all('option'): | |
i = i.string.strip() | |
if len(i) > 0: | |
res.append((i, sec, grade)) | |
#time.sleep(0.5) | |
return res | |
def resultats_concours(c, sec, grade): | |
print(f'<h2>Concours {c}, section {sec} ({grade})</h2>') | |
adm = [] | |
r = requests.get(BASE_URL, params={'campagne': CAMPAGNE, 'conc': c, 'phase': 'ADMISSIBILITE'}) | |
s = BeautifulSoup(r.content, features='lxml') | |
s = s.find(id='ZonePrint').find('table') | |
for tr in s.find_all('tr'): | |
td = [i.string.strip() for i in tr.find_all('td')] | |
rang = td[1].replace('Admissible classé n° ', '') | |
adm.append(f'{td[0]} ({rang})') | |
admis = [] | |
r = requests.get(BASE_URL, params={'campagne': CAMPAGNE, 'conc': c, 'phase': 'ADMISSION'}) | |
s = BeautifulSoup(r.content, features='lxml') | |
s = s.find(id='ZonePrint').find('table') | |
for tr in s.find_all('tr'): | |
td = [i.string.strip() for i in tr.find_all('td')] | |
if 'liste principale' in td[1]: | |
bold = lambda x: '<b>' + x + '</b>' | |
else: | |
bold = lambda x: '<em>' + x + '</em>' | |
admis.append(f'{bold(td[0])}') | |
print('<table>') | |
print('<tr><th>Admissibles</th><th>Admis</th></tr>') | |
for i, j in itertools.zip_longest(adm, admis, fillvalue=""): | |
n = j.replace('<b>', '').replace('<em>', '').replace('</b>', '').replace('</em>', '') | |
if len(n) > 0 and not n in i: | |
print(f'<tr style="background: #FFD0D0"><td>{i}</td><td>{j}</td></tr>') | |
else: | |
print(f'<tr><td>{i}</td><td>{j}</td></tr>') | |
print('</table>') | |
admconc = [] | |
r = requests.get(BASE_URL, params={'campagne': CAMPAGNE, 'conc': c, 'phase': 'ADMCONC'}) | |
s = BeautifulSoup(r.content, features='lxml') | |
s = s.find(id='ZonePrint').find('table') | |
for tr in s.find_all('tr'): | |
td = tr.find_all('td')[0] | |
admconc.append(td.string.strip()) | |
if len(admconc) > 0: | |
print(f'<details><summary>Liste des admis à concourir ({len(admconc)})</summary><p>') | |
for i in admconc: | |
print(i + '<br/>') | |
print('</p></details>') | |
admconc = [] | |
r = requests.get(BASE_URL, params={'campagne': CAMPAGNE, 'conc': c, 'phase': 'ADMAPOUR'}) | |
s = BeautifulSoup(r.content, features='lxml') | |
s = s.find(id='ZonePrint').find('table') | |
for tr in s.find_all('tr'): | |
td = tr.find_all('td')[0] | |
admconc.append(td.string.strip()) | |
if len(admconc) > 0: | |
print(f'<details><summary>Liste des admis à poursuivre ({len(admconc)})</summary><p>') | |
for i in admconc: | |
print(i + '<br/>') | |
print('</p></details>') | |
def main(): | |
sections = get_sections() | |
print('''<!DOCTYPE html> | |
<html lang="fr"><head> | |
<meta charset="utf-8"/> | |
<title>Résultats concours CNRS 2019</title> | |
<style> | |
body { | |
font-family: Helvetica, Arial, sans; | |
font-size: 10pt; | |
margin: 20px; | |
} | |
h2 { | |
margin-top: 50px; | |
} | |
tr { | |
background: #E0E0E0; | |
} | |
td { | |
padding: 5px 20px 5px 20px; | |
width: 50%; | |
} | |
td b { | |
color: #008000; | |
} | |
th { | |
padding: 5px 20px 5px 20px; | |
background: #E0E0FF; | |
} | |
details { | |
margin-left: 10px; | |
padding: 5px; | |
} | |
details p { | |
margin-left: 20px; | |
} | |
table { | |
margin-bottom: 10px; | |
} | |
</style> | |
</head><body> | |
<h1>Résultats concours CNRS 2019</h1> | |
<p> | |
Résultats des concours chercheurs CNRS 2019, aggrégés sur une seule page, | |
à partir du <a href="http://intersection.dsi.cnrs.fr/intersection/resultats-cc-fr.do">serveur | |
officiel</a>. | |
</p> | |
''') | |
concours = get_concours(sections) | |
for c, sec, grade in concours: | |
resultats_concours(c, sec, grade) | |
print('</body></html>') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment