我很讨厌投票,别人叫我帮忙投票,我都懒得去投,公司的各种投票我一般都是装作看不到。
但是今天这个不一样,今天是我们的一个社团要投票,前些天社团的领导送给我一台旧的洗衣机,的确帮了我很大的忙,为了表示感谢,我我去研究了一下怎么帮他在评比中刷票。
因为涉及隐私我就不截图了。刚开始投票都是在微信群里发的,我正好电脑挂着微信,用浏览器打开,打开抓包工具,投了一票,抓数据包看了一下。卧槽传了十几个参数,其中还有我的微信ID,还有时间戳,然后还有几个参数没搞明白是什么,没有IP。
于是先尝试写个脚本,修改一下微信ID和时间戳,看看能不能投票成功,悲剧的是没有投票成功。
这时候我再想想,如果不带微信ID能不能投票成功,于是开了个IE浏览器,把连接复制上去,投票的时候抓个数据包看看,竟然能投票成功了。所以这个投票还是有漏洞的,没有绑定微信也能投票成功。关闭了浏览器后重新打开连接投票时提示已投了,猜想可能跟缓存cookie有关,于是清除所有缓存,继续投票OK了。
明白了这个套路,原来只是绑定cookie而已,于是拿起python写了脚本去投票,发现还是投不成功,蛋疼,因为还有几个随机参数我不知道怎么去造。
这时候想起之前用的selenium,每次打开不都是一个全新的浏览器吗?好的,那就用selenium去写。写好放服务器上去跑,效率是挺低的,一分钟只能投三票,浏览器加载实在是太慢了,不过想想慢一点也蛮好的,免得刷票太明显了。
晚上脚本挂服务器,第二天早上起来,卧槽比第二名足足多了快两千票,吓得我赶紧关了。不过也能保证获奖了。
所以为什么我这么讨厌这种投票的活动,最后都会变成刷票了,还有频繁的要朋友帮忙投票,也会欠下很多人情。
附录脚本:
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class Case4(unittest.TestCase):
def setUp(self):
self.verificationErrors = []
self.accept_next_alert = True
def test_case4(self):
while True:
try:
self.driver = webdriver.Firefox()
driver=self.driver
driver.implicitly_wait(5)
driver.get("http://xxx.com")
driver.maximize_window()
#滚动条拉到底部
js="var q=document.documentElement.scrollTop=10000"
driver.execute_script(js)
driver.find_element_by_xpath("/html/body/div[2]/div[46]/div[4]").click()
driver.find_element_by_xpath("//div[@class='pageTwo page']").click()
driver.find_element_by_xpath("/html/body/div[2]/div[47]/div[3]/div[2]/div[4]/div[3]/div[1]/div[1]").click()
driver.find_element_by_xpath("//div[@class='pageThree page']").click()
driver.find_element_by_xpath("/html/body/div[2]/div[47]/div[3]/div[3]/div[8]/div[3]/div[1]/div[1]").click()
driver.find_element_by_xpath("//div[@class='pagefour page']").click()
driver.find_element_by_xpath("/html/body/div[2]/div[47]/div[3]/div[4]/div[3]/div[3]/div[1]/div[1]").click()
driver.find_element_by_xpath("//div[@class='pagefive page']").click()
driver.find_element_by_xpath("/html/body/div[2]/div[47]/div[3]/div[5]/div[4]/div[3]/div[1]/div[1]").click()
driver.quit()
except:
driver.quit()
print "error"
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException, e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
也可以用python的requests中session的方法,进行持续连接
import requests
import json
import random
def vote(usragent):
session = requests.Session()
r = session.get("http://xxx.com/?app_act=detail&id=16&from=timeline&isappinstalled=0")
headers = {
"Accept": "*/*",
"If-Modified-Since": '0',
"Referer": "http://xxx.com/?app_act=detail&id=16&from=timeline&isappinstalled=0",
"Accept-Language": "zh-CN",
"Accept-Encoding": "gzip, deflate",
"User-Agent": usragent,
}
r1 = session.get("http://xxx.com/?app_ajax=1&app_act=vote&id=16&itemid=1",headers = headers,cookies=r.cookies)
r2 = session.get("http://xxx.com/?app_ajax=1&app_act=vote&id=16&itemid=1",headers = headers,cookies=r.cookies)
json1 = json.loads(r1.text)
json2 = json.loads(r2.text)
if json1['code'] == json2['code'] and json1['code'] == 0:
print "Vote Success!"
return 2
else:
print "Vote Fail!"
return 0
if __name__ == "__main__":
number = raw_input("Please enter the number of votes")
agent = []
with open("agent.txt","r") as f:
lines = f.readlines()
for line in lines:
agent.append(line.strip('\n'))
total = 0
for i in range(0,int(number)):
total += vote(agent[random.randint(0,len(agent))])
print "A total of [%s] votes"%total