• 基于selenium和requests的自动刷易班脚本
  • 受人委托帮忙写的,代码优化空间大
#-*- codeing = utf-8 -*-
#@Time :  19:56
#@Author : LTLT
#@File : yiban.py
#@Software : PyCharm

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
import threading
import requests
import random
import json
import time
import sys

print("""[+]本脚本用于易班自动 打卡 评论10篇文章 点赞10次
[+]将需要打卡的用户名和密码以 用户名+空格+密码 的形式逐行写入脚本同目录下的config.txt中(没有就新建)
#用户名 密码 用#表示该账户锁定,即不参与此次打卡
添加参数headless运行时,不显示浏览器界面,进入无头模式
[+]建议每次最多运行8~10个人的账号,不然会导致加载缓慢而selenium无法定位XPATH
[+]受到网络以及硬件等因素干扰,本脚本无法100%每一步都成功获得网薪,作者认为无伤大雅
------Auther LTLTLoveLxey------
""")

proxies = {
    "http": "",
    "https": "",
}

headers_pool = [
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
    "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1",
    "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
]

responseText = [
    '冲!冲!冲!冲!冲!冲!',
    '加油啊加油啊',
    '今天签到完成!',
    '写得真棒!!!',
    '大赞!大赞!大赞!',
    '今日阅读get!',
    '好耶好耶好耶',
    '留下阅读的足迹'
]

loginUrl = 'https://www.yiban.cn/login?go=https://www.yiban.cn/'

loginInfo = []

# 读入userName & userPass
try:
    fileRead = open('config.txt', 'r', encoding='utf-8')
    for lines in fileRead:
        lines = lines.replace("\n", '')
        lines = lines.replace('   ', '')
        if lines!='' and lines[:1]!='#':
            userProfire = lines.split(' ')
            print(lines)
        else:
            continue
        appendTXT = '{{"test": "test", "userName": "{}", "userPass": "{}"}}'.format(userProfire[0], userProfire[1])
        loginInfo.append(appendTXT)
except:
    print("config.txt读取失败,请问是否创建,且内容格式为每行 用户名 + 空格 + 密码 例如:admin 123456")
    exit(0)
# 获取新闻链接
newsList = []
resNew = requests.get(url='https://www.yiban.cn/',proxies=proxies,headers={'User-Agent': random.choice(headers_pool)})
sources = resNew.text
sourcesSplit = sources.split('<li class="news-item odd">')
# 只获取5*2=10篇文章进行评论和点赞
for i in range(1,6):
    detilSplite = sourcesSplit[i].split('<a target="_blank" href="')
    newsList.append(detilSplite[1][0:detilSplite[1].find('">')])
    newsList.append(detilSplite[2][0:detilSplite[2].find('">')])

# 开启多线程,每个账号一个线程
thread_list = []

#要启动的函数
def out(loginUrl,loginInfo, newsList, responseText):
    options = Options()
    # 无头骑士(雾)模式
    if len(sys.argv)>1:
        if sys.argv[1] == 'headless':
            options.add_argument('-headless')
    try:
        driver=webdriver.Firefox(firefox_options=options)
    except:
        exit("Error!")
    if loginInfo!='':
        i = json.loads(loginInfo)
    else:
        exit()
    # 用户登录
    driver.get(loginUrl)
    time.sleep(2)
    driver.find_element_by_id('account-txt').send_keys(i.get('userName'))
    driver.find_element_by_id('password-txt').send_keys(i.get('userPass'))
    driver.find_element_by_id('login-btn').send_keys(Keys.ENTER)
    time.sleep(3)
    # 尝试签到
    try:
        driver.find_element_by_xpath('//*[@id="tool-sign"]').click()
        time.sleep(2)
        driver.find_element_by_xpath('/html/body/div[6]/div/div[2]/div/div[1]/div/dl/dd[1]/i').click()
        time.sleep(2)
        driver.find_element_by_xpath('/html/body/div[6]/div/div[2]/div/div[2]/div/a[1]').click()
        time.sleep(2)
    except:
        pass
    time.sleep(1)
    for k in range(10):
        try:
            driver.get(newsList[k])
            time.sleep(4)
            # 点赞
            try:
                driver.find_element_by_xpath('/html/body/div[2]/section/section[3]/div/div[2]/div').click()
                time.sleep(2)
            except:
                time.sleep(5)
                driver.find_element_by_xpath('/html/body/div[2]/section/section[3]/div/div[2]/div').click()
            # 评论
            driver.find_element_by_xpath('/html/body/div[2]/section/section[3]/div/div[1]').click()
            time.sleep(1)
            driver.find_element_by_xpath('/html/body/div[2]/section/section[3]/div/div[2]/div[1]/input').send_keys(random.choice(responseText))
            time.sleep(2)
            driver.find_element_by_xpath('/html/body/div[2]/section/section[3]/div/div[2]/div[2]').click()
            time.sleep(5)
        except:
            pass

        # 间隔61秒后
        time.sleep(61)
    # 今日任务完成,退出登录
    driver.get('https://www.yiban.cn/logout')
    time.sleep(1)
    print("User {} finished".format(i.get('userName')))
    driver.close()

#控制线程数
for j in range(len(loginInfo)):
    thname = 'thr'+str(j)
    print("Thread {} Start...".format(thname))
    thname = threading.Thread(target=out, args=(loginUrl, loginInfo[j], newsList, responseText,))
    thread_list.append(thname)

#启动线程
for t in thread_list:
    t.start()