Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import config
- import telebot
- import requests
- from bs4 import BeautifulSoup
- token = '' # paste your own token
- bot = telebot.TeleBot(token)
- html_file_path = 'file_to_parse.html'
- @bot.message_handler(content_types=["text"])
- def send_answer(message):
- str_msg = ''
- answer = {}
- msg = message.text.split(' ')
- for word in msg:
- answer[word] = parse_datafile(word)
- for key in answer.keys():
- str_msg += '\n'
- str_msg = str_msg + key + '\n'
- str_msg += '\n'
- for wrd in answer[key]:
- str_msg = str_msg + wrd + '\n'
- bot.send_message(message.chat.id, str_msg)
- def load_data(word):
- url = 'https://slovnik.seznam.cz/ru/?q=%s' % (word)
- r = requests.get(url)
- with open(html_file_path, 'w') as output_file:
- output_file.write(r.text)
- def read_file(filename):
- with open(filename) as input_file:
- text = input_file.read()
- return text
- def parse_datafile(word):
- lines = []
- results = []
- synonym = ''
- i = 1
- answer = load_data(word)
- text = read_file(html_file_path)
- soup = BeautifulSoup(text, features="lxml")
- word_list = soup.find('div', {'id': 'fastMeanings'})
- try:
- for string in word_list.strings:
- lines.append(repr(string))
- while(i < len(lines)):
- if(lines[i-1] == lines[i]):
- synonym = synonym.replace('\'', '')
- results.append(synonym)
- synonym = ''
- i += 1
- else:
- synonym = synonym + lines[i] + ' '
- i += 2
- return(results)
- except:
- print('sth goes wrong')
- return([])
- if __name__ == '__main__':
- bot.polling(none_stop=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement