使用Python实现小说有更新时自动推送到手机

第一次写Python写的不好麻烦多多指导

本代码仅使用于代码中提到的网站、其它网站可以替换xpath路径适配

本代码仅实现了pushDeer推送

代码

import requests
from lxml import etree

# pushDeer推送的key
# https://www.pushdeer.com/official.html
pushDeerKey = "这里填上自己的key"


def RequestUrl(url):
    # 请求小说的url
    response = requests.get(url)
    # 解析得到的html
    html = etree.HTML(response.text)
    # 使用xpath获取元素
    nodes = html.xpath("//section[@class='zxjz fk']/ul/li[1]/a")
    # 打开文件(用于判断上次章节名称和当前章节名称是否一致)
    file = open("record.txt", "r+")
    # 读取文件的第一行
    file_str = file.readline()
    print("file_str:" + file_str)
    # 判断文件中的章节名和请求得到的章节名是否一致
    if file_str.__eq__(nodes[0].text):
        print("暂无更新...")
        return
    # 如果不一致则说明小说有更新了
    print("小说有新的更新...")
    file.seek(0, 0)
    file.truncate(0)
    file.writelines(nodes[0].text)
    href = nodes[0].get("href")
    href = "http:" + href
    # response = requests.get(href)
    # print(response.text)
    content = "[" + href + "](" + href + ")"
    pushDeer("您关注的小说有更新了!!!!", content)


# 推送消息到pushDeer
def pushDeer(title, content):
    requests.get(
        "https://api2.pushdeer.com/message/push?pushkey=" + pushDeerKey + "&text=" + title + "&desp=" + content + "&type=markdown")


if __name__ == '__main__':
    print("程序开始执行...")
    # 小说url
    RequestUrl("https://m.bagzw.com/4_4065/")
最后修改:2022 年 06 月 15 日 01 : 47 PM
如果觉得我的文章对你有用,请随意赞赏