Python crawler: 5.20 write a wechat regular batch sending 99 love words script for your girlfriend

ABBA ABBA ABBA

520 arrived (detailed explanation of being late), how to give a large number of estrus words to your girlfriend? First of all, there must be love words. Where does love words come from? I found an address:“ https://chp.shadiao.app/api.php ”So, how to get love messages and send them to your girlfriend through wechat? Please take a look at the detailed steps

Request sentiment API

First, import the Requests library. If it is not installed in cmd: pip3 install requests, first create an empty list content for storing love words, create a Sessions for continuous access (which is actually useless here), and then assign the url to the url variable. Because the api uses get request, use the get method to request the api address, Then use the append method of the list to save into the content, and finally customize a function encapsulation. Here I customize a parse function

import requests

content = []

def parse():
	Sessions = requests.session()
	url = "https://chp.shadiao.app/api.php"
	re = Sessions.get(url=url)
	content.append(re.text)

Send wechat message

Import the wechat interface provided by the itchat library to find friends and send messages (the library needs to be installed through pip), and then use auto_ The login method logs in to wechat, then adds the hotReload parameter and assigns it to True to store the login information, and then uses search_ The friends method looks up your girlfriend's comment name and assigns it to the user, and then uses the send method to send the message. The first parameter of this method is the information to be sent, that is, the love words in the content list, and then fill in a toUserName parameter, which is the information to be sent to the user, but the user's information is a list. The dictionary has many parameters, We only need the value of UserName in the first dictionary: user[0]["UserName"], and finally customize a function encapsulation. Here I customize a sen_msg function, but you need to set a parameter to get the subscript of the list. I set it to size here, because 99 items need to be sent circularly later. It is impossible to send a list of love words, so the message sent should be: content[size]

import itchat

def sen_msg(size):
	itchat.auto_login(hotReload=True)
	user = itchat.search_friends("Here are your girlfriend's notes")
	itchat.send(content[size], toUserName=user[0]["UserName"])

Start function

Use the for loop to request the api 99 times, store 99 love words into the content, and send them to your girlfriend through wechat. Finally, customize a function package. Here I customize a sen_msg function

def run():
    for i in range(99):
        parse()
        sen_msg(size=i)

Timing transmission

Here we import a schedule third-party library (which needs to be installed through pip). This library is a scheduler. We use it to create a scheduled task, and use every() thursday. at("13:14"). The do (run) method runs the run function at 13:14 on Friday, and then uses run_ The pending method runs in the while loop and checks the time every second, so you need to import time and wait for one second with the sleep method

import schedule
import time

schedule.every().thursday.at("13:14").do(run)  # It will be issued at 13:14 on Thursday
while True:
    schedule.run_pending()
    time.sleep(1)

Complete code

Because running Sen_ The MSG function will log in to wechat once, but it may not be in front of the computer at the scheduled time, so we will use itchat auto_ Login (hotReload=True) is written at the beginning, that is, log in once when running the script. hotReload=True will save my login information

import requests
import schedule
import itchat
import time

content = []
Sessions = requests.session()
itchat.auto_login(hotReload=True)  # hotReload=True to store login information

# Request sentiment api
def parse():
    url = "https://chp.shadiao.app/api.php"
    re = Sessions.get(url=url)
    content.append(re.text)

# Send wechat message
def sen_msg(size):
    print(content[size])
    user = itchat.search_friends("Your girlfriend's notes")
    itchat.send(content[size], toUserName=user[0]["UserName"])

# start-up
def run():
    for i in range(99):
        parse()
        sen_msg(size=i)

schedule.every().thursday.at("13:14").do(run)  # It will be issued at 13:14 on Thursday
while True:
    schedule.run_pending()
    time.sleep(1)

Keywords: Python crawler

Added by Kookedoh on Wed, 09 Feb 2022 09:57:20 +0200