这与rnd 参数无关。完全随机,由Math.random() js函数填充。
正如您所怀疑的,它大约是cookies。 PHPSESSID cookie 对于每个后续请求都至关重要。只需启动 requests.Session() 并将其用于您提出的每个请求:
Session 对象允许你在
要求。它还在从
会话实例。
...
# start session
session = requests.Session()
r = session.get(base_url)
soup = BeautifulSoup(r.text)
table = soup.find('table', {'id':'tableResultat'})
trs = table.findAll('tr')
for tr in trs:
span = tr.find('span')
cle = span.get('id')
url = 'https://j2c-com.com/Euronaval14/catalogueWeb/ajaxSociete.php?cle=' + cle + '&rnd=' + generate_random_number(0,9999999999999999)
pop = session.post(url) # <-- the POST request here contains cookies returned by the first GET call
print url
print pop.text
break
它会打印(查看 HTML 是否填满了所需的数据):
https://j2c-com.com/Euronaval14/catalogueWeb/ajaxSociete.php?cle=D000365D000365&rnd=0.1625497943120751
<table class='divAdresse'>
<tr>
<td class='ficheAdresse' valign='top'>Via Nazionale 54<br>IT-00184 - Roma<br><img
src='../../intranetJ2C/images/flags/IT.gif' style='margin-right:5px;'>ITALY<br><br>Phone: +39 06 488
0247 | Fax: +39 06 482 74 76<br><br>Website: <a href='http://www.aiad.it' target='_new'>www.aiad.it</a></td>
</tr>
</table>
<br>
<b class="divMarque">Contact:</b><br>
<font class="ficheAdresse"> Carlo Festucci - Secretary General<br>
<a href="mailto:c.festucci@aiad.it">c.festucci@aiad.it</a></font>
<br><br>
<div id='divTexte' class='ficheTexte'></div>
更新。
您没有在表格中获得其他参展商的结果的原因很难解释,但这里的重点是模拟当您单击浏览器中的行时在后台调用的所有后续 ajax 请求:
import random
import decimal
import requests
from bs4 import BeautifulSoup
base_url = 'https://j2c-com.com/Euronaval14/catalogueWeb/cataloguerecherche.php?listeFavoris=&typeRecherche=1&typeRechSociete=&typeSociete=&typeMarque=&typeDescriptif=&typeActivite=&choixSociete=&choixPays=&choixActivite=&choixAgent=&choixPavillon=&choixZoneExpo=&langue=gb&rnd=0.1410133063327521'
fiche_url = 'https://j2c-com.com/Euronaval14/catalogueWeb/fiche.php'
reload_url = 'https://j2c-com.com/Euronaval14/catalogueWeb/reload.php'
data_url = 'https://j2c-com.com/Euronaval14/catalogueWeb/ajaxSociete.php'
def generate_random_number(i,d):
"Produce a random between 0 and 1, with 16 decimal digits"
return str(decimal.Decimal('%d.%d' % (random.randint(0, i),random.randint(0, d))))
# start session
session = requests.Session()
r = session.get(base_url)
soup = BeautifulSoup(r.content)
for span in soup.select('table#tableResultat tr span'):
cle = span.get('id')
session.post(reload_url)
session.post(fiche_url, data={'page': 'page:catalogue',
'pasFavori': '1',
'listeFavoris': '',
'cle': cle,
'stand': '',
'rnd': generate_random_number(0, 9999999999999999)})
session.post(reload_url)
pop = session.post(data_url, data={'cle': cle,
'rnd': generate_random_number(0, 9999999999999999)})
print pop.text
打印:
<table class='divAdresse'><tr><td class='ficheAdresse' valign='top'>Via Nazionale 54<br>IT-00184 - Roma<br><img src='../../intranetJ2C/images/flags/IT.gif' style='margin-right:5px;'>ITALY<br><br>Phone: +39 06 488 0247 | Fax: +39 06 482 74 76<br><br>Website: <a href='http://www.aiad.it' target='_new'>www.aiad.it</a></td></tr></table><br><b class="divMarque">Contact:</b><br><font class="ficheAdresse"> Carlo Festucci - Secretary General<br><a href="mailto:c.festucci@aiad.it">c.festucci@aiad.it</a></font><br><br><div id='divTexte' class='ficheTexte'></div>
<table class='divAdresse'><tr><td class='ficheAdresse' valign='top'>An der Faehre 2<br>27809 - Lemwerder<br><img src='../../intranetJ2C/images/flags/DE.gif' style='margin-right:5px;'>GERMANY<br><br>Phone: +49 421 673 30 | Fax: +49 421 673 3115<br><br>Website: <a href='http://www.abeking.com' target='_new'>www.abeking.com</a></td></tr></table><br><b class="divMarque">Contact:</b><br><font class="ficheAdresse"> Thomas Haake - Sales Director Navy</font><br><br><div id='divTexte' class='ficheTexte'></div>
<table class='divAdresse'><tr><td class='ficheAdresse' valign='top'>Mohamed Bin Khalifa Street (street 15)<br>PO Box 107241<br>107241 - Abu Dhabi<br><img src='../../intranetJ2C/images/flags/AE.gif' style='margin-right:5px;'>UNITED ARAB EMIRATES<br><br>Phone: +971 2 445 5551 | Fax: +971 2 445 0644</td></tr></table><br><b class="divMarque">Contact:</b><br><font class="ficheAdresse"> Pierre Baz - Business Development<br><a href="mailto:pierre.baz@abudhabimar.com">pierre.baz@abudhabimar.com</a></font><br><br><div id='divTexte' class='ficheTexte'></div>
...