自动监控靖哥哥空间并打卡

代码写得比较烂,有需要的可以看看,自己优化一下。

import requests
import time
# import pyautogui
import autoit
import random
from bs4 import BeautifulSoup
from selenium import webdriver


class AutoReply:
    def __init__(self):
        self.base_url = "http://support.i-search.com.cn/member/wangjing"
        try:
            r = requests.get(self.base_url)
            r.raise_for_status()
            soup = BeautifulSoup(r.text, 'html.parser')
            self.newest_article = soup.find_all('a', rel="bookmark")[0]
        except Exception as e:
            raise e

    def find_new_article(self):
        while True:
            r = requests.get(self.base_url)
            soup = BeautifulSoup(r.text, 'html.parser')
            newest_article = soup.find_all('a', rel="bookmark")[0]
            if newest_article == self.newest_article:
                time.sleep(0.5)
                continue
            else:
                self.new_article_url = newest_article.get('href')
                break

    def comment_new_article(self):
        option = webdriver.ChromeOptions()
        # 载入自己的谷歌浏览器配置文件,省去登录过程
        option.add_argument(r"user-data-dir=C:\Users\young ray\AppData\Local\Google\Chrome\User Data")
        driver = webdriver.Chrome(options=option)
        driver.get(self.new_article_url)
        driver.find_element_by_xpath('//span[@class="radio-btn"]').click()
        time.sleep(1)
        autoit.mouse_click(x=500, y=600)
        # pyautogui.typewrite(random.choice(["shafa", "daka", "I'm coming", "nice"]))
        autoit.send(random.choice(["沙发", "打卡", "我来了", "抢第一"]))  # 使用autoit输入中文
        driver.find_element_by_xpath('//button[@class="green"]').click()
        time.sleep(2)
        driver.quit()


if __name__ == "__main__":
    ap = AutoReply()
    ap.find_new_article()
    ap.comment_new_article()