# -*- coding: utf-8 -*- # !/usr/bin/env python import os, sys,time,datetime import urllib import requests from mongoengine import register_connection, PointField, DynamicDocument, StringField,Document import simplejson as json import simplejson as json import base64 import sys import json import base64 import urllib2 from urllib import quote_plus from urllib2 import urlopen from urllib2 import Request from urllib2 import URLError from urllib import urlencode # from django.db.models.fields import DateTimeField # # # os.environ.setdefault("DJANGO_SETTINGS_MODULE", "configs.testing") # # from script.base import init_env # # init_env(interactive = False) import pyautogui import qrcode IS_PY3 = sys.version_info.major == 3 if IS_PY3: from urllib.request import urlopen from urllib.request import Request from urllib.error import URLError from urllib.parse import urlencode from urllib.parse import quote_plus else: import urllib2 from urllib import quote_plus from urllib2 import urlopen from urllib2 import Request from urllib2 import URLError from urllib import urlencode # 防止https证书校验不正确 import ssl import pytesseract from PIL import Image # while True: # a = pyautogui.getWindowsWithTitle(u'图片查看') # time.sleep(1) ssl._create_default_https_context = ssl._create_unverified_context # 利用百度APP,直接解析截图中的地址,以及端口编号。 API_KEY = 'OVcN78LP40CBEwWk5REF2Hyu' SECRET_KEY = 'a7luZBdbzjsfU9oE2GD3yPeTBgPty03t' OCR_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic" TOKEN_URL = 'https://aip.baidubce.com/oauth/2.0/token' register_connection(alias = 'spider', name = 'spider', host = '211.159.224.10', port = 27119, username = 'service', password = 'oOzjoQcO5DyyiN97AY0NpzJ6vztjNpx5', authentication_source = 'admin') class wangzhuang(Document): devNo = StringField(default = '') phone = StringField(default = '') meta = { 'collection': 'wangzhuangXiaochengxu', 'db_alias': 'spider', 'unique_together': {'devNo'} } def fetch_token(): params = {'grant_type': 'client_credentials', 'client_id': API_KEY, 'client_secret': SECRET_KEY} post_data = urlencode(params) if (IS_PY3): post_data = post_data.encode('utf-8') req = Request(TOKEN_URL, post_data) try: f = urlopen(req, timeout=5) result_str = f.read() except URLError as err: print(err) if (IS_PY3): result_str = result_str.decode() result = json.loads(result_str) if ('access_token' in result.keys() and 'scope' in result.keys()): if not 'brain_all_scope' in result['scope'].split(' '): print ('please ensure has check the ability') exit() return result['access_token'] else: print ('please overwrite the correct API_KEY and SECRET_KEY') exit() request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic" # accurate_basic def read_file(image_path): f = None try: f = open(image_path, 'rb') return f.read() except: print('read image file fail') return None finally: if f: f.close() def request(url, data): req = Request(url, data.encode('utf-8')) has_error = False try: f = urlopen(req) result_str = f.read() if (IS_PY3): result_str = result_str.decode() return result_str except URLError as err: print(err) def get_zone_text(region): file1 = pyautogui.screenshot(region=region) picPath = u'Q:/temp_chaoxiang/' + '%s.jpg' % int(time.time()*1000) file1.save(picPath) string = pytesseract.image_to_string(Image.open(picPath), lang='chi_sim') print picPath,string return string.replace(' ','') def get_text_from_baidu(region): try: file1 = pyautogui.screenshot(region=region) picPath = u'Q:/temp_chaoxiang/' + '%s.jpg' % int(time.time()*1000) file1.save(picPath) f = open(picPath, 'rb') img = base64.b64encode(f.read()) if f: f.close() # 调用文字识别服务 params = {"image":img} request_url1 = request_url + "?access_token=" + token headers = {'content-type': 'application/x-www-form-urlencoded'} response = requests.post(request_url1, data=params, headers=headers) # 解析返回结果 result_json = response.json() for words_result in result_json["words_result"]: text = words_result["words"] return text return '' except Exception,e: print u"解析图片报错了!!!!",e # 二进制方式打开图片文件 # 获取access token token = fetch_token() def back_to_message(): stringTitle = get_zone_text(region=(886,180,170,35)) stringLeft = get_zone_text(region=(782,183,228,27)) if u'陈军' in stringTitle: return True elif u'充电支付' in stringLeft: pyautogui.click(x=1167, y=198) # 直接点击关闭按钮 time.sleep(1) elif u'通话' in stringLeft: pyautogui.click(x=1265, y=436) time.sleep(1) pyautogui.click(x=1265, y=436) time.sleep(1) stringTitle = get_zone_text(region=(886,180,170,35)) if u'陈军' in stringTitle: return True else: return False # 从30万启动开始 for ii in range(0,300000): jj = 300000 - ii print jj qrStr = 'https://www.ctiot.info/d/18%06d' % jj img =qrcode.make(qrStr) filePath = "Q:/temp/%s" % jj img.save(filePath) time.sleep(0.2) weixin = pyautogui.getWindowsWithTitle(u'微信')[0] if not weixin: break weixin.activate() time.sleep(3) # 点击文件按钮 pyautogui.click(x=761, y=777) time.sleep(0.2) # 输入二维码文件名称 pyautogui.click(x=1083, y=928) pyautogui.write("%s" % jj) time.sleep(0.2) # 选择该文件,传到编辑框 with pyautogui.hold('alt'): pyautogui.press('o') time.sleep(0.1) # 发消息 with pyautogui.hold('alt'): pyautogui.press('s') time.sleep(0.1) # --------------------------------- 手机这边识别二维码------------------------- shouji = pyautogui.getWindowsWithTitle(u'手机管理器')[0] if not shouji: break shouji.activate() time.sleep(3) if not back_to_message(): print u'无法定位到消息接受窗口' break # 点击图片 pyautogui.click(x=1067, y=782) time.sleep(1) # 点击图片的二维码识别按钮 pyautogui.click(x=1025, y=864) time.sleep(3) string = get_zone_text(region=(782,183,228,27)) if u'充电支付' not in string: print u'没有启动小程序,需要等等吧' time.sleep(1800) string = get_zone_text(region=(847,568,82,30)) if u'消' in string: pyautogui.click(x=847, y=568) time.sleep(0.2) # devNo = get_text_from_baidu(region=(855,278,68,21)) # if devNo and devNo != '18%06d' % jj: # print u'设备编号错误,可能是二维码没有发过来',devNo,jj # break string = get_text_from_baidu(region=(1122,262,72,40)) if not u'电话' in string : continue # 点击联系方式 pyautogui.click(x=1154, y=278) time.sleep(1) #解析电话 string = get_zone_text(region=(813,431,331,46)) if not string: continue try: newObj = wangzhuang(str(jj),string) newObj.save() print u'搞到一条信息!!!',string except Exception,e: continue print 'OK'