1. 程式人生 > 程式設計 >Python實現手機號自動判斷男女性別(例項解析)

Python實現手機號自動判斷男女性別(例項解析)

本文性別判斷主要依靠airtest中的自動化測試實現

通過自動對比支付寶頁面男女影象,從而實現男女判斷

程式碼如下:

男女判斷函式:

// An highlighted block
def numbe():
  if exists(Template(r"tpl1574867500094.png",threshold=0.85,rgb=True,target_pos=0,record_pos=(0.779,0.382),resolution=(960,540))):
    sex = "女"   
  if exists(Template(r"tpl1574924960910.png",threshold=0.89,target_pos=5,540))):
    sex = "男"
  else:
    sex = "不存在"
  namesex = sex
  keyevent("4")
  return namesex

手機滑動(根據手機解析度自行調整):

// An highlighted block
def scoll():
  try:
    swipe(v1=(629,1750),v2=(629,310)) # 滑動距離需要根據手機解析度自行調整        
  except:
    print("can't go back to the main page")

刷選函式:

// An highlighted block
def number():  
  data_list =[]
  for i in range(9): # 根據手機解析度自行調整
    try:
      title =poco(name="com.alipay.mobile.contactsapp:id/contact_item_name")[i].get_text()
      name = poco(name="com.alipay.mobile.contactsapp:id/concast_from")[i].get_text()
      print(title)
      name_a =name[5:6]
      if title not in data_list and name_a is not "1":
        poco("com.alipay.mobile.contactsapp:id/contact_item_name")[i].click()       
        sexname=numbe()      
        if sexname =="男":
          print(str(sexname))
      
        else:
          print(str(sexname))
          
      else:
        print(name_a)
        print("不存在")
    except:
      print("出錯,跳過!")

綜合:

// An highlighted block
# -*- encoding=utf8 -*-
__author__ = "liuqingsong"
def numbe():
  if exists(Template(r"tpl1574867500094.png",540))):
    sex = "男"
  else:
    sex = "不存在"
  namesex = sex
  keyevent("4")
  return namesex
def scoll():
  try:
    swipe(v1=(629,310)) # 滑動距離需要根據手機解析度自行調整        
  except:
    print("can't go back to the main page")

def number():  
  data_list =[]
  for i in range(9): # 根據手機解析度自行調整
    try:
      title =poco(name="com.alipay.mobile.contactsapp:id/contact_item_name")[i].get_text()
      name = poco(name="com.alipay.mobile.contactsapp:id/concast_from")[i].get_text()
      print(title)
      name_a =name[5:6]
      if title not in data_list and name_a is not "1":
        poco("com.alipay.mobile.contactsapp:id/contact_item_name")[i].click()       
        sexname=numbe()      
        if sexname =="男":
          print(str(sexname))
          with open(r'./new/男.csv','a',encoding='utf-8') as f:
            f.write("{},{}\n".format(title,sexname))
        else:
          print(str(sexname))
          with open(r'./new/女.csv',sexname))
      else:
        print(name_a)
        print("不存在")
    except:
      print("出錯,跳過!")
a=0
while a<5:#根據手機上號碼量的多少自行選擇
  number()
  scoll()
  sleep(1)
  a=a+1

以上是用的是airtest實現的,效率不是很高,同樣進行簡單改動可以實現支付寶真實號碼篩選,效率很高,偶爾使用一下還是可以的,切不可用於非法用途,大家有什麼好的方式歡迎留言!

總結

以上所述是小編給大家介紹的Python實現手機號自動判斷男女性別,希望對大家有所幫助!