使用 Python 进行HTTP代理 多线程刷(投)票

这几天朋友参加比赛需要,就写了个脚本帮忙刷票.
我们首先来到要投票的网站上来看看。
随便找一个投上一票

使用 Python 进行HTTP代理 多线程刷(投)票_第1张图片
居然不用登陆,当然是每个IP只能投一票。
打开Chrome dev tools, 看一下
Get请求。
那直接把Request URL复制下来,这个就是用于刷票的URL
按理来说只要把这个URL 发给任何一个人诱惑他点开,就是帮你投票了。

使用 Python 进行HTTP代理 多线程刷(投)票_第2张图片
他返回了一个Json数据格式。告诉我们已经投过票了。

然后我们登VPN 换一个IP 试试看。

使用 Python 进行HTTP代理 多线程刷(投)票_第3张图片
Json “Total” 名称 就是当前的票数。“result”为true 显然告诉我们投票成功了。
那的确是这样的。

然后想要刷票呢,我们需要找一些开放HTTP代理的IP。我找了半天然后推荐这个网站 可以直接抓取IP到这种格式。

使用 Python 进行HTTP代理 多线程刷(投)票_第4张图片

然后接下来就是写脚本了。
由于我没有长期接触过Python 网络方面的编程。以前也只是看着用Requests库爬了一些小网站的数据。
可以先看下这篇博文

#coding=utf-8
import urllib2
import urllib
import re
import threading
import sys
from time import ctime
import time
rlock = threading.RLock()
def vote(proxyIP,i,urls):
    try:
        #print "voting...%d..." % i
        #使用代理IP
        proxy_support = urllib2.ProxyHandler(proxyIP)
        opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)
        #定义Opener

        urllib2.install_opener(opener)
        #把opener绑定到全局

        sendt = '投票'.decode('utf-8').encode('gb2312')

        #设置刷票地址
        #post数据bn
        values = {}
        req = urllib2.urlopen(urls)
        #直接打开这个URL
        html = req.read()
        #读取返回数据
        if html.find('true'.decode('utf-8').encode('gb2312')):
            print "投票 [%d] 成功" % i
            return 1
        else:
            print "投票 [%d] 失败" % i
            return 0;
    except Exception:
        return False

if __name__ == "__main__":
    args = sys.argv
    if(len(args) == 3):
        ipFile = open(args[1]);
        ipList = ipFile.readlines()
        ipFile.close()
        length = range(len(ipList))
        threads = []
        for i in length:
            ipLine = ipList[i]

            ip=ipLine.strip()
            proxy_ip = {'http': ip}
            t = threading.Thread(target=vote,args=(proxy_ip,i,args[2]))
            print "get ",args[2],ip
            threads.append(t)
        for i in length:
            threads[i].start();
            if i%100:
                time.sleep(5)
                #每100个线程等待 5秒
        for i in length:
            threads[i].join()

    else:
        print """刷票工具
                python brush.py IP文件 Get地址:

                """

然后我们运行来看看结果
原来的票数

这里写图片描述

运行了15分钟之后

这里写图片描述

你可能感兴趣的:(Python)