123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- # -*- 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
- # 超翔的需要利用android手机,打开小程序,然后拿到数据。我直接用的影刀连接android手机(必须调试模式,运行模拟点击的调试模式),然后再通过PC自动化进行操作
-
- 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 chaoxiang(Document):
- devNo = StringField(default = '')
- phone = StringField(default = '')
- meta = {
- 'collection': 'chaoxiang',
- '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)
-
- # 二进制方式打开图片文件
- # 获取access token
- token = fetch_token()
- # 拼接通用文字识别高精度url
- #
- 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')
- return string.replace(' ','')
-
- def back_to_search():
- string = get_zone_text(region=(782,183,228,27))
- if u'启动设备' in string:
- pyautogui.click(x=1265, y=436)
- time.sleep(1)
- elif u'搜索' in string:
- return True
- elif u'通话' in string:
- pyautogui.click(x=1265, y=436)
- time.sleep(1)
- pyautogui.click(x=1265, y=436)
- time.sleep(1)
- elif u'首页' in string:
- pyautogui.click(x=1090, y=241)
- time.sleep(1)
-
- string = get_zone_text(region=(782,183,228,27))
- if u'搜索' in string:
- return True
- else:
- return False
-
- 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
- #
- # string = get_text_from_baidu(None)
- for ii in range(0,259889):
- jj = 259889 - ii
- if jj < 100000:
- break
- print jj
- shouji = pyautogui.getWindowsWithTitle(u'手机管理器')[0]
- if not shouji:
- break
- shouji.activate()
- time.sleep(3)
-
- if not back_to_search():
- print u'无法定位到搜索界面'
- break
-
- # 点击搜索框
- pyautogui.doubleClick(x=1066, y=249,interval=0.1)
- time.sleep(1)
-
- # 点击删除按钮,把可能的多余的删除掉
- pyautogui.click(x=1172, y=806,clicks=10,interval=0.1)
-
- time.sleep(0.1)
-
- # 输入设备ID号
- pyautogui.write("%s" % jj)
- time.sleep(0.1)
-
- # 点击搜索
- pyautogui.click(x=1170, y=868)
- time.sleep(1)
-
- # 检查是否有搜索结果
- string = get_zone_text(region=(828,316,332,100))
- print string
- if u'无' in string:
- print u'没有找到此设备',jj
- continue
-
- # 点击设备,多等下,因为广告比较慢
- pyautogui.click(x=1069, y=343)
- time.sleep(5)
-
- # 点击可能打开的广告,没有广告,多点击一次也无所谓
- pyautogui.click(x=1003,y=197)
- time.sleep(0.5)
-
- # 检查是否有联系商户,没有直接返回
- string = get_text_from_baidu(region=(1138,765,61,32))
- if not u'联系' in string:
- print u'没有找到联系方式按钮'
- result = back_to_search()
- if result:
- continue
- else:
- print u',请人工干预'
- break
-
- # 点击联系方式
- pyautogui.click(x=1167,y=775)
- time.sleep(1)
-
- #解析电话
- string = get_zone_text(region=(813,431,331,46))
- try:
- newObj = chaoxiang(str(jj),string)
- newObj.save()
- print u'搞到一条信息!!!',string
- except Exception,e:
- continue
-
- result = back_to_search()
- if result:
- continue
- else:
- print u'无法返回到搜索页面,请人工干预'
- break
- # time.sleep(0.1)
- #
- #
- # while True:
- # a= auto.GetFocusedControl()
- # time.sleep(2)
- # print a
- # print a.Name
- # time.sleep(2)
- # while True:
- # pyautogui.moveTo(987,574)
- # file1 = pyautogui.screenshot(region=(766,220,412,575))
- # file1.save(u'Q:/友商信息/汽车桩/星星充电/站列表截图/%s.png' % int(time.time()*1000))
- # ii += 1
- # print ii
- # pyautogui.scroll(-400)
- # time.sleep(0.2)
-
- print 'OK'
|