from bs4 import BeautifulSoup as bs import requests, sys, re from termcolor import colored if len(sys.argv) < 2: print colored("Falta o argumento palavra: Tente 'python main.py palavra'", 'red') exit() elif len(sys.argv) > 2: print colored("O programa so toma apenas um argumento, o primeiro argumento sera utilizado.", 'red') page = requests.get("http://dictionary.cambridge.org/us/dictionary/english/" + sys.argv[1]) soup = bs(page.text, "html.parser") key = 0 for options in soup.find_all("div", { "class" : "di-head" }): print str(key) + ' - ' + re.sub(r'[\ \n]{2,}', '', options.h2.text) key += 1 opc = input("Escolha uma opcao:") if opc > key - 1: print colored("Opcao invalida.", 'red') exit() key_2 = 0 for forms in soup.find_all("div", {"class":"di-body"}): if key_2 == opc: for mode in forms.find_all("div", {"class":"sense-block"}): print colored('\n' + re.sub(r'[\ \n]{2,}', '', mode.h4.text), 'green') for text in mode.find_all('div', {'class':'def-block pad-indent'}): print re.sub(r'[\ \n]{2,}', '', text.text) break key_2 += 1