用python写刷票程序
刷票一般要突破以下限制:
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、向服务器请求验证码
可以用
+BEGIN python
import urllib
urllib.urlretrieve(imgurl,imgfile)#imgurl可以从页面源代码找到
+END
但是由于每次请求服务器,都会重新生成验证码,所以这样请求道的验证码即便识别出来,再POST进去时也会提示错误。解决办法时使用cookie
+BEGIN python
import cookie
import urllib2
cookiejar=cookielib.MozillaCookieJar(cookieFilename)
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
response=opener.open(imgurl)
+END
如果需要使用代理服务器,则可以这样写
+BEGIN python
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)#设置超时时间
+END
这样就是以本机8087端口带有cookie验证访问服务器,下一步识别出验证码,依然带cookie POST进服务器即可。
2、保存验证码到本地
上面response即返回得到的验证码二进制流,写入文件用下面代码
+BEGIN python
content=response.read()
fp=file(imgfile,'wb')#将二进制图片保存
fp.write(content)
fp.close()
+END
3、验证码识别
验证码识别用tesseract,由于tesseract没有提供python接口,这里用系统调用外部命令
用法为
+BEGIN 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写并行程序
目录 一.大数据时代的现状 二.面对挑战的方法 2.1 并行计算 2.2 改用GPU处理计算密集型程序 3.3 分布式计算 三.用python写并行程序 3.1 进程与线程 3.2 全局解释器锁GIL ...
- hadoop学习笔记——用python写wordcount程序
尝试着用3台虚拟机搭建了伪分布式系统,完整的搭建步骤等熟悉了整个分布式框架之后再写,今天写一下用python写wordcount程序(MapReduce任务)的具体步骤. MapReduce任务以来H ...
- 开发记录_自学Python写爬虫程序爬取csdn个人博客信息
每天刷开csdn的博客,看到一整个页面,其实对我而言,我只想看看访问量有没有上涨而已... 于是萌生了一个想法: 想写一个爬虫程序把csdn博客上边的访问量和评论数都爬下来. 打算通过网络各种搜集资料 ...
- 十行代码--用python写一个USB病毒 (知乎 DeepWeaver)
昨天在上厕所的时候突发奇想,当你把usb插进去的时候,能不能自动执行usb上的程序.查了一下,发现只有windows上可以,具体的大家也可以搜索(搜索关键词usb autorun)到.但是,如果我想, ...
- Python初学者随笔(一)_ 用Python写的第一个游戏“猜数字”
如标题所写,这篇随笔主要记录下学习Python过程中用Python写的第一个游戏--"猜数字"_跟着"小甲鱼"学Python,链接: https://b23.t ...
- 用python写个简单的小程序,编译成exe跑在win10上
每天的工作其实很无聊,早知道应该去IT公司闯荡的.最近的工作内容是每逢一个整点,从早7点到晚11点,去查一次客流数据,整理到表格中,上交给素未蒙面的上线,由他呈交领导查阅. 人的精力毕竟是有限的,所以 ...
- Python写一个自动点餐程序
Python写一个自动点餐程序 为什么要写这个 公司现在用meican作为点餐渠道,每天规定的时间是早7:00-9:40点餐,有时候我经常容易忘记,或者是在地铁/公交上没办法点餐,所以总是没饭吃,只有 ...
- 使用Python写的第一个网络爬虫程序
今天尝试使用python写一个网络爬虫代码,主要是想訪问某个站点,从中选取感兴趣的信息,并将信息依照一定的格式保存早Excel中. 此代码中主要使用到了python的以下几个功能,因为对python不 ...
- 使用Python写Windows Service服务程序
1.背景 如果你想用Python开发Windows程序,并让其开机启动等,就必须写成windows的服务程序Windows Service,用Python来做这个事情必须要借助第三方模块pywin32 ...
随机推荐
- Oracle 表空间和用户权限管理【转】
一. 表空间 Oracle数据库包含逻辑结构和物理结构. 数据库的物理结构指的是构成数据库的一组操作系统文件. 数据库的逻辑结构是指描述数据组织方式的一组逻辑概念以及它们之间的关系. 表空间是数据库逻 ...
- 整理了一份React-Native学习指南
原文: http://www.w3ctech.com/topic/909 自己在学习React-Native过程中整理的一份学习指南,包含 教程.开源app和资源网站等,还在不断更新中.欢迎pull ...
- Java Day03 面向对象程序设计
1.面向对象 面向对象是指一种程序设计泛型,同时也是一种程序开发的方法. 2.类 类是一种抽象的概念,类中包含了数据与对数据的操纵. 具有相同特性(数据元素)和行为(功能)的对象的抽象就是类.类是对象 ...
- ZeroMQ中PUB-SUB模式测试
因为公司有需求,对程序模块之间通信效率有较高的需求.之前公司用的通信组件是ActiveMQ,根据网上公布的测试结果显示其效率比较低, 后来考虑准备在新的项目中开始使用ZeroMQ.看了几天发现用起来比 ...
- C#隐藏(new)方法和重写(override)方法
在基类调用的时候 隐藏方法还是调用基类的方法 而重写方法调用的就是子类的中的方法 同时,当子类中的方法与父类或者所实现的接口中的扩展方法冲突时,那么此时相当于一个隐藏方法 基类调用或者接口调用的时候使 ...
- Linux_Cytoscape
- Strusts2--课程笔记8
文件的和上传和下载: (1)文件的上传: Struts是通过拦截器实现文件上传的,而默认拦截器栈中包含了文件上传拦截器,故表单通过Struts2可直接将文件上传,其底层是通过apache的common ...
- uva 12356 Army Buddies
简单的并查集应用. #include<stdio.h> #include<string.h> #include<math.h> #include<algori ...
- hdu1022
#include <stdio.h>#include <string.h> int main(void){ int n,i,j,k,l; char o1[10],o2[10], ...
- hdu1020
#include <stdio.h> int main(void){ int n,i,c; char txt[10001]; scanf("%d", &n); ...