Automating Daily Love Messages with Python and QQ on macOS
This guide demonstrates how to use Python on macOS to scrape love quotes, store them, and automatically launch QQ to send a random quote each day, including code snippets and steps to set up an Automator app for startup execution.
Introduction
The article explains a personal automation project that scrapes love messages from a website, saves them to a text file, and uses a Python script to launch QQ on macOS and send a random message each day.
1. Scrape Love Messages
The script visits pages of a love‑quote site, extracts the text inside <p> tags, and writes each quote to qinghua.txt separated by the delimiter !@#$% .
<code>import requests
import re
def fuckLoveWords():
with open("qinghua.txt", "w", encoding="utf-8") as f:
for i in range(1000, 1200):
print("第" + str(i) + "页")
try:
url = "http://www.ainicr.cn/qh/" + str(i) + ".html"
response = requests.get(url).text
counts = re.findall('<p>(.*?)</p></a>', response)
for count in counts:
f.write(count + '!@#$%')
except:
pass
if __name__ == "__main__":
fuckLoveWords()
</code>2. Launch QQ and Send Message
The second script reads the stored file, picks the first line as the current message, rewrites the file without that line, and then uses macOS commands to open QQ, copy the message to the clipboard, and send it.
<code>import os
import subprocess
def getText():
filePath = "/Users/jsq/PycharmProjects/qinghua/qinghua.txt"
with open(filePath, 'r+', encoding='utf-8') as f:
content = f.read()
content = content.split("!@#$%")
res = content[0]
del content[0]
contentStr = '!@#$%'.join(content)
with open(filePath, 'w', encoding='utf-8') as f2:
f2.write(contentStr)
return res
if __name__ == "__main__":
msg = getText()
print(msg)
os.system("open 'tencent://message/?uin=输入要调起的QQ&Site=&Service=201&sigT=2cf2671557dd'")
p1 = subprocess.Popen(["echo", msg], stdout=subprocess.PIPE)
subprocess.Popen(["pbcopy"], stdin=p1.stdout)
</code>3. Configure macOS Auto‑Run
Using the built‑in Automator app, the user creates a workflow that runs the Python script at login. Steps include opening Automator, selecting “Application”, adding a “Run Shell Script” action, inserting the command to execute the Python file, and saving the workflow as an app.
The saved app is then added to System Preferences → Users & Groups → Login Items, ensuring the script runs automatically after the user logs in.
Conclusion
By combining web scraping, file handling, and macOS automation, the solution provides a hands‑free way to send a daily love quote via QQ, demonstrating practical Python scripting and system integration techniques.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.