#!/usr/bin/env python3 """ Scrapes and shows information about Top Level Domains. """ import bs4 import requests from module import commands, example URI = 'https://en.wikipedia.org/wiki/List_of_Internet_top-level_domains' @commands('tld') @example('.tld me') def gettld(bot, trigger): """Show information about the given Top Level Domain.""" word = trigger.group(2).strip() if " " in word: return bot.reply("One TLD at a time.") if not word.startswith("."): word = "." + word res = requests.get(URI, verify=True) res.raise_for_status() soup = bs4.BeautifulSoup(res.text, "html.parser") td = soup.find("td", string=word) if not td: return bot.say(f"Unable to find data for TLD: {word}") table_headers = [th.string for th in td.parent.parent.find_all("th")] if None in table_headers: n = table_headers.index(None) table_headers[n] = "Administrator" # this should be the only header on the page where th.string fails table_entries = [] for td in td.parent.find_all("td"): string = td.text.strip() if not string: try: string = td.a.string except AttributeError: string = "" table_entries.append(string) msg = "[\x0304TLD\x03] " for n in range(len(table_headers)): msg += f"\x0310{table_headers[n]}\x03: " msg += f"\x0312{table_entries[n]}\x03 | " msg = msg[:-3] bot.msg(msg)