python通过手机号获取微信男女性别

不用图像识别,就可以拿到微信性别。

先上一张图片,

通过红框里的小头像可以看出是男是女,或者未知。

我们可以通过判断头像来获取性别,识别男女。

但是太麻烦和复杂了。

其实我们可以通过界面元素进行拾取当前性别

首先打开安卓的界面层级拾取工具    一般位于安卓sdk包下tools下      的uiautomatorviewer.bat

我们发现根本不需要通过图像识别,就轻而易举的拿到性别了。

这是手动获取的,那我们怎样自动获取呢。

我们可以通过程序进行获取,

比如 appium自动化测试工具,adb命令,android自动化测试Uiautomator框架 等。。

首先appium 需要配置很多环境,Uiautomator框架要在android里实现。

而adb命令比较通用,python调用adb也很方面。

那我们就用adb写自动获取代码。

通过uiautomatorviewer.bat我们拿出当前的xml代码

(点击uiautomatorviewer界面上左上角水平第四个按钮保存即可)

当前页面的全部xml代码(经过格式化以后的排版,默认是一行):

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<hierarchy rotation="0">
	<node index="0" text="" resource-id="" class="android.widget.FrameLayout" package="com.tencent.mm" content-desc="当前所在页面,详细资料" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1080,1920]">
		<node index="0" text="" resource-id="com.tencent.mm:id/dgn" class="android.widget.FrameLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1080,1920]">
			<node index="0" text="" resource-id="" class="android.widget.FrameLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1080,1920]">
				<node index="0" text="" resource-id="" class="android.widget.LinearLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1080,1920]">
					<node index="0" text="" resource-id="" class="android.widget.FrameLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,60][1080,1920]">
						<node index="0" text="" resource-id="com.tencent.mm:id/gz" class="android.view.ViewGroup" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,60][1080,1920]">
							<node index="0" text="" resource-id="com.tencent.mm:id/h0" class="android.widget.FrameLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,60][1080,204]">
								<node index="0" text="" resource-id="com.tencent.mm:id/h1" class="android.view.ViewGroup" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,60][1080,204]">
									<node index="0" text="" resource-id="com.tencent.mm:id/gw" class="android.widget.LinearLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,60][912,204]">
										<node index="0" text="" resource-id="com.tencent.mm:id/i1" class="android.widget.LinearLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,60][146,204]">
											<node index="0" text="" resource-id="com.tencent.mm:id/i2" class="android.widget.ImageView" package="com.tencent.mm" content-desc="返回" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,60][144,204]" />
											<node index="1" text="" resource-id="com.tencent.mm:id/hf" class="android.widget.ImageView" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[144,96][146,168]" /></node>
										<node index="1" text="" resource-id="" class="android.widget.LinearLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[182,60][398,204]">
											<node index="0" text="详细资料" resource-id="android:id/text1" class="android.widget.TextView" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[182,95][398,168]" /></node>
									</node>
									<node index="1" text="" resource-id="" class="android.support.v7.widget.LinearLayoutCompat" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[912,60][1080,204]">
										<node index="0" text="" resource-id="" class="android.widget.LinearLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[912,60][1080,204]">
											<node index="0" text="" resource-id="" class="android.widget.RelativeLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[912,60][1080,204]">
												<node index="0" text="" resource-id="com.tencent.mm:id/hi" class="android.widget.ImageButton" package="com.tencent.mm" content-desc="更多" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[912,60][1080,204]" /></node>
										</node>
									</node>
								</node>
							</node>
							<node index="1" text="" resource-id="android:id/content" class="android.widget.FrameLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,204][1080,1920]">
								<node index="0" text="" resource-id="com.tencent.mm:id/c8f" class="android.widget.FrameLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,204][1080,1920]">
									<node index="0" text="" resource-id="com.tencent.mm:id/c_z" class="android.widget.LinearLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,204][1080,1920]">
										<node index="0" text="" resource-id="android:id/list" class="android.widget.ListView" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="true" focused="true" scrollable="false" long-clickable="true" password="false" selected="false" bounds="[0,204][1080,1920]">
											<node index="0" text="" resource-id="android:id/title" class="android.widget.TextView" package="com.tencent.mm" content-desc="分隔栏" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,204][1080,264]" />
											<node index="1" text="" resource-id="" class="android.widget.LinearLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,264][1080,726]">
												<node index="0" text="" resource-id="com.tencent.mm:id/anj" class="android.widget.LinearLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,264][1080,528]">
													<node index="0" text="" resource-id="com.tencent.mm:id/pz" class="android.widget.ImageView" package="com.tencent.mm" content-desc="头像" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[42,300][234,492]" />
													<node index="1" text="" resource-id="" class="android.widget.LinearLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[306,300][1044,383]">
														<node index="0" text="" resource-id="com.tencent.mm:id/ank" class="android.widget.LinearLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[306,306][1044,371]">
															<node index="0" text="李鹏 " resource-id="com.tencent.mm:id/q0" class="android.widget.TextView" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[306,306][414,371]" />
															<node index="1" text="" resource-id="com.tencent.mm:id/anl" class="android.widget.ImageView" package="com.tencent.mm" content-desc="男" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[423,308][484,369]" /></node>
													</node>
												</node>
												<node index="1" text="" resource-id="" class="android.widget.TextView" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,528][1080,529]" />
												<node index="2" text="" resource-id="com.tencent.mm:id/anz" class="android.view.View" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,529][1080,589]" />
												<node index="3" text="" resource-id="" class="android.widget.LinearLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,589][1080,726]">
													<node index="0" text="设置备注和标签" resource-id="com.tencent.mm:id/ao0" class="android.widget.TextView" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,589][1080,726]" /></node>
											</node>
											<node index="2" text="" resource-id="android:id/title" class="android.widget.TextView" package="com.tencent.mm" content-desc="分隔栏" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,726][1080,786]" />
											<node index="3" text="" resource-id="" class="android.widget.LinearLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="false" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,786][1080,930]">
												<node index="0" text="" resource-id="com.tencent.mm:id/ij" class="android.widget.LinearLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="false" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[42,786][1038,930]">
													<node index="0" text="" resource-id="com.tencent.mm:id/jg" class="android.widget.LinearLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="false" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[42,801][496,914]">
														<node index="0" text="" resource-id="" class="android.widget.LinearLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="false" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[42,825][288,890]">
															<node index="0" text="地区" resource-id="android:id/title" class="android.widget.TextView" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="false" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[42,825][288,890]" /></node>
														<node index="1" text="" resource-id="com.tencent.mm:id/c_d" class="android.widget.LinearLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="false" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[306,829][496,885]">
															<node index="0" text="" resource-id="" class="android.widget.LinearLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="false" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[306,829][496,885]">
																<node index="0" text="河南  信阳" resource-id="android:id/summary" class="android.widget.TextView" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="false" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[306,829][496,885]" /></node>
														</node>
													</node>
												</node>
											</node>
											<node index="4" text="" resource-id="android:id/title" class="android.widget.TextView" package="com.tencent.mm" content-desc="分隔栏" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,930][1080,990]" />
											<node index="5" text="" resource-id="" class="android.widget.LinearLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,990][1080,1170]">
												<node index="0" text="添加到通讯录" resource-id="com.tencent.mm:id/an_" class="android.widget.Button" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[42,990][1038,1134]" /></node>
											<node index="6" text="" resource-id="android:id/title" class="android.widget.TextView" package="com.tencent.mm" content-desc="分隔栏" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,1170][1080,1230]" />
											<node index="7" text="" resource-id="" class="android.widget.LinearLayout" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,1230][1080,1310]">
												<node index="0" text="" resource-id="android:id/title" class="android.widget.TextView" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,1230][1080,1310]" /></node>
										</node>
									</node>
								</node>
							</node>
						</node>
					</node>
				</node>
			</node>
		</node>
		<node index="1" text="" resource-id="android:id/statusBarBackground" class="android.view.View" package="com.tencent.mm" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1080,60]" /></node>
</hierarchy>

包含所要信息的一行xml的代码:

<node index="1" text="" resource-id="com.tencent.mm:id/anl" class="android.widget.ImageView" package="com.tencent.mm" content-desc="男" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[423,308][484,369]" /></node>

发现 性别在  content-desc="男" 这个地方  ,  

一共有三种状态 

1   content-desc="男"   

2   content-desc="女"

3   就是 连这个xml源码也没有  ,也就是对方性别未知的意思。

全部的xml源码都是以node为节点,我们要做的就是匹配出    男 女 未知

那我们首先要找寻到上面那一行xml部分源码  

也就是找到包含    resource-id="com.tencent.mm:id/anl" 为基准的节点,包含在<node></node>

为什么不直接去匹配 男 女这种字眼呢?

原因是姓名和地区中可能包含 男 女 字眼,我们要准确匹配。

所以我们先找到包含resource-id="com.tencent.mm:id/anl"的这一行,

因为resource-id="com.tencent.mm:id/anl"在全部的xml代码中是唯一的,

再从这一行中拿出性别,那就很准确了。

 

现在思路就是:

1 首先获取手机设备

2 电脑端使用python程序通过adb命令控制手机

3 拿出手机端的页面

4 进行匹配并打印

开始我们的编写我们的代码:

 

import os,re
# 获取所有的手机设备
def getDevicesAll():
    devices = []
    try:
        for dName_ in os.popen("adb devices"):
            if "\t" in dName_:
                if dName_.find("emulator") < 0:
                    devices.append(dName_.split("\t")[0])
        devices.sort(cmp=None, key=None, reverse=False)
    except:
        pass
    print(u"\n设备名称: %s \n总数量:%s台" % (devices, len(devices)))
    return devices
# 导出手机端当前页面元素
def export(devices_name):
    # 页面xml保存到手机存储
    step1 = os.popen('adb -s {0} shell uiautomator dump /sdcard/{1}.xml'.format(devices_name,devices_name)).read()
    if step1.find('UI') != -1:
        print(devices_name+'\txml写进手机存储成功')
        # 页面xml从手机存储推送到电脑,和当前运行程序同级
        os.popen('adb -s {0} pull /sdcard/{1}.xml '.format(devices_name,devices_name))
        print(devices_name+"\txml推送到电脑成功")
        with open(devices_name + ".xml",encoding="utf-8") as f:
            res = f.read()
        return res
    else:
        print(devices_name+"\txml写进手机存储失败")
    return "null"
# 匹配函数
def check_line(res,find_str):
    list1 = re.findall( '<[^<]*?' + find_str + '[^>]*?>', res)
    if list1:
        return list1[0]
    return "null"

def main():
    # 以resource-id为基准进行匹配
    findstr = 'resource-id="com.tencent.mm:id/anl"'
    devices = getDevicesAll()
    for d_name in devices:
        res = export(d_name)
        if res!="null":
            result = check_line(res,findstr)
            if result=="null":
                print("此手机号性别未知")
            elif result.find("男") != -1:
                print("此手机号性别男")
            elif result.find("女") != -1:
                print("此手机号性别女")
if __name__=="__main__":
    main()

 

主要代码就是上面的,

getDevicesAll函数获取所有连接电脑的usb手机设备,拿到设备名字

export函数获取当前连接的手机的页面

check_line函数拿到准确的一行,拿不到说明是未知性别

main函数进行逻辑判断等

 

我提供的主要是思路以及主要代码。

那些adb输入手机号,点击搜索,返回,重新搜索一系列操作都很简单,

就不啰嗦了。还有很多设备同时搜索控制进行识别手机号等,需要你们自己完成。

不过主要代码已经在上面了。你们进行自己定制更改即可。

 

对了,有的电脑上可能没有adb工具,百度一下adb安装等字眼,配置一下就好了。

 

转发请备注博主链接。

欢迎进(Q)群,帮你解决问题:

 

 

 

 

 

 

展开阅读全文
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值