Python网络爬虫2—网课自动刷点赞刷评论刷积分


疫情期间上网课,学校使用UMN教学平台进行授课,点赞和评论都可以获得积分,积分影响最终成绩,别的同学都是手动弄,我觉得太麻烦,所以我打算用Python刷一刷。

#2.2版本公告:新增账号保存功能,用户只需要输入一次账号密码,会自动以文本格式保存在当前文件夹下,第二次运行会自动读取
#2.1版本公告:程序通过相对路径找到chromedriver.exe,不需要用户再手动填写绝对路径了
#2.0版本公告:加入自动评分和评论的功能,加入可显示剩余人数的功能
#1.7版本公告:做了代码优化,并封装了部分复用的代码
#1.6版本公告:加入了用户自定义输入,可自定义评论内容
#1.5版本公告:修复了因找不到chromedriver而造成的'chromedriver' executable needs  to be in PATH的bug
#1.4版本公告:修复因跳过页面展开而造成的实际人数统计的错误,加入了页面滑动以及显性等待机制,最长等待20秒
#1.3版本公告:修复一处因服务器排队而造成的程序无法运行的bug,加入了3分钟的页面隐性等待
#1.2版本公告:修复一处在展开页面时,因为网络不稳定而造成的list index out of range的bug,加入了异常跳过机制
#1.1版本公告:修复一处因页面返回而导致的实际点赞人数不足的bug,在页面返回时需重新展开所有作业名单
#unm自动点赞器1.0功能介绍:用于在unm平台实现对其他同学作业的自动点赞。

import time,pyautogui,openpyxl,os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def look_all_task():
#----展开所有作业名单----#
    try:
        for i in range(2):
            pyautogui.scroll(-10000)
            more = WebDriverWait(driver,20).until(EC.presence_of_element_located((By.CLASS_NAME,"btn-inner")))
            driver.execute_script("arguments[0].click();",more)
            time.sleep(2)
    except IndexError:
        print('跳过因为网络不稳定展开页面失败而造成的list index out of range问题')

def account_save(user_acount,user_password):
#----保存用户账号密码信息----#
    with open(account_path,'w') as f:
        f.writelines([user_acount+'\n',user_password])
        f.close()

def account_load():
#----读取用户账号密码信息----#
    with open(account_path,'r') as f:
        f_content=f.readlines()
        return f_content[0],f_content[1]

if __name__=='__main__':
    #----用户自定义输入----#
    base_dir = os.path.dirname(__file__)  #获取当前文件夹的绝对路径
    account_path=base_dir+"//account.txt"
    user_comment=input('请输入你要评论的内容,(样例:你的作业做得真好,非常优秀):')
    if not os.path.exists(account_path):
        user_acount=input('请输入你的umn账号:')
        user_password=input('请输入你的umn密码:')
        account_save(user_acount,user_password)
    else:
        user_acount,user_password=account_load()
    #----登录网站----#
    url_task='https://m.umu.cn/session/exercise/?surl=1SqbQ623d'
    url='https://www.umu.cn/index#/index'
    driver=webdriver.Chrome(executable_path='chromedriver.exe')
    # driver=webdriver.Chrome()
    driver.get(url)
    driver.implicitly_wait(180) #隐式等待,最长等待180秒
    account=driver.find_elements_by_class_name('input-text')
    account[0].send_keys(user_acount)
    account[1].send_keys(user_password)
    button=driver.find_element_by_class_name('btn-warning')
    button.click()
    time.sleep(2)
    #----转到作业二的界面----#
    driver.get(url_task)
    time.sleep(4)
    look_all_task()
    #----统计任务数----#
    look_task=driver.find_elements_by_class_name('review-status')
    surplus=len(look_task)
    #----进入个人作业详情页----#
    for j in range(50):
        look_task=driver.find_elements_by_class_name('review-status')
        driver.execute_script("arguments[0].click();",look_task[j+1])
        time.sleep(4)
        driver.implicitly_wait(10)
        #----点赞----#
        try:
            like=driver.find_element_by_class_name('liked-user-prise')
            driver.execute_script("arguments[0].click();",like)
            print('点赞成功')
            time.sleep(2)
        except:
            print('没有找到点赞功能')
        #----评论打分----#
        try:
            comment_button = driver.find_element_by_class_name('do-score')
            pyautogui.scroll(-1000)
            time.sleep(2)
            comment_button.click()
            time.sleep(2)
            score=driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div/div/div/div[2]/div/div/div/div[2]/div[1]/div/input')
            score.send_keys('100')
            comment=driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div/div/div/div[2]/div/div/div/div[3]/div/textarea')
            comment.send_keys(user_comment)
            time.sleep(1)
            submit=driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div/div/div/div[1]/div/span[2]/span')
            submit.click()
            print('评论成功')
            time.sleep(2)
        except:
            print('读取标签失败')
        surplus-=1
        print('剩余',surplus)
        #----转到作业二的界面并展开作业列表----#
        driver.get(url_task)
        time.sleep(2)
        driver.implicitly_wait(10) 
        look_all_task()

总结:这种程序实际上是模仿浏览器进行操作,很难防范,可造成的危害也比较有限,纯属娱乐性质。


  目录