在python第1部分中使用Selenium和Telebot预约签证预约

In this tutorial, I want to share my experience in creating Appointment_Bot, my first telegram bot for an automation task. Here, I suppose that you are not familiar with bots and their atmosphere. Still, you know to code in python and, more important, you have ambitions in discovering the new world of web scraping and sharing the results of your analysis in some social media like Telegram. Now, I dive in code explanation, and I want to tell you why Appointment_Bot? Let’s go.

在本教程中,我想分享我创建Appointment_Bot(我的第一个用于自动化任务的电报机器人)的经验。 在这里,我想您对机器人及其氛围并不熟悉。 尽管如此,您仍然知道要使用python进行编码,更重要的是,您有雄心勃勃地发现网络抓取的新世界,并在某些社交媒体(例如Telegram)中共享分析结果。 现在,我将深入探讨代码说明,并想告诉您为什么选择Appointment_Bot? 我们走吧。

During 2019, I decided to continue my study and research as a Ph.D. student. So, I got an admission of Electrical Engineering. Meanwhile, at the beginning of 2020, the world faced a global problem, COVID 19 pandemic. If you have tried to get an appointment for Biometric, you confirm that the website is awful in its speed and showing different errors during the date booking process. This is not the whole story. On the other hand, appointment times are opened suddenly, and if you are not ready for the booking process, you will lose the battle (It means that the website is not friendly with phone’s browsers and you should have access to a laptop or PC).

在2019年期间,我决定继续攻读博士学位。 学生。 因此,我被电机工程专业录取。 同时,在2020年初,世界面临全球性问题,即COVID 19大流行。 如果您想预约生物识别技术,请确认该网站的速度糟糕,并且在日期预订过程中显示出不同的错误。 这还不是全部。 另一方面,约会时间突然打开,如果您还没有准备好进行预订,那么您将输掉这场战斗(这意味着该网站与手机的浏览器不友好,您应该可以使用笔记本电脑或PC) 。

With the help of *Appointment_Bot*, users open their Telegram and start the booking process in their smartphones. The bot gets the required information and logins into the user’s account. Then, it passes all steps to reach the final calendar of open days for an appointment. During the process, the bot notifies the user if it gets any error, or passes each step. Appointment_Bot uses selenium to act like a real user, from waiting between each action to click on “Submit” and “Continue” buttons.

借助* Appointment_Bot *,用户可以打开他们的电报并在智能手机上开始预订过程。 该机器人会获取所需的信息并登录到该用户的帐户。 然后,它通过所有步骤以达到约会的开放日的最终日历。 在此过程中,漫游器会在出现任何错误或通过每个步骤时通知用户。 Appointment_Bot使用Selenium来充当真实用户,从等待每个动作之间单击“ Submit ”和“ Continue ”按钮。

通过阅读本教程,您将知道如何 (By reading this tutorial, you will know how to)

1- Create a Telegram bot using BotFather.

1-使用BotFather创建一个Telegram机器人。

2- Check Gmail account for new message update.

2-检查Gmail帐户是否有新邮件更新。

3- Work with bs4 to extract target data from HTML and CSS code of a website.

3-与bs4一起从网站HTML和CSS代码中提取目标数据。

4- Work with Selenium to execute an automated operation in the human behavior level.

4-与Selenium一起在人类行为水平上执行自动化操作。

5- Work with Telebot, a framework for Telegram Bot API, to handle different user’s input commands.

5-使用Telebot (Telegram Bot API的框架)来处理不同用户的输入命令。

To start, first, you need to start a conversation with this ID: @BotFather. Next, you insert /newbot command, and BotFather creates a new bot for you. But just before you complete the process, you have to give some information about your bot, like name and username. Finally, you will receive your bot’s TOKEN id. Read this conversation between BotFather and me.

首先,您需要使用以下ID开始对话: @BotFather 。 接下来,您插入 / newbot命令,然后BotFather为您创建一个新的机器人。 但是在完成此过程之前,您必须提供一些有关您的机器人的信息,例如名称和用户名。 最后,您将收到机器人的令牌 ID。 阅读我和BotFather之间的对话。

Image for post
Create your bot with BotFather
使用BotFather创建您的机器人

Now, you have the required data to start your coding. To have a better understanding of Appointment_Bot’s procedure, first, see the picture below.

现在,您已具有开始编码所需的数据。 为了更好地了解Appointment_Bot的过程,请首先参见下图。

Image for post
A schematic of all steps from getting data to check open days for booking an appointment
从获取数据到检查预约开放天的所有步骤的示意图

When the user inserts /start, the message handler of Telebot finds this command and runs send_welcome function.

当用户插入/ start时 ,Telebot的消息处理程序将找到此命令并运行send_welcome函数。

In this function, an inline keyboard button is created, named Booking Appointment, and whenever the user press this button, a query is sent to vac_callback function.

在此功能中,将创建一个名为Booking Appointment的嵌入式键盘按钮,每当用户按下该按钮时,就会向vac_callback函数发送查询。

Here, the user must select which center wants to book an appointment, Ankara, or Istanbul. For this purpose, two inline keyboard buttons are designed for cities. If the user presses each button, a proper function is called to handle the generated query.

在这里,用户必须选择哪个中心要预订约会,安卡拉或伊斯坦布尔。 为此,为城市设计了两个嵌入式键盘按钮。 如果用户按下每个按钮,则会调用适当的函数来处理生成的查询。

Also, a user class is defined to save some important parameters of each user such as password and username of the account, Gmail password if the username is a Gmail type of email and the user allows the bot to access his/her Gmail automatically.

此外,定义了一个用户类来保存每个用户的一些重要参数,例如帐户的密码和用户名,如果用户名是Gmail类型的电子邮件,并且用户允许漫游器自动访问其Gmail,则Gmail密码。

重要提示:如果要发送消息以从用户那里获得特定输入,则最好使用这种格式。 首先,僵尸程序发送一条消息,例如,指示用户如何输入用户名和密码。 然后,输入数据将传递到下一个消息处理程序以进行进一步处理。 参见示例。 (Important tip: If you want to send a message to get a specific input from the user, it is better to use such a format. First, the bot sends a message, for example, to instruct the user how to insert a username and password; then, the input data is passed to the next message handler for further process. See example.)

Next, the given username is checked, and if is a Gmail type, the bot asks the user to get the permission of his/her email access to get OTP. OTP is a one time password, which is sent by the website and has time limitation for use. Input data must be sent in separate rows, and otherwise, the bot sends a warning message.

接下来,检查给定的用户名,如果是Gmail类型,则该漫游器会要求用户获得其电子邮件访问权限以获取OTP。 OTP是一次性密码,由网站发送,使用时限。 输入数据必须在单独的行中发送,否则,机器人会发送警告消息。

In response, based on the user’s answer, two call_backs are called to handle queries.

作为响应,基于用户的答案,将调用两个call_backs来处理查询。

In the booking function, get_captcha is called to take a screenshot of the captcha image. The bot posts captcha and waits for the user’s input. Then, it calls update_captcha to process the inserted captcha number.

预订功能中, 调用get_captcha以获取验证码图像的屏幕截图。 机器人发布验证码并等待用户输入。 然后,它调用update_captcha处理插入的验证码编号。

Here, to find the elements of the webpage, first right click on the page and select inspect. Then, in elements section of the appeared window on the right side of the page, find your element like this.

在这里,要查找网页的元素,请首先右键单击该页面,然后选择检查。 然后,在页面右侧出现的窗口的elements部分中,找到您的这样的元素。

Image for post
Image for post

Please read the continue in Part 2

请阅读第二部分的继续

Thank you :)

谢谢 :)

翻译自: https://medium.com/@amir.94.eng_72581/booking-a-visa-appointment-using-selenium-and-telebot-in-python-part-1-bbad66503a8d

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值