刷票一般要突破以下限制:

1、验证码识别

2、同一ip不可连续投票

解决办法

1、用tesseract工具,链接在此 https://code.google.com/p/tesseract-ocr/ (人人还是加不了https链接)

2、使用代理,国内可以的代理服务器可以从这里找到 http://cn-proxy.com/

程序语言当然用python

浏览器投票的流程如下

1、向服务器发送请求,服务器返回验证码和表单

2、填好表单,发送到服务器

可以用firefox+httpfox插件查看整个事件过程,以及发送请求的POST和GET数据格式

使用python的urllib2库实现上述过程

1、向服务器请求验证码

可以用

import urllib

urllib.urlretrieve(imgurl,imgfile)#imgurl可以从页面源代码找到

但是由于每次请求服务器,都会重新生成验证码,所以这样请求道的验证码即便识别出来,再POST进去时也会提示错误。解决办法时使用cookie

import cookie

import urllib2

cookiejar=cookielib.MozillaCookieJar(cookieFilename)

opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))

response=opener.open(imgurl)

如果需要使用代理服务器,则可以这样写

import cookie

import urllib2

proxy_line='127.0.0.1:8087'

cookieFilename='cookie.txt'

cookiejar=cookielib.MozillaCookieJar(cookieFilename)

opener=urllib2.build_opener(urllib2.ProxyHandler({'http':proxy_line}),urllib2.HTTPCookieProcessor(cookieFileJar))

response=opener.open(imgurl,timeout=2)#设置超时时间

这样就是以本机8087端口带有cookie验证访问服务器,下一步识别出验证码,依然带cookie POST进服务器即可。

2、保存验证码到本地

上面response即返回得到的验证码二进制流,写入文件用下面代码

content=response.read()

fp=file(imgfile,'wb')#将二进制图片保存

fp.write(content)

fp.close()

3、验证码识别

验证码识别用tesseract,由于tesseract没有提供python接口,这里用系统调用外部命令

用法为

import os

imgfile='img.jpg'

out

用python刷票代码如下

# -*- coding: utf-8 -*-

import os

import urllib

import urllib2

import string

import socks

import httplib2

import cookielib

import time

import random

tes='tesseract.exe'

filepath='./'

imgurl='http://example.com/vote/img.jsp'

myurl="http://example.com/vote"

voteInfoId='xxxxxxxx'

headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0'}#,

# 'Cookie':'324E661DE12427BD71CE63DF436A80D1'}

imgfile=filepath + '/img.jpg'

outfile=filepath + '/out'

proxy_file=filepath+'/proxy.txt'

user_file=filepath+'/user2.txt'

cookieFilename=filepath +'/cookies.txt'

#myproxy_line='211.142.236.137:80'

#cookieJarFileLWP=cookielib.LWPCookieJar(cookieFilename)

cookieFileJar=cookielib.FileCookieJar(cookieFilename)

#opener=urllib2.build_opener(urllib2.ProxyHandler({'http':myproxy_line}),urllib2.HTTPCookieProcessor(cookieMozillaJar))

opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieFileJar))

f_user=open(user_file)

f_proxy=open(proxy_file)

proxy_all=f_proxy.read().split('\n')

f_user=open(user_file)

user_all=f_user.read().split('\n')

count=1

cookieFileJar=cookielib.FileCookieJar(cookieFilename)#使用cookie登陆

while True:

i=random.randint(1,100)

j=random.randint(1,60)

#print user_all.length()

user_line=user_all[i]

myproxy_line=proxy_all[j]

for myproxy_line in [myproxy_line]:#f_proxy:

#使用代理和cookie

opener=urllib2.build_opener(urllib2.ProxyHandler({'http':myproxy_line}),urllib2.HTTPCookieProcessor(cookieFileJar))

try:

response2=opener.open(imgurl,timeout=1)#返回二进制图片

except Exception,e:

print 'I can not connect the server,try again'

continue

content=response2.read()

fp=file(imgfile,'wb')#将二进制图片保存

fp.write(content)

fp.close()

outcmd="%s %s %s -l eng digits -psm 7" %(tes,imgfile,outfile)

print 'I begin to recognize the CAPTCHA code ..'

os.system(outcmd)

code_file=open(filepath+'/out.txt')

mycode_line=code_file.readline()

code_file.close()

if len(mycode_line)<=3:

print 'I guess the CAPTCHA code is %s,but I think it\'s error.' % (mycode_line)

continue

mycode=mycode_line[0:4]

print 'I guess the CAPTCHA code is %s' % (mycode)

mylist=user_line.split('----')

proxy_list=myproxy_line.split(':')

myid=mylist[0]#.decode('utf-8')

myname=mylist[1]#.decode('utf-8')

mycomm=mylist[2]#.decode('utf-8')

data={'method':'vote',

'voteInfoId':voteInfoId,

'forward':'***',

'info1':myid,

'info2':myname,

'info3':mycomm,

'inputCode':mycode,

'submit':'确定'}

print 'Now I begin to vote...'

print 'the user is %s' % (myid)

print 'the name is %s' % (myname)

print 'the comment is %s' % (mycomm)

post_data=urllib.urlencode(data)

try:

response=opener.open(myurl,post_data)

except Exception,e:

print 'I can\'t connect the server ,so vote is failure'

continue

content=response.read()

#img_req=opener.open(imgurl)

#cookieFileJar.save(cookieFilename)

#req=opener.open(myurl,post_data)

#req=urllib2.Request(myurl,data=post_data,headers=headers)

fp=file('test.html','w')

print 'I put the received html to the file test.html'

fp.write(content)

fp.close()

#outcmd="%s %s %s" %(tes,imgfile,outfile)

#count+=1

#print cookieFileJar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

python自动填写小程序表单_python小程序实现刷票功能详解相关推荐

  1. python简单计算器综合实验报告_Python实现的简单计算器功能详解

    本文实例讲述了Python实现的简单计算器功能.分享给大家供大家参考,具体如下: 使用python编写一款简易的计算器 计算器效果图 首先搭建计算器的面板: 计算器面板结构 建造一个继承于wx.Fra ...

  2. python实现简单计算器功能键介绍_Python实现的简单计算器功能详解

    本文实例讲述了Python实现的简单计算器功能.分享给大家供大家参考,具体如下: 使用python编写一款简易的计算器 计算器效果图 首先搭建计算器的面板: 计算器面板结构 建造一个继承于wx.Fra ...

  3. python 数列筛选_对numpy中的数组条件筛选功能详解

    在程序设计中,时常会遇到数据的唯一化.相同.相异信息的提取等工作,在格式化的向量存储矩阵中南,numpy能够提供比较不错的快速处理功能. 1,唯一化的实现: In [63]: data = np.ar ...

  4. python标准类型内建模块_Python内建模块struct实例详解

    本文研究的主要是Python内建模块struct的相关内容,具体如下. Python中变量的类型只有列表.元祖.字典.集合等高级抽象类型,并没有像c中定义了位.字节.整型等底层初级类型.因为Pytho ...

  5. python爬虫多线程是什么意思_python爬虫中多线程的使用详解

    queue介绍 queue是python的标准库,俗称队列.可以直接import引用,在python2.x中,模块名为Queue.python3直接queue即可 在python中,多个线程之间的数据 ...

  6. python分析方向的第三方库_Python标准库与第三方库详解

    干货大礼包!21天带你轻松学Python(文末领取更多福利) 点击查看课程视频地址 本课程来自于千锋教育在阿里云开发者社区学习中心上线课程<Python入门2020最新大课>,主讲人姜伟. ...

  7. python中values是什么意思_Python values()与itervalues()的用法详解

    dict 对象有一个 values() 方法,这个方法把dict转换成一个包含所有value的list,这样,我们迭代的就是 dict的每一个 value: d = { 'Adam': 95, 'Li ...

  8. python for i in range(len())_Python for i in range ()用法详解

    for i in range ()作用: range()是一个函数, for i in range () 就是给i赋值: 比如 for i in range (1,3): 就是把1,2依次赋值给i r ...

  9. python中is和 的区别_Python中is和==的区别详解

    Python中有很多运算符,今天我们就来讲讲is和==两种运算符在应用上的本质区别是什么. 在讲is和==这两种运算符区别之前,首先要知道Python中对象包含的三个基本要素,分别是:id(身份标识) ...

最新文章

  1. golang的mongodb操作(mgo)
  2. 蜘蛛仅靠电场就能起飞,连风都不需要,网友:懂了,蜘蛛会用原力
  3. 绿色计算在数据中心的应用及节能效果浅析
  4. 高性能日志框架 Log4a 原理分析
  5. Numpy 排序 -- sort()、argsort()
  6. 清华大学人工智能研究院成立智能信息获取研究中心
  7. 福建物联网产业发展势头良好
  8. WPF实现一个彩虹按钮
  9. LeetCode 294. 翻转游戏 II(记忆化递归)
  10. 36日期计算包含计算某月某日是星期几的公式
  11. 2020年的海报设计,掌握7种风格,稳了
  12. ‘python3\r’: No such file or directory
  13. python爬虫什么意思-python爬虫什么意思
  14. c++中模板的实现(模板类和模板函数)
  15. ActiveMQ笔记(二)
  16. 共享单车的定位,应该是利用手机实现
  17. Mac版idea快速切换大小写快捷键
  18. tomcat监控脚本(监控进程,测试接口,告警动作为发送邮件)
  19. elementui后台管理demo
  20. 手把手教你对接阿里云短信服务

热门文章

  1. 蒸汽幻想服务器修改,月影科技-蒸汽幻想服务端(附带网站+单机IP)
  2. C++、easyx组合的界面版五子棋(适合新手)
  3. 软件架构实践(第三版)
  4. 惹某人持续划水的开学第二周(习题+感悟)
  5. zeal使用教程_Zeal离线文档下载速度慢解决方案
  6. windows下载安装Zeal
  7. OSI七层模型简单介绍
  8. 3.1.5 spark体系之分布式计算-scala编程-scala中的集合(数组array、list、set、map、元组)
  9. springboot和netty整合的web聊天室
  10. 华南虎事件给媒体界的一些思考