自动投票程序

下面是前几天写的一个自动投票的程序,代码比较粗糙,过几天整理一下:

# -*- coding: cp936 -*- import httplib, urllib, urllib2, re # GenerateParams def generateParams(ticketNum, ipAddr, recoreId): params = urllib.urlencode( { "ps2":ticketNum, "ip":ipAddr, "sid":"", "MM_insert":"form1", "MM_update":"form1", "MM_recordId":recoreId, } ) return params # GenerateIP=============================== startIpLine = 20700 f_ipdata = open('ipdata.txt', 'r') for i in range(startIpLine): f_ipdata.readline() def generateIp(): global f_ipdata pattern = re.compile(r'/d+/./d+/./d+/./d+') ip = pattern.findall(f_ipdata.readline())[0] return ip #GenerateTicketNum def generateTicketNum(startNum, recordId): fin = urllib2.urlopen('some url') contents = fin.read() fin.close() pattern = re.compile(r'id=%d.*/n.*票数.*>(/d+)</span>' % recordId) lastTicketNum = pattern.findall(contents) #startNum argument is not useful now if lastTicketNum == []: return startNum ticketNum = str(int(lastTicketNum[0]) + 1) return ticketNum # Must change each time params = generateParams( ticketNum, generateIp(), 29) # num : def postNTimes(startTicketNum, sleepTime, num, recordId): uri = 'some url' headers = { 'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.13) Gecko/2009073022 Firefox/3.0.13', 'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml; q=0.9,*/*; q=0.8', 'Content-Type': 'application/x-www-form-urlencoded' } ticketNum = startTicketNum import time for i in range(num): try: ticketNum = generateTicketNum(ticketNum, recordId) ip = generateIp() print ip, ticketNum params = generateParams(ticketNum, ip, recordId) post(uri, params, headers) time.sleep(sleepTime) except: print 'exception' time.sleep(60) def post(uri, params, headers): req = urllib2.Request(uri, params, headers) f= urllib2.urlopen(req) ##if __name__ == '__main__': ## postNTimes('1739',10, 1500, 40)

你可能感兴趣的:(自动投票程序)