女朋友让我深夜十二点催她睡觉,我有Python我就不干

事情是这样的:今晚,我的女朋友让我催促她12点上床睡觉。

[En]

Here’s the thing: tonight, my girlfriend asked me to urge her to bed at 12:00.

但是我太困了,来不及了。对吗?对女朋友来说,睡觉有多重要?

[En]

But I was so sleepy that I couldn’t make it. Right? How is it important for a girlfriend to sleep?

然而,我不敢违抗我女朋友的命令。

[En]

However, I dare not disobey my girlfriend’s orders.

但是睡觉也不能缺!

这时候我们该怎么办呢?是时候让Python登场了!

干货主要有:

① 200 多本 Python 电子书(和经典的书籍)应该有

② Python标准库资料(最全中文版)

③ 项目源码(四五十个有趣且可靠的练手项目及源码)

④ Python基础入门、爬虫、网络开发、大数据分析方面的视频(适合小白学习)

⑤ Python学习路线图(告别不入流的学习)

Python学习Q群101677771

Python登场

这一次,让我们做一个程序,让微信在晚上12点自动给女朋友发一条信息。这也可以被视为男友的责任。

[En]

This time, let’s do a program to send Wechat automatically to send a message to our girlfriend at 12:00 at night. It can also be regarded as a boyfriend’s duty.

安装和导入

我们需要两个模块: apscheduler , pyautogui

快捷键 Windows+r 打开运行控制框,输入 cmd,进入命令行,输入:

<span class="hljs-attr">pip <span class="hljs-string">install apscheduler
<span class="hljs-attr">pip <span class="hljs-string">install pyautogui</span></span></span></span>

导入:

<span class="hljs-keyword">import pyautogui
<span class="hljs-keyword">from datetime <span class="hljs-keyword">import datetime
<span class="hljs-keyword">from apscheduler.schedulers.blocking <span class="hljs-keyword">import BlockingScheduler </span></span></span></span></span>

pyautogui

首先,让我们实现消息的自动发送。

[En]

First of all, let’s implement automatic message sending.

pyautogui 是一个非常强大的库,可以操作鼠标和键盘。我们将用它来完成自动操作电脑。

先来做一些基本设置:

<span class="hljs-attr">pyautogui.PAUSE = <span class="hljs-number">1 </span></span>

然后我们登录微信并最小化。

[En]

Then we log on to Wechat and minimize.

接下来我们把鼠标放到微信的任务栏图标上,运行以下语句,获取此时光标的坐标,返回一个Point对象:

<span class="hljs-built_in">print(pyautogui.position()) </span>

打开微信,选择女友的回复窗口,将鼠标放在输入框上,还可以得到光标坐标,以便将焦点锁定在输入框上,方便以后输入。

[En]

Open Wechat, select the girlfriend’s reply window, place the mouse over the input box, and also get the cursor coordinates, in order to lock the focus to the input box to facilitate input later.

<span class="hljs-built_in">print(pyautogui.position()) </span>

接下来,控制程序依次点击这两个点:

[En]

Next, the control program clicks these two points in turn:

<span class="hljs-selector-tag">pyautogui<span class="hljs-selector-class">.click(<span class="hljs-selector-tag">icon_position) # &#x9ED8;&#x8BA4;&#x5DE6;&#x952E;&#x5355;&#x51FB;
<span class="hljs-selector-tag">pyautogui<span class="hljs-selector-class">.click(148, 879)
<span class="hljs-selector-tag">pyautogui<span class="hljs-selector-class">.click(<span class="hljs-selector-tag">entry_position)
<span class="hljs-selector-tag">pyautogui<span class="hljs-selector-class">.click(174, 751)</span></span></span></span></span></span></span></span></span></span>

打开微信并锁定焦点后,我们开始输入文本。

[En]

After opening Wechat and locking the focus, we start entering text.

有两种输入文本的方法:

[En]

There are two ways to enter text:

  • pyautogui.typewrite([‘o’, ‘n’, ‘e’, ‘enter’])在方法中传入一个列表,里面每一元素都是单个字母或特殊按键
  • pyautogui.typewrite(‘You can type multiple letters in this way’)传入字符串,但不能同时打印字母和特殊按键。

这两种方式都不能直接输入中文,所以你只能依靠你的输入法来输入中文。

[En]

Neither of these two ways can input Chinese directly, so you can only rely on your input method to input Chinese.

pyautogui.typewrite([*<span class="hljs-keyword">list(<span class="hljs-string">'zhengzai '), *<span class="hljs-keyword">list(<span class="hljs-string">'jinxing '), <span class="hljs-string">'shift', *<span class="hljs-keyword">list(<span class="hljs-string">'pyautogui'), <span class="hljs-string">'shift', *<span class="hljs-keyword">list(<span class="hljs-string">'shiyan '), <span class="hljs-string">'enter'], <span class="hljs-number">0.1) </span></span></span></span></span></span></span></span></span></span></span></span>

为了使我们的操作更加 人模狗样 像人的操作,我么来加上移动鼠标的代码:

pyautogui.moveTo(icon_position, duration=<span class="hljs-number">2) </span>

看看效果:

当然,如果你有很多输入,觉得很麻烦,你可以通过复制和粘贴来完成:

[En]

Of course, if you have a lot of input and find it troublesome, you can do it by copying and pasting:

<span class="hljs-keyword">import pyperclip

pyperclip.copy(<span class="hljs-string">'&#x6B63;&#x5728;&#x8FDB;&#x884C;&#x53D1;&#x4E2D;&#x6587;&#x8BD5;&#x9A8C;&#xFF0C;&#x770B;&#x5230;&#x8BF7;&#x5FFD;&#x7565;&#xFF0C;&#x66F4;&#x4E0D;&#x8981;&#x9A82;&#x50BB;&#x903C;') </span></span>

通过这种方式,我们完成了自动发送微信消息的功能。

[En]

In this way, we have completed the function of automatically sending Wechat messages.

apscheduler

APScheduler 是一个Python库,可实现延迟调度要执行Python代码的功能,可以只执行一次,也可以定期执行。可以随时添加新任务或删除旧任务。能够十分方便地进行定时任务。

<span class="hljs-string">scheduler <span class="hljs-string">= <span class="hljs-string">BlockingScheduler() </span></span></span>

add_job 方法在这里传了 3 个参数,第一个为到时间后要执行的函数,第二个为触发器的类型。这里选用的是 date 触发器,特定的时间点触发,作业任务只会执行一次。第三个参数 run_date 就是执行的时间。在这前我已经把自动发送消息的代码封装为了 main 函数,只需到时后调用即可。

完整代码

<span class="hljs-keyword">import pyautogui
<span class="hljs-keyword">import pyperclip
<span class="hljs-keyword">from datetime <span class="hljs-keyword">import datetime
<span class="hljs-keyword">from apscheduler.schedulers.blocking <span class="hljs-keyword">import BlockingScheduler

<span class="hljs-function"><span class="hljs-keyword">def <span class="hljs-title">main<span class="hljs-params">():
    pyautogui.PAUSE = <span class="hljs-number">0

    icon_position = pyautogui.Point(x=<span class="hljs-number">148, y=<span class="hljs-number">879) </span></span></span></span></span></span></span></span></span></span></span></span></span>

就这样办!你现在可以上床睡觉了。

[En]

It’s done! You can go to bed now.

结果

第二天早上起来,被妈妈骂了一顿,问我为什么午夜12点电脑还开着,为什么还一个人发微信!

[En]

When I got up the next morning, I was scolded by my mother and asked me why my computer was still on at 12:00 midnight and why I was still posting Wechat by myself!

然而,幸运的是,我没有失去我的女朋友,所以我圆满地完成了女朋友的任务!

[En]

However, fortunately, I did not lose my girlfriend, so I successfully completed my girlfriend’s task!

— the End —

好了,这就是本文的全部内容。这就是我要分享的全部。感谢您的阅读。

[En]

Well, that’s all for this article. That’s all I share. Thank you for reading.

Original: https://www.cnblogs.com/sn5200/p/15892152.html
Author: Python可乐的呀
Title: 女朋友让我深夜十二点催她睡觉,我有Python我就不干

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/510048/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球