Pytelegrambotapi直列Google搜索引擎

发布于 2025-01-21 16:30:23 字数 1296 浏览 4 评论 0原文

@bot.inline_handler(func=lambda query: len(query.query) > 0)
    def query_text(query):
        sleep(6)
        text=query.query
        html=requests.get(f'https://google.com/search?q={text}')
        # print(html.status_code)
        open('index.html','w', encoding='utf-8').write(html.text)
        soup=BeautifulSoup(html.text, 'html.parser').find_all('div',{"class":"***********"})
        for i in soup:
            fk.append(types.InlineQueryResultArticle(id=str(len(fk)), title=f"{i.find('h3').get_text()}",description=f"{i.find('div',{'class':'**********'}).get_text()}",input_message_content=types.InputTextMessageContent(message_text=i.find('a').get('href').replace('/url?q=','https://google.com/url?q=')),hide_url=True,url=i.find('a').get('href').replace('/url?q=','https://google.com/url?q='),thumb_url='https://w7.pngwing.com/pngs/338/520/png-transparent-g-suite-google-play-google-logo-google-text-logo-cloud-computing.png', thumb_width=30, thumb_height=30))
            print(i.find('a').get('href').replace('/url?q=','')+'\n')
        sleep(2)
        bot.answer_inline_query(query.id, fk)

当我编写@bot Google请求时
机器人将其视为G GO GOOGEE
是什么导致错误
“对电报API的请求不成功。错误代码:400。描述:不良请求:查询太旧了,响应超时过期或查询ID无效”
如何制作文本输入超时,以免对每个字母响应?

@bot.inline_handler(func=lambda query: len(query.query) > 0)
    def query_text(query):
        sleep(6)
        text=query.query
        html=requests.get(f'https://google.com/search?q={text}')
        # print(html.status_code)
        open('index.html','w', encoding='utf-8').write(html.text)
        soup=BeautifulSoup(html.text, 'html.parser').find_all('div',{"class":"***********"})
        for i in soup:
            fk.append(types.InlineQueryResultArticle(id=str(len(fk)), title=f"{i.find('h3').get_text()}",description=f"{i.find('div',{'class':'**********'}).get_text()}",input_message_content=types.InputTextMessageContent(message_text=i.find('a').get('href').replace('/url?q=','https://google.com/url?q=')),hide_url=True,url=i.find('a').get('href').replace('/url?q=','https://google.com/url?q='),thumb_url='https://w7.pngwing.com/pngs/338/520/png-transparent-g-suite-google-play-google-logo-google-text-logo-cloud-computing.png', thumb_width=30, thumb_height=30))
            print(i.find('a').get('href').replace('/url?q=','')+'\n')
        sleep(2)
        bot.answer_inline_query(query.id, fk)

When I write @bot google request

Bot takes it as g go goo google

What is causing the error

"A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: query is too old and response timeout expired or query ID is invalid"

How to make text input timeout so that it doesn't respond to every letter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巾帼英雄 2025-01-28 16:30:23

我认为,错误在于您解析数据的方式。为了获得答案方法,至少需要8秒钟(基于睡眠)。电报内联查询只有几秒钟的时间,直到被认为是旧的,因此,在调用bot.answer_inline_query()之后,最好处理数据,然后使用bot.send_message( )

我不确定它如何与异步代码一起使用。
如果您找到了另一个解决方案,请告诉我:)

I think, the error resides in your way of parsing data. It takes at least 8 seconds (based on sleeps) just to get to the answer method. Telegram inline queries have very few seconds until they are considered old, so, it is better to process data after you call bot.answer_inline_query() and then send it to user using bot.send_message()

I am not certain how it works with async code though.
If you find another solution, please let me know :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文