前言
现在生活中离不开各类的比赛,然而,各个比赛离不开投票,我们会清一色有时候找到我们的朋友在朋友圈发 — 帮宝贝投一票,帮某某老师,学生投一票。又或许你自己参加比赛,你在为你自己拉票。
作为一名程序员,你是否想为自己的生活开一点 G 呢?熟悉网络请求的我们,应该从问题根源分析问题。对于一个投票的网站。大致分为两类:
既然原理已经剖析完成,那么剩下的就是设计程序的问题了,对于一个点击投票的事件,它的实质就是一次 http (post) 请求,然后后台对数据进行更改。那么我们就可以对这个操作流程进行抓包,分析这个请求是那种类型,需要那些参数。然后根据这个请求模拟写出请求。
然而最重要的就是 ip 代理,你要用代理的 ip 去访问那个接口,让对方以为是你代理的那个 ip 再对他访问,所以你需要维护一个代理 ip 池。对于代理 ip 池,并不是什么高大上的东西,准确的来说就是一个集合中包含一些可用的 ip,能够供我使用。市面上也有很多出售代理 ip,也不贵。我用的是蘑菇代理。
碰巧,最近参加的一个比赛就有拉票环节,如果人为手动拉票的话效率地下,并且你肯定也不会愿意天天去舔人家求情。那就自己分析一波!
找到 url 和几个参数,就可以准备程序了。模拟请求了
因为这是多次请求,所以要考虑性能的问题和效率问题。不能让异常漫天飞,中断,ip 白白浪费,或者苦苦等待吧。 对于代理 ip,各家卖的虽然有些差异但是大体相同。大致均为卖数量,然后每个 ip 从开始被用后能够维持几分钟的使用。并且有的 ip 是不能用的,有的是高延迟的,这些在写程序的时候都要过滤掉。这里面就要考虑下这个程序额设计。
import requests
import random
import time
import threading
from queue import Queue
def loadip():##从代理ip中获取ip 一次若干扩充到queue中
url2 = 'http://piping.mogumiao.com/proxy/api/get_ip_al?appKey=f16367295e284173ae450f&count=20&expiryDate=0&format=1&newLine=2'
req = requests.get(url2)
date = req.json()
if(date['code'])!='3001':
ipdate2 = date['msg']
for va in ipdate2:
que.put(va)
class downspider(threading.Thread):##线程类
def __init__(self, threadname, que):
threading.Thread.__init__(self)
self.threadname = threadname
self.que = que
def run(self):
print('start thread' + self.threadname)
while True:
try:
toupiaospider(que,self.threadname)##投票函数
except Exception as e:
print(e,'888')
break
def getproxies():#获取ip 拼接成需要的代理格式
b=que.get()
d = '%s:%s' % (b['ip'], b['port'])
global proxies
proxies['http'] = d
return proxies
def toupiaospider(que,threadname):
if (que.qsize() < 15): # 拓展ip池
loadip()
proxies2=getproxies()
for i in range(0,5):
try:
#formData['times']=i
req = requests.post(url, headers=header, data=formData, proxies=proxies2, timeout=1.5)
res = req.json()
if res['res']==2001 or req.status_code!=200:
continue
print(threadname,proxies2['http'],res,que.qsize())
except Exception as e:
print('errror',e)
if __name__ == '__main__':
proxies = {'http': ''}
stadus = 0
que = Queue()
threads=[]#线程
url='http://yunxin.163.com/api/vote/update'
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'}
formData = {
'Referer':'http://yunxin.163.com/promotion/minichallenge/gallery?from=groupmessage&isappinstalled=0',
'id':'17',
'times':'1',
'activity':'minichallenge1'
}
proxies = {'http': '182.247.92.99:21136',
}
loadip()
time.sleep(5)
##线程数组 ->启动 ——>等待join
threadList = ['thread-1','thread-2','thread-3','thread-4','thread-4','thread-5']
for j in threadList:
thread = downspider(j, que)
thread.start()
threads.append(thread) for t in threads:
t.join()
结果
在 java 中比较棘手的就是 java 自身对 json 和 http 请求的处理不太方便,需要借助第三方 jar,并且一些操作稍显的繁琐。
首先 java 要弄清几点:
针对上面的问题。写了个 demo 测试进行预备,对于获取 ip 的 api,大致这种格式
首先你要下载 fastjson 和 jsoup 的 jar 包。或者加入 maven 依赖。(可在 maven 官网下 jar 包)
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.12.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
然后写个 demo 跑一下
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有