4 ms·
Seems to me you don't need regex, nor the call to list() at all for that free_food = [s for s in soup.stripped_strings if "free" in s.lower()] works
by column 10y ago
Seems to me you don't need regex, nor the call to list() at all for that
free_food = [s for s in soup.stripped_strings if "free" in s.lower()]
works
- js2 10y agoYou can also avoid building the list and terminate at the first true value with `any`: free_food = any(s for s in soup.stripped_strings if ...)
- tedmiston 10y agoWell on that note, if you just want the boolean, I think it'd be enough to say: free_food = 'free' in soup.get_text().lower()
- jamesbvaughan 10y agoOh even better. I'm kind of new to Python, so I hadn't seen that before!
- dmn001 10y agoRight, if you're just matching a substring in a list of strings then you don't need regex for that. Also, lxml is over 20x faster than BeautifulSoup, although it doesn't really matter with such a low frequency of requests, it's something to note if you had another project that required parsing e.g. 1,000's of html pages.