Slthy / Microplus-Lenex

Web scraper and Lenex encoder for swimming events hosted by Microplus s.r.l
MIT License
1 stars 0 forks source link

Bug-prone url parsing #17

Closed CremaLuca closed 1 year ago

CremaLuca commented 1 year ago

Nella funzione swrid fai questo replace() https://github.com/Slthy/Microplus-Lenex/blob/9e0c07df9bea626b55bf0dc7c8c8e563c21bbd53/utils.py#L86

Se però l'URL in href dovesse aggiungere altri query params o dovesse cambiarne l'ordine il tuo codice si romperebbe. Se ho capito bene vuoi salvare l'athleteId dall'url, consiglio soluzione con urlparse

import urlparse
...
url = BeautifulSoup(html_res.text, features="html.parser").find('a', href=True)['href']
return urlparse.parse_qs(urlparse.urlparse(url).query).get('athleteId', [None])[0]

Puoi gestire come credi meglio tu il valore di default/errore nel caso non ci sia athleteId, se tieni None specifica che la funzione restituisce un tipo Optional[str] (o int, non so)

Slthy commented 1 year ago

yep, questa soluzione è sicuramente meglio. Per ora lascio da parte questa funzionalità, visto che non so neanche se serva veramente.