Skip to content

Instantly share code, notes, and snippets.

@IanSmith123
Last active January 22, 2019 14:24
Show Gist options
  • Save IanSmith123/b89a5b642d66a696c0ef89c90d8e067d to your computer and use it in GitHub Desktop.
Save IanSmith123/b89a5b642d66a696c0ef89c90d8e067d to your computer and use it in GitHub Desktop.
v2ex 摸鱼 刷dau脚本
import os
import sys
import json
import argparse
from time import sleep
from random import randint, shuffle
from selenium import webdriver
site_url = "https://www.v2ex.com"
driver_dumpfile = 'cookie.txt'
try:
target_score = int(sys.argv[1])
except IndexError:
target_score = 2018
def dump_cookie(d: webdriver.Chrome, dumpfile):
with open(dumpfile, 'w', encoding='utf8') as f:
cookies = json.dumps(d.get_cookies(), indent=2)
f.write(cookies)
# f.write(d.get_cookies())
def load_cookie(d: webdriver.Chrome, dumpfile: str) -> webdriver.Chrome:
with open(dumpfile, 'r', encoding='utf8') as f:
cookies = json.loads(f.read())
for cookie in cookies:
# print(cookie)
d.add_cookie(cookie)
print("读取cookie完成")
return d
def login(d: webdriver.Chrome, user, passwd, dumpfile) -> webdriver.Chrome:
# d.get("https://www.v2ex.com")
# sleep(3)
d.get("https://www.v2ex.com/signin")
# 输入账号和密码
d.find_element_by_xpath('//*[@id="Main"]/div[2]/div[2]/form/table/tbody/tr[1]/td[2]/input').send_keys(user)
d.find_element_by_xpath('//*[@id="Main"]/div[2]/div[2]/form/table/tbody/tr[2]/td[2]/input').send_keys(passwd)
# 定位到验证码输入框
d.find_element_by_xpath('//*[@id="Main"]/div[2]/div[2]/form/table/tbody/tr[3]/td[2]/input').click()
print("在网页中输入验证码")
# 10s内手动输入验证码,然后回车
sleep(10)
# 检查是否登录成功
if user == d.find_element_by_xpath('//*[@id="Top"]/div/div/table/tbody/tr/td[3]/a[2]').text:
print("login success")
print(d.get_cookies())
dump_cookie(d, dumpfile)
else:
print("login failed")
exit(1)
return d
def water(d: webdriver.Chrome, user, target):
d.get(site_url)
urls = d.find_elements_by_xpath('//*[@id="Main"]/div[2]/div/table/tbody/tr/td[3]/span[1]/a')
# print(urls)
urls = [item.get_attribute("href") for item in urls]
print(urls)
if len(urls) < 1:
print("error")
return "error"
# 开始划水
count = 0
seed = randint(1, 4) # 预生成一个seed
while True:
shuffle(urls)
for url in urls:
count += 1
d.get(url)
wait_time = randint(3, 15)
print("第{}次刷新, 等待{}s".format(count, wait_time))
sleep(wait_time)
# 检查dau
seed -= 1
if not seed:
order, current_dau = check_dau(d, user)
print("当前活跃度排名 {}, 分数 {}".format(order, current_dau))
sleep(randint(5, 9))
seed = randint(4, 15) # 更新seed
if target - current_dau < 20:
print("积分接近 {}, 自动退出".format(target))
exit(0)
if count > 1000:
break
return count
def check_dau(d: webdriver.Chrome, user):
d.get('https://www.v2ex.com/member/{}'.format(user))
order = d.find_element_by_xpath('//*[@id="Main"]/div[2]/div[1]/table/tbody/tr/td[3]/span[2]/a').text
order = int(order)
current_dau = 0
if order < 11:
# 位于前十
d.get('https://www.v2ex.com/top/dau')
dau_list = d.find_elements_by_xpath('//*[@id="Main"]/div[2]/div[2]/table/tbody/tr/td[3]/div')
dau_list = [int(item.text) for item in dau_list]
print(dau_list)
current_dau = dau_list[order - 1]
return order, current_dau
if __name__ == "__main__":
username = "e"
password = "A"
# 如果文件不存在,则重新登录
driver = webdriver.Chrome()
driver.get(site_url)
sleep(1)
if os.path.isfile(driver_dumpfile) and os.path.getsize(driver_dumpfile):
driver = load_cookie(d=driver, dumpfile=driver_dumpfile)
else:
driver = login(d=driver, user=username, passwd=password, dumpfile=driver_dumpfile)
sleep(randint(1, 4))
fish = water(d=driver, user=username, target=target_score)
@IanSmith123
Copy link
Author

IanSmith123 commented Dec 28, 2018

  1. 配置selenium和chrome webdriver,将chrome webdriver放置于当前目录,然后export PATH=$PATH:./

  • if 本地运行:
  1. 运行程序,打开登录页面之后会自动输入账号和密码,手动输入验证码,手动回车
  • if 挂远程运行:
  1. 本地运行程序,登录完成之后停掉程序,目录下面会生成一个cookie.txt, 将本程序和cookie.txt放到远程, 运行程序

  1. 开始自动化摸鱼,该干啥干啥去,想起来的时候去刷一下dau看看自己的排名

@IanSmith123
Copy link
Author

脚本自动显示dau

@IanSmith123
Copy link
Author

  • 验证码不好处理,可以本地人工输入验证码登录成功之后保存下来,放到远程运行

  • 如果远程没有桌面环境,加一行headless关掉窗口

  • 可以设定目标积分,快到达目标积分时停止脚本

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment