Compare commits
No commits in common. "test_train" and "master" have entirely different histories.
test_train
...
master
28
main.py
28
main.py
@ -17,16 +17,11 @@ async def main():
|
||||
trains = []
|
||||
async with aiohttp.ClientSession() as session:
|
||||
tasks = []
|
||||
for station in stations[:10]:
|
||||
for station in stations:
|
||||
tasks.append(fetch_station(station, session, trains))
|
||||
await asyncio.gather(*tasks)
|
||||
|
||||
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:
|
||||
@ -64,27 +59,6 @@ async def fetch_station(station: str, session: aiohttp.ClientSession, trains: li
|
||||
except aiohttp.client_exceptions.ServerDisconnectedError:
|
||||
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__":
|
||||
asyncio.run(main())
|
||||
|
Loading…
Reference in New Issue
Block a user