Compare commits
1 Commits
master
...
test_train
Author | SHA1 | Date | |
---|---|---|---|
cefd3dc910 |
28
main.py
28
main.py
@ -17,11 +17,16 @@ async def main():
|
|||||||
trains = []
|
trains = []
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
tasks = []
|
tasks = []
|
||||||
for station in stations:
|
for station in stations[:10]:
|
||||||
tasks.append(fetch_station(station, session, trains))
|
tasks.append(fetch_station(station, session, trains))
|
||||||
await asyncio.gather(*tasks)
|
await asyncio.gather(*tasks)
|
||||||
|
|
||||||
print(trains)
|
print(trains)
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
tasks = []
|
||||||
|
for train in trains[:5]:
|
||||||
|
tasks.append(fetch_train(train, session))
|
||||||
|
await asyncio.gather(*tasks)
|
||||||
|
|
||||||
|
|
||||||
async def fetch_station(station: str, session: aiohttp.ClientSession, trains: list) -> None:
|
async def fetch_station(station: str, session: aiohttp.ClientSession, trains: list) -> None:
|
||||||
@ -59,6 +64,27 @@ async def fetch_station(station: str, session: aiohttp.ClientSession, trains: li
|
|||||||
except aiohttp.client_exceptions.ServerDisconnectedError:
|
except aiohttp.client_exceptions.ServerDisconnectedError:
|
||||||
print(f"station {station} failed ❎ ")
|
print(f"station {station} failed ❎ ")
|
||||||
|
|
||||||
|
async def fetch_train(train:str, session: aiohttp.ClientSession):
|
||||||
|
url = "http://www.belgianrail.be/jp/nmbs-realtime/trainsearch.exe/fn?ld=std&"
|
||||||
|
url_header = {'User-Agent': 'python script to fetch trains of belgium. (d.tonitch@gmail.com - in case it is a problem)',
|
||||||
|
'From': 'd.tonitch@gmail.com'}
|
||||||
|
url_data = {
|
||||||
|
'trainname':train.replace(" ", "+"),
|
||||||
|
'selectDate':'oneday',
|
||||||
|
'date':datetime.now().strftime('%d/%m/%Y'),
|
||||||
|
'productClassFilter':'85',
|
||||||
|
'start':'Chercher'
|
||||||
|
}
|
||||||
|
try:
|
||||||
|
async with session.post(url, data=url_data, headers=url_header) as resp:
|
||||||
|
if resp.status == 200:
|
||||||
|
print(f"train {train} success ✅")
|
||||||
|
soup = BeautifulSoup(await resp.text(), 'html.parser')
|
||||||
|
products = soup.body.find_all("td")
|
||||||
|
print(soup.body.prettify())
|
||||||
|
except aiohttp.client_exceptions.ServerDisconnectedError:
|
||||||
|
print(f"train {train} failed ❎ ")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
||||||
|
Loading…
Reference in New Issue
Block a user