1. Introduction

Hello, I’m Anguo!

I wrote an article before, in which I proposed a scheme to automatically send morning news to groups on wechat every day

How to use Python crawler to send morning news to wechat group? (detailed)

However, for many people, writing an App first requires certain mobile terminal development experience, and then also need to write another barrier-free service application, which seems to be a certain difficulty

This article will introduce another scheme, that is: using the AutoJS introduced in the previous article to realize the automatic sending of morning news to wechat group

2. Crawlers and services

For the convenience of demonstration, baidu hot search is used as the data source of morning news.

Use Requests + BeautifulSoup to crawl the 15 most popular entries by heat

import requests from bs4 import BeautifulSoup def baidu_top_tipic(): "" hot" baidu search ", "" requests_page = requests. Get soup = (' http://top.baidu.com/buzz?b=1&c=513&fr=topbuzz_b42_c513 ') BeautifulSoup(requests_page.text, "LXML ") # BeautifulSoup(requests_page.text," LXML ") # BeautifulSoup = soup. Find_all ("div", class_='c-single-text-ellipsis', text=True) top_list = [] for index, text in enumerate(soup_text): Top_list.append ((STR (index + 1) + ", "+ text.string.strip())) return '\n'. Join (top_list[:15])Copy the code

Then, use FastAPI to write the API for obtaining morning news and deploy it to the cloud server (take CentOS as an example).

Import uvicorn from fastAPI import fastAPI from every_news import * # pip3 install uvicorn # pip3 install fastAPI # instantiation @app.get("/news") async def rsc_api(): MSG = get_news() return {"code": 200, "MSG ": MSG} if __name__ = = "__main__ ': uvicorn. The run (=' news_api: app 'app, host =" 0.0.0.0 ", the port = 6789, reload = True, the debug = True)Copy the code

Finally, run the following command to get the service running in the background

Log nohup python3 /xag/news_api.py > /news_api.log 2> &1&Copy the code

3. Automate group messaging

Write AutoJS scripts in VS Code

First, define a way to send a message to a group chat

PS: Using the click() coordinate to perform a click is only available on Android 7.0+

//API call to get news data var url = "http://host:6789/news"; Var group_name = "Group name "; Function send_wx_msg(group_name, send_msg) {// If sleep, wake up device // note: WakeUpIfNeeded () // Open wechat app.launch(" com.0700.mm "); Var chat_element_bounds = text(group_name).findone ().bounds(); // Android7.0+ click(chat_element_bounds.centerx (), chat_element_bounds.centery ()); Sleep (3000) id("auj").className("EditText").findone ().settext (send_msg) sleep(3000) // Send message text(" send ").click() log(" send! " ) // Return to the mobile desktop back(); home();Copy the code

Then, start a new thread in the main thread, call the API, fetch the data and send it out

Threads.start (function () {http.get(url, {}, function (res, err) {if (err) {log(" Sorry! Failed to get news today..." ) return; } log(" Today's news success!" ) let html = res.body.string(); let msg = JSON.parse(html).msg; send_wx_msg(group_name, msg) }); })Copy the code

Next, use VS Code to import the source Code to the mobile device

Finally, select Source file – right-click – more – Scheduled task and set scheduled task

4. The last

In this way, it can realize the function of sending morning news to designated groups every morning

Of course, if it involves sending multiple group chats, you only need to use AutoJS to query multiple target group chats + page slide and traverse to send information

In addition, due to the instability of barrier-free services, you can set the priority of AutoJS application services to ensure the stable operation of the program

If you think the article is good, please like, share, leave a message, because this will be my continuous output of more high-quality articles the strongest power!

Recommended reading

Mobile Automation AutoJS Quick Start Guide (Part 1)

5 minutes, using Intranet penetration to quickly achieve remote desktop

How can Jmeter execute Python scripts concurrently

Talk about the best PC automation solution – Pywinauto

Talk about the best PC automation scheme – WinAppDriver