Python Selenium自動化领取图书馆积分
环境说明
Python 3.10.7
IDE VsCode laste
chrome浏览器:106.0.5249.103
chromedriver:下载地址
吐槽
以前靠抽奖图书馆柜子突然变成靠读者积分排名领取,任务还多的离谱,就突发起来抓个api自动刷,谁知道一开始就是无底洞,抓的api直接调用不了,似乎有反爬(流下没有技术的泪水),然后又想到直接浏览器模拟用自动化测试Selenuium工具,现学现用可算折腾一天可算整出来了,谁知道每次生成定位的地方会生成两种路径,但是懒狗不想继续折腾了,直接异常处理加上暴力循环。直接一天随机看20本书总有五本能对。
目的
登陆采用等待20秒人工识别验证码,然后判断收藏书籍是否为空,不为空全部取消收藏(防止重复收藏bug),然后随机1-50000书籍编号页面,进行收藏和打卡,暂时就这么多,后续可能更新评论,委托等~~~~
运行后,弹出的浏览器一定要最大窗口化!
最后效果
运行代码
导入上面的库没有就pip下载 如:
pip install re
运行后,弹出的浏览器一定要最大窗口化!
from time import time
from selenium import webdriver
from selenium.webdriver.common.by import By
import random
from selenium.webdriver import ActionChains
import time
import re
user = "xxxxxx" # 账号
password = 'Lk030314' #密码
wd = webdriver.Chrome("chromedriver")
# 等待五秒
wd.implicitly_wait(5)
wd.get("https://findwhsw.libsp.cn/#/readRange")
submit = wd.find_element_by_xpath(
'//*[@id="app"]/div/div/div[1]/div/div/div/div[3]') # 首先创建对象
time.sleep(1)
ActionChains(wd).click(submit).perform() # 点击登陆
time.sleep(1)
# 输入账号
wd.find_element_by_xpath(
'/html/body/div[2]/div/div[2]/div/div[1]/div/div/div/div[2]/div/div[1]/div[1]/input').send_keys(user)
# 输入密码
wd.find_element_by_xpath(
'/html/body/div[2]/div/div[2]/div/div[1]/div/div/div/div[2]/div/div[1]/div[2]/input').send_keys(password)
# 勾选同意
submit1 = wd.find_element_by_xpath(
'/html/body/div[2]/div/div[2]/div/div[1]/div/div/div/div[2]/div/div[1]/div[6]/label/span/input')
ActionChains(wd).click(submit1).perform() # 点击登陆
print("等待15秒登陆")
for i in range(15):
time.sleep(1)
print('还剩下:' + str(15 - i))
# 清空收藏
def quxiaoshoucang():
try:
wd.get('https://findwhsw.libsp.cn/#/personalCenter')
wd.implicitly_wait(5)
time.sleep(1)
shudan = wd.find_element_by_xpath(
'//*[@id="app"]/div/div/div[2]/div/div/div/div/div/div[1]/div/div[2]/span')
ActionChains(wd).click(shudan).perform() # 点击书单
time.sleep(1)
# 点击默认书单
chuangjian = wd.find_element_by_xpath(
'//*[@id="app"]/div/div/div[2]/div/div/div/div/div/div[2]/div/div[1]/a[2]')
ActionChains(wd).click(chuangjian).perform() # 点击创建
time.sleep(1)
moren = wd.find_element_by_xpath(
'//*[@id="app"]/div/div/div[2]/div/div/div/div/div/div[2]/div/div[2]/div/div[2]/div/div[1]')
ActionChains(wd).click(moren).perform() # 点击默认书单
time.sleep(1)
shoucang = wd.find_element_by_xpath(
'//*[@id="app"]/div/div/div[2]/div/div/div/div/div[1]/div[2]/div[1]/div[1]')
# for i in shoucang:
# print(i.text)
print(shoucang.text)
# 把前100个的收藏拿出来用来循环取消收藏
str = shoucang.text
a = re.findall("\d+", str)
a = int(a[0])
# 收藏的书>0 则跳过
if a > 0:
for i in range(a):
# 点击取消收藏
quxiao = wd.find_element_by_xpath(
'//*[@id="app"]/div/div/div[2]/div/div/div/div/div[1]/div[2]/div[2]/div[1]/div/div/div/div[1]/div/div/div[2]/div[1]/span/img')
ActionChains(wd).click(quxiao).perform() # 取消
time.sleep(1)
# 确定
quxiaoqueren = wd.find_element_by_xpath(
'/html/body/div[2]/div/div/div/div[2]/div/div/div[2]/button[2]/span')
ActionChains(wd).click(quxiaoqueren).perform() # 取消确认
time.sleep(1)
wd.refresh()
time.sleep(2)
except:
pass
def yitiaolong():
time.sleep(3)
# 开始收藏
try:
shoucang = wd.find_element_by_xpath(
'//*[@id="container"]/div[1]/div[1]/div[3]/div[3]/div/div[2]/span/img')
ActionChains(wd).click(shoucang).perform() # 点击收藏
time.sleep(2)
# 收藏确认
shoucangqueren = wd.find_element_by_xpath(
'/html/body/div[2]/div/div[2]/div/div[1]/div[3]/div/button[2]/span')
ActionChains(wd).click(shoucangqueren).perform() # 确认收藏
time.sleep(1)
# 打分
pingfen = wd.find_element_by_xpath(
'//*[@id="container"]/div[1]/div[1]/div[3]/div[2]/div/div/div/div[2]/span[2]/ul/li[5]/div[1]/i')
ActionChains(wd).click(pingfen).perform() # 打分
except BaseException:
pass
# 取消收藏
quxiaoshoucang()
print("已清空收藏")
for i in range(20):
id = random.randint(2, 60000)
time.sleep(1)
wd.get("https://findwhsw.libsp.cn/#/searchList/bookDetails/"+str(id))
time.sleep(1)
wd.get("https://findwhsw.libsp.cn/#/searchList/bookDetails/"+str(id))
yitiaolong()
print("完成全部任务拉")
# element.click()
评论区