Skip to content

Instantly share code, notes, and snippets.

@mvrozanti
Last active October 24, 2021 00:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvrozanti/ee48e7000dc70236ea974a9f2a0a4dd5 to your computer and use it in GitHub Desktop.
Save mvrozanti/ee48e7000dc70236ea974a9f2a0a4dd5 to your computer and use it in GitHub Desktop.
Consulta de sinônimos e antônimos de uma palavra no português brasileiro
#!/usr/bin/env python
from bs4 import BeautifulSoup
import requests
SIN_URL = 'https://www.sinonimos.com.br/'
ANT_URL = 'https://www.antonimos.com.br/'
def main(palavra):
sess = requests.session()
# sinônimos
le_html = sess.get(SIN_URL + palavra, timeout=10).text
bs = BeautifulSoup(le_html, 'lxml')
s_wrapper_divs = bs.find_all('div', {'class':'s-wrapper'})
for div in s_wrapper_divs:
div_sentido = div.find('div', {'class':'sentido'})
if div_sentido:
print(div_sentido.text)
sinonimos = div.find_all('span') + div.find_all('a')
txt_sinonimos = ''
for sinonimo in sinonimos:
if sinonimo.text:
if (sinonimo.has_attr('class') and sinonimo['class'] == ['sinonimo']) \
or not sinonimo.has_attr('class'):
txt_sinonimos += sinonimo.text + ', '
print('\t' + txt_sinonimos[:-2] + '.\n')
# antônimos
print('\nAntônimos:\n')
le_html = sess.get(ANT_URL + palavra, timeout=10).text
bs = BeautifulSoup(le_html, 'lxml')
s_wrapper_divs = bs.find_all('div', {'class':'s-wrapper'})
for div in s_wrapper_divs:
# txt_ants = div.text
# if ':' in txt_ants:
# txt_ants = txt_ants[[txt_ants.index(':') + 1]:]
print('\t' + div.text + '\n')
if __name__ == '__main__':
import sys
if len(sys.argv) > 1: main(sys.argv[1])
else: print('Uso: sinon <palavra>')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment