答案1
curl请求如下(可以在浏览器开发者工具的网络部分获取):
curl 'https://www.concours-maths-cpge.fr/' -H 'Cookie: startBAK=xxxxxxxxxxxxxxxx; start=xxxxxxxxxxxx' -H 'Origin: https://www.concours-maths-cpge.fr' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.8' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36' -H 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryBj169hPosWDbuaq7' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' -H 'Cache-Control: max-age=0' -H 'Referer: https://www.concours-maths-cpge.fr/' -H 'Connection: keep-alive' --data-binary $'------WebKitFormBoundaryBj169hPosWDbuaq7\r\nContent-Disposition: form-data; name="cmd"\r\n\r\n\r\n------WebKitFormBoundaryBj169hPosWDbuaq7\r\nContent-Disposition: form-data; name="mode"\r\n\r\nsimple\r\n------WebKitFormBoundaryBj169hPosWDbuaq7\r\nContent-Disposition: form-data; name="annee"\r\n\r\n2017\r\n------WebKitFormBoundaryBj169hPosWDbuaq7\r\nContent-Disposition: form-data; name="concours"\r\n\r\n0\r\n------WebKitFormBoundaryBj169hPosWDbuaq7\r\nContent-Disposition: form-data; name="filiere"\r\n\r\n0\r\n------WebKitFormBoundaryBj169hPosWDbuaq7\r\nContent-Disposition: form-data; name="matiere"\r\n\r\n0\r\n------WebKitFormBoundaryBj169hPosWDbuaq7\r\nContent-Disposition: form-data; name="epreuve"\r\n\r\n0\r\n------WebKitFormBoundaryBj169hPosWDbuaq7\r\nContent-Disposition: form-data; name="commande"\r\n\r\nrechercher\r\n------WebKitFormBoundaryBj169hPosWDbuaq7--\r\n' --compressed
卷曲后的数据为:
<head><title>Sujets de concours</title>
<link rel='stylesheet' href='javascript/table.css' type='text/css' />
<script language='javascript' src='javascript/table.js'></script>
</head>
<body background='images/julia.gif'>
<div align=left><table style='background:none'><tr>
<td><img src='images/ups.gif'>
<td><h1>Union des Professeurs de classes préparatoires Scientifiques<br>Sujets de concours
</tr></table></div>
<div align='center'><form enctype='multipart/form-data' method='post'>
<input type='hidden' name='cmd' value=''/>
<input type='hidden' name='mode' value='simple'/>
<input type='hidden' name='annee' value='2017' />
<input type='hidden' name='ordre' value='-annee:+nom:+filiere:+matiere:+epreuve' />
<table border=2 rules='none' style='table-layout:fixed' width='90%'><tr><td><button title='tri croissant' style='width:100%' onclick='mysubmit(this,"commande=rechercher|ord=+annee")'>année</button><td><button title='placer à gauche' style='width:100%' onclick='mysubmit(this,"commande=rechercher|ord=+nom")'>concours</button><td><button title='placer à gauche' style='width:100%' onclick='mysubmit(this,"commande=rechercher|ord=+filiere")'>filière</button><td><button title='placer à gauche' style='width:100%' onclick='mysubmit(this,"commande=rechercher|ord=+matiere")'>matière</button><td><button title='placer à gauche' style='width:100%' onclick='mysubmit(this,"commande=rechercher|ord=+epreuve")'>épreuve</button><td style='width:5em; padding:0px'></tr>
<tr class='even'><td class='rowspan' rowspan=10><a title='2017'>2017</a><td class='rowspan' rowspan=2><a title='Agrégation externe'>Agr-ext</a><td class='rowspan' rowspan=2><a title='M1'>M1</a><td class='rowspan' rowspan=2><a title='maths'>maths</a><td><a title='algèbre'>algèbre</a><td><button title='fichiers disponibles' style='width:100%' onclick='mysubmit(this,"commande=rechercher|init=0|numero=m17a4a")'>fichiers</button></tr>
<tr class='odd'><td><a title='analyse'>analyse</a><td><button title='fichiers disponibles' style='width:100%' onclick='mysubmit(this,"commande=rechercher|init=0|numero=m17a4y")'>fichiers</button></tr>
<tr class='even'><td><a title='Agrégation concours spécial'>Agr-special</a><td><a title='autre'>autre</a><td><a title='maths'>maths</a><td><a title='unique'>unique</a><td><button title='fichiers disponibles' style='width:100%' onclick='mysubmit(this,"commande=rechercher|init=0|numero=m17azu")'>fichiers</button></tr>
<tr class='odd'><td class='rowspan' rowspan=2><a title='Agrégation interne'>Agr-int</a><td class='rowspan' rowspan=2><a title='autre'>autre</a><td class='rowspan' rowspan=2><a title='maths'>maths</a><td><a title='1'>1</a><td><button title='fichiers disponibles' style='width:100%' onclick='mysubmit(this,"commande=rechercher|init=0|numero=m17bz1")'>fichiers</button></tr>
<tr class='even'><td><a title='2'>2</a><td><button title='fichiers disponibles' style='width:100%' onclick='mysubmit(this,"commande=rechercher|init=0|numero=m17bz2")'>fichiers</button></tr>
<tr class='odd'><td class='rowspan' rowspan=2><a title='Banque Agro-Véto'>Agro</a><td class='rowspan' rowspan=2><a title='BCPST'>BCPST</a><td class='rowspan' rowspan=2><a title='maths'>maths</a><td><a title='A'>A</a><td><button title='fichiers disponibles' style='width:100%' onclick='mysubmit(this,"commande=rechercher|init=0|numero=m17ab1")'>fichiers</button></tr>
<tr class='even'><td><a title='B'>B</a><td><button title='fichiers disponibles' style='width:100%' onclick='mysubmit(this,"commande=rechercher|init=0|numero=m17ab2")'>fichiers</button></tr>
<tr class='odd'><td class='rowspan' rowspan=3><a title='Concours Commun Polytechniques'>CCP</a><td class='rowspan' rowspan=3><a title='MP'>MP</a><td><a title='info'>info</a><td><a title='option'>option</a><td><button title='fichiers disponibles' style='width:100%' onclick='mysubmit(this,"commande=rechercher|init=0|numero=i17pmo")'>fichiers</button></tr>
<tr class='even'><td class='rowspan' rowspan=2><a title='maths'>maths</a><td><a title='1'>1</a><td><button title='fichiers disponibles' style='width:100%' onclick='mysubmit(this,"commande=rechercher|init=0|numero=m17pm1")'>fichiers</button></tr>
<tr class='odd'><td><a title='2'>2</a><td><button title='fichiers disponibles' style='width:100%' onclick='mysubmit(this,"commande=rechercher|init=0|numero=m17pm2")'>fichiers</button></tr>
<tr><td colspan=5><table width='100%'><tr>
<td>sujets 1 à 10 (total = 63)
<td align='right'><button title='page suivante' onclick='mysubmit(this,"commande=rechercher|init=10")'>></button></tr></table>
<td><button title='modifier la recherche' style='width:100%' onclick='mysubmit(this,"commande=connexion")'>retour</button>
</tr></table>
</form></div></body>
并且没有可使用 wget 的 pdf 链接。因此,使用 ajax,点击时使用 javascript 生成 pdf 链接。
唯一的解决办法是硒网络驱动程序它基本上自动化了浏览器中的交互动作。您可以将 chrome/chormium 或 firefox 与 selenium 结合使用(当您的代码与 Selenium 一起运行时,打开的浏览器窗口会自动执行点击),但最好的方法是使用无头浏览器,例如PhantomJS与硒。
以下是一些示例链接以及有关使用 Selenium(和 PhantomJS)进行抓取的一些问题:
使用 Python Selenium 和 PhantomJS 进行抓取
使用 Python 和 PhantomJS 进行无头 Selen 测试