用python实现查询移动手机余额话费的代码
001 #!/usr/bin/env python
002 #encoding=utf-8
003
004 import Image
005 import sys
006 import urllib, urllib2, cookielib
007 import cmd
008 import re
009 import StringIO
010
011 class Mobile(cmd.Cmd):
012
013 def __init__(self):
014 cmd.Cmd.__init__(self)
015 self.intro= “”"
016 请输入 help 查看帮助
017 “”"
018 self.prompt = “Yidong> ”
019 self.form = {
020 ‘submitMode’:’2′,
021 ‘ErrorUrl’:’../briefLogon.do’,
022 ‘ReturnURL’:'www.sd.10086.cn/newecare/common/prior.jsp’,
023 ‘FieldID’:’1′,
024 ‘entrance’:'IndexBrief’,
025 ‘mobileNum’:”,
026 ‘logonMode’:’1′,
027 ‘servicePWD’:”,
028 ‘randCode’:”,
029 ‘smsRandomCode’:”
030 }
031 self.formAction = ‘http://www.sd.10086.cn//portal/servlet/LoginServlet’
032 self.mobilePage = ‘http://www.sd.10086.cn/newecare/common/prior.jsp’
033 cookie = cookielib.CookieJar()
034 cookie.clear()
035 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
036 opener.addheaders = [
037 ('User-agent', 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.91 Safari/534.30'),
038 ('Referer', 'http://www.sd.10086.cn/portal/briefLogon.do'),
039 ('Accept-Language', 'zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3'),
040 ('Accept-Encoding', 'gzip, deflate'),
041 ('Host', 'www.sd.10086.cn'),
042 ]
043 urllib2.install_opener(opener)
044
045 def _getCode(self):
046 urlImg = ‘http://sd.10086.cn/portal/sms/briefValidateCode.jsp’
047 imgs = (
048 (0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,0,1,1,1,1,0,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,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,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,)
058 )
059 fp = StringIO.StringIO(urllib2.urlopen(urllib2.Request(urlImg)).read())
060 im = Image.open(fp)
061 im = im.crop((8,5,52,20))
062 code=”
063 for i in range(4):
064 im_new = im.crop((11*i,0,11*(i+1),15))
065 width, height = im_new.size
066 l = []
067 for i in range(width):
068 for j in range(height):
069 c1, c2, c3 = im_new.getpixel((i, j))
070 if (c1 < 40) and (c2 < 40) and (c2 < 40) :
071 l.append(1)
072 else:
073 l.append(0)
074 n=0
075 for img in imgs:
076 same=0
077 for i in range(165):
078 if img[i] == l[i]:
079 same += 1
080 if same > 150:
081 code += str(n)
082 break
083 n += 1
084 return code
085
086 def do_ye(self, info):
087 ”’
088 查询话费: Yidong>ye 手机号码 服务密码
089 Yidong> ye 15153006103 888888
090 ”’
091
092 arg = info.split(‘ ‘)
093 if len(arg) <> 2:
094 print ‘error 89!’
095 return 0
096
097 print ‘waiting … ‘,
098 sys.stdout.flush()
099
100 self.form['mobileNum'] = arg[0]
101 self.form['servicePWD'] = arg[1]
102 self.form['randCode'] = self._getCode()
103 req = urllib2.Request(self.formAction, urllib.urlencode(self.form))
104 doc = urllib2.urlopen(req).read()
105 reU = re.compile(‘(www.sd.10086.cn/newecare/common/prior.jsp;ssojsessionid=\S+)”‘, re.S)
106 result = reU.findall(doc)
107 try:
108 doc = urllib2.urlopen(urllib2.Request(‘http://’+result[0])).read()
109 doc = urllib2.urlopen(urllib2.Request(‘http://www.sd.10086.cn/portal/servlet/CookieServlet?FieldID=2′)).read()
110 except:
111 print ‘error: 98!’
112 return 0
113 reA = re.compile(“‘(\S+)’”, re.S)
114 result = reA.findall(doc)
115 url = ‘http://www.sd.10086.cn/newecare/loginAttritd.do?Attritd=%s&randnum=6010.6788671377135&menuID=null’ % (result[0])
116 urllib2.urlopen(urllib2.Request(url))
117
118 doc = urllib2.urlopen(urllib2.Request(‘http://www.sd.10086.cn/newecare/loginSuccess.jsp’)).read()
119 reYE = re.compile(“您的余额:(\S+)元”, re.S)
120 result = reYE.findall(doc)
121 try:
122 print result[0]+”元”
123 except:
124 print ‘error: 109!’
125
126 def main():
127 mobile = Mobile()
128 try:
129 mobile.cmdloop()
130 except KeyboardInterrupt:
131 print “bye”
132
133 if __name__ == “__main__”:
134 main()