diff --git a/cogs/google.py b/cogs/google.py index 9248318..87b1650 100644 --- a/cogs/google.py +++ b/cogs/google.py @@ -24,11 +24,9 @@ class Google(commands.Cog): if results is None: return await ctx.send("Er ging iets fout (Response {})".format(status)) + # Filter out all Nones elements = list(filter(lambda x: x is not None, results)) - if len(elements) > 10: - elements = elements[:10] - embed = discord.Embed(colour=discord.Colour.blue()) embed.set_author(name="Google Search") @@ -37,6 +35,10 @@ class Google(commands.Cog): embed.description = "Geen resultaten gevonden." return await ctx.reply(embed=embed, mention_author=False) + # Cut excess results out + if len(elements) > 10: + elements = elements[:10] + links = [] for index, (link, title) in enumerate(elements): diff --git a/functions/scraping.py b/functions/scraping.py index 7b41bce..cde779f 100644 --- a/functions/scraping.py +++ b/functions/scraping.py @@ -24,8 +24,12 @@ def google_search(query): bs = BeautifulSoup(resp.text, "html.parser") def getContent(element): + """ + Function to find links & titles in the HTML of a
element + """ link = element.find("a", href=True) title = element.find("h3") + if link is None or title is None: return None @@ -35,6 +39,7 @@ def google_search(query): return None return link["href"], sp.text + divs = bs.find_all("div", attrs={"class": "g"}) return list(getContent(d) for d in divs), 200