最近心血来潮在晋江写了本扑街文学,大概10w字左右,虽然完全没人看,但毕竟是我的心血,如果有访问量的变化还是想及时知道,并且想要及时的消息提示——那么发wx就再好不过了。
那么就开整吧。
第一步:获得页面response
首先需要找到文章页面的url 类似以下这样
把你要查的那个页面的html扒下来。怎么扒?用下urllib贼简单
https://www.jianshu.com/p/63dad93d7000
需要登录怎么办?参考这个python爬虫19 | 爬虫遇到需要的登录的网站怎么办?用这3招轻松搞定! - 知乎
ok,这时候发现 需要的数据竟然在茫茫然的表格中跟html代码混在一起,这时候需要beautifulsoup登场了 参考Beautiful Soup4 之table数据提取_yf999573的博客-CSDN博客_bs4 table
要是需要定时执行怎么办呢,参考让 Python 程序定时执行的 8 种姿势~ - 知乎
第二步,把得到的信息发给自己:参考用Python实现自动发消息,自定义内容,太省事了!_退休的龙叔的博客-CSDN博客_python自动发消息脚本
直接上成品代码吧 简单易懂
import requests
from bs4 import BeautifulSoup
import re
import datetime
import time
import pyautogui as pag
import pyperclip
def check_recent():
headers = {
# 假装自己是浏览器,
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36',
# 把你刚刚拿到的Cookie塞进来
'Cookie':'你的cookie',
}
session = requests.Session()
#这里填写你自己文章的地址
response = session.get('https://my.jjwxc.net/backend/managenovel.php?novelid=00000000&jsid=3000000_4000000', headers=headers)
response.encoding = response.apparent_encoding # 自动转码
html = response.text
soup = BeautifulSoup(html, features="html.parser") # 创建一个BeautifulSoup对象
table_node = soup.find_all('table')
res = ''
for table in table_node:
res = res + table.get_text()
#把空格去一去
res = res.replace(' ', '')
res = res.replace('\n', '')
res = res.replace('\r', '')
res = res.replace('\t', '')
#用正则匹配一下
match1 = re.findall("总点击:\d+\xa0总书评:\d+\xa0精华评:\d+\xa0收藏数:\d+", res)
message = match1[0].split('\xa0')
# print(message)
new_info = []
for i in [0,1,3]:
new_info.append(int(message[i].split(':')[1]))
return new_info
def compare_with_before(recent_info, new_info) :
difference = [new_info[i] - recent_info[i] for i in range(3)]
if difference[0] > 0:
text = "总点击增加了" + str(difference[0])
print(text)
send_message(text)
if difference[1] > 0:
text = "总书评增加了" + str(difference[1])
print(text)
send_message(text)
if difference[2] > 0:
text = "收藏数增加了" + str(difference[2])
print(text)
send_message(text)
def send_message(text):
#填入你自己的微信输入框坐标
pag.click(-937, 95) # 鼠标点击并定位到聊天窗口
pyperclip.copy(text) # 复制该行
pag.hotkey("ctrl", "v") # 粘贴,mac电脑则把ctrl换成command
pag.typewrite("\n") # 发送
time.sleep(5) # 每次发完间隔5s
def time_printer():
now = datetime.datetime.now()
ts = now.strftime('%Y-%m-%d %H:%M:%S')
print('do func time :', ts)
if __name__ == '__main__':
#初始化默认为0,也可以改成你的数据
recent_info = [0,0,0]
while True:
time_printer()
new_info = check_recent()
compare_with_before(recent_info,new_info)
recent_info = new_info
time.sleep(1200)