Advertisement
JB1n

Untitled

Oct 15th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.78 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import config
  3. import telebot
  4. import requests
  5. from bs4 import BeautifulSoup
  6.  
  7. token = '' #  paste your own token
  8.  
  9. bot = telebot.TeleBot(token)
  10. html_file_path = 'file_to_parse.html'
  11.  
  12.  
  13. @bot.message_handler(content_types=["text"])
  14. def send_answer(message):
  15.     str_msg = ''
  16.     answer = {}
  17.     msg = message.text.split(' ')
  18.  
  19.     for word in msg:
  20.         answer[word] = parse_datafile(word)
  21.     for key in answer.keys():
  22.         str_msg += '\n'
  23.         str_msg = str_msg + key + '\n'
  24.         str_msg += '\n'
  25.         for wrd in answer[key]:
  26.             str_msg = str_msg + wrd + '\n'
  27.  
  28.     bot.send_message(message.chat.id, str_msg)
  29.  
  30.  
  31. def load_data(word):
  32.     url = 'https://slovnik.seznam.cz/ru/?q=%s' % (word)
  33.     r = requests.get(url)
  34.     with open(html_file_path, 'w') as output_file:
  35.         output_file.write(r.text)
  36.  
  37.  
  38. def read_file(filename):
  39.     with open(filename) as input_file:
  40.         text = input_file.read()
  41.     return text
  42.  
  43.  
  44. def parse_datafile(word):
  45.     lines = []
  46.     results = []
  47.     synonym = ''
  48.     i = 1
  49.  
  50.     answer = load_data(word)
  51.     text = read_file(html_file_path)
  52.     soup = BeautifulSoup(text, features="lxml")
  53.     word_list = soup.find('div', {'id': 'fastMeanings'})
  54.     try:
  55.         for string in word_list.strings:
  56.             lines.append(repr(string))
  57.         while(i < len(lines)):
  58.             if(lines[i-1] == lines[i]):
  59.                 synonym = synonym.replace('\'', '')
  60.                 results.append(synonym)
  61.                 synonym = ''
  62.                 i += 1
  63.             else:
  64.                 synonym = synonym + lines[i] + ' '
  65.                 i += 2
  66.         return(results)
  67.     except:
  68.         print('sth goes wrong')
  69.         return([])
  70.  
  71. if __name__ == '__main__':
  72.     bot.polling(none_stop=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement