I was working on one telgram bot, and when finally i made everything i discovered that it is extremely slow. def s takes 5-7 seconds to get result and send it to user, i tried to optimise it with methods from other sites, but it didn't work. How can i optimise it?

import telebot
import pyscp
from googlesearch import search
scp = "scp-"
bot = telebot.TeleBot("NO")
def extract_arg(arg):
    return arg.split()[1:]
def o(message):
   global status
   status = extract_arg(message.text)
    object = status[0]
   except Exception as e:
    object ="7777"
   l = scp + object
   url = "scpfoundation.net/" + l
   ru_wiki = pyscp.wikidot.Wiki('scpfoundation.net')
   p = ru_wiki(l)
    k = ('{}'.format(p.title))
    text = (f'<a href="{url}">{k}</a>')
   except Exception as e:
    text="Простите, этот номер не присвоен не одному из объектов"
   bot.send_message(message.chat.id, text,parse_mode='HTML')
def extract_argument(argument):
    return argument.split()[3:]
def s(m):
    status1 = m.text
    status2 = status1.replace('/s', "")
    f = open("base.txt", "r")
    searchlines = [line.strip() for line in f.              readlines() if line.strip()]
    out = []
        for i, line in enumerate(searchlines):
                if status2.lower() in line.lower():
                            for l in searchlines[i : i + 1]:
    except Exception as e:
        bot.send_message(m.chat.id, "Простите, не смог ничего найти.",parse_mode='HTML')
    finalout = list(set(out))
    number = len(finalout)
    g, nm, count, count1, gey =[], int("0"), int("0"), int("0"), []
    while (nm<number):
        url = 'http://scpfoundation.net/'
            ru_wiki = pyscp.wikidot.Wiki('scpfoundation.net')
            p = ru_wiki(finalout[count])
            k = ('{}'.format(p.title))
            result = " ".join ([url, finalout[count]])
            g.append(f'<a href="{result}">{k}</a>')
        except Exception as e:
    ka = search(f'{status2} site:scpfoundation.net', num_results=4)
    while (numbeer<5):
            ru_wiki = pyscp.wikidot.Wiki('scpfoundation.net')
            p = ru_wiki(ka[counter])
            kj = ('{}'.format(p.title))
            if (kj not in gey and "forum" not in ka[counter] and "draft" not in ka[counter] and "fragment" not in ka[counter]):
                    result = ka[counter]
                    g.append(f'<a href="{result}">{kj}</a>')
        except Exception as e:
    story = '\n'.join(g)
        bot.send_message(m.chat.id, story,parse_mode='HTML')
    except Exception as e:
        bot.send_message(m.chat.id, "Простите, ничего не найдено.", parse_mode='HTML')
def help(t):
    bot.send_message(t.chat.id, "/o — поиск по номеру; /s — поиск по названию; /help — это сообщение; /join — присоеденится к сообществу; /faq — ответы на частые вопросы",parse_mode='HTML')
def join(j):
    joiner=(f'<a href="http://scpfoundation.net/system:join">Подай простую заявку!</a>')
    bot.send_message(j.chat.id, joiner,parse_mode='HTML')
def faq(f):
    faqer=(f'<a href="http://scpfoundation.net/faq">Читать тут.</a>')
    bot.send_message(f.chat.id, faqer,parse_mode='HTML')

The problem is with while (numbeer<5) 5 is number of urls i grab from google search, and less urls i grab less times it need. I can't reduce the numbers of urls, so maybe i can optimise another part of code to reduce the time?

  • \$\begingroup\$ What does the code do? Why does a "telegram bot" need to fetch 5 urls ? \$\endgroup\$
    – user985366
    Commented Jun 4, 2021 at 14:27

1 Answer 1


There are a number of issues, but I'm guessing that the major slowdown in s() is due to the unnecessarily repeated calls to pyscp.wikidot.Wiki('scpfoundation.net').

This web request is done once for each match in searchlines and then done 5 more times again. I'm assuming this is the exact same data retrieved every time.

You should do this only once in s() at most in my opinion then use the saved result inside your while loops.

  • \$\begingroup\$ What are other issues? This answer helped me very much \$\endgroup\$ Commented Jun 4, 2021 at 15:42
  • \$\begingroup\$ When you enumerate(searchlines) in s() for example, you have a nested for loop that returns you l but you already know l as line from the outer loop, So that inner for is not likely doing anything to help. (unless of course I have misinterpreted things) \$\endgroup\$
    – JonSG
    Commented Jun 4, 2021 at 15:56
  • \$\begingroup\$ It helps me to split line in right way, but thx \$\endgroup\$ Commented Jun 4, 2021 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.