标签: 2018
引言:
其实这一篇本来不是写这个的,在周一开始想做的时候就想好了标题:
《Android与Python的巧妙结合抓取公众号所有历史文章》,
搜狗仅显示最近10条群发,想做爬公号历史文章的应该都了解过,
而利用无障碍服务+Python实现的思路很简单:
之所以要执行第八步的原因是保存到本地的HTML直接打开很多图片都会裂开(链接失效)
然后呢,我花了三天都没折腾出来,最终卡在无法获得微信x5浏览器网页节点 这个节骨眼
上,无法再进分毫…
无障碍服务利用的是UI Automation,然后呢,这玩意是不支持WebView的!!!、
尽管能遍历打印出结点的信息,但是模拟点击一点用都没有…
webview里的东西,AccessibilityService就无能为力了。我…
后来群里童鞋说移动端自动化测试框架Appuim支持webview,我立马花了半天
去撸文档(不得不说相比起AccessibilityService只能依靠res-id和text来查找
节点,appium支持xpath等方式,找节点,不能爽太多,不过缺点的话是
反应会慢一些,特别是在edittext输入的时候),但是这里有个坎又把我给卡住了
appuim每次运行自己的脚本,微信都需要重新登录,每次运行都要登录的结果
就是:我的三个小号因为登录过于频繁,当天都无法登录了…
(有知道如何规避每次运行脚本都要重新的登录的务必告知下~)
放弃了,太耗费时间了!
另外还有一个套路是微信利用的x5浏览器,可以在微信里打开这个链接:
debugx5.qq.com
接着选择信息,勾选:
接着打开谷歌浏览器,输入:chrome://inspect/#devices
接着随手打开一篇文章,就会有这样的东西:
点击inspect,然后可以得到整个页面的结构,可以拿到和
搜狗获取的一样类型的文章链接,链接很长,而且失效都是12小时
如果是手机用户点击查看文章,然后复制链接,是这样的短连接,
时效还不知道是多久(Maybe永久):
http://mp.weixin.qq.com/s/O00w469tkOrr507drGln9A
关于如何获得微信公众号历史文章目前知道的比较简单的套路就这些,
至于抓包破解之类的,目前level还不够,折腾不过来,先放一放,
后面说不定get√了新的姿势点,问题一下子就解了呢?嘿嘿~
另外爬取微信文章里图片,音频视频可见之前写过的:
小猪的Python学习之旅 —— 10.三分钟上手Requests库
上节利用AccessibilityService实现了自动加好友和拉人进群聊,
感觉还没用起来,这不,又加了自动抢红包和朋友圈自动点赞~
另外,在AccessibilityService除了Webview这个坑外,通过red-id来
查找控件,在每个版本的APP中都可能是不一样的,比如微信就是,
本节代码仅保证在6.6.6版本上是可用的,其他版本需要你自行去
适配啦!另外,项目是用Kotlin写的哈,顺道复习一波语法~
这个肯定是没有xposed抢得快的,就是监听Notification信息,如果有
微信红包的字眼,自动打开聊天页面,遍历列表,找到未打开的红包,
自动点开,领取后自动关闭。
运行效果图:
啧啧,三个红包几下子就抢完了,速度还是杠杠的,了解了原理,以后换其他
应用,比如钉钉之类的,另外这个脚本目前只支持抢页面可见的红包,
可以开启listview滚动监听,滑动的时候去判断页面有无红包,然后抢。
不过监听了这个玩意的话,页面会挺卡的,不建议开启,自己点红包,
直接就能开了~
这个功能就不说啦,及时点赞,拉近同事朋友感情,每天点赞那么勤,
别人还以为你对她有意思呢?啧啧,有兴趣的还可以自行扩展,对别
人的说说内容进行下过滤,根据不同的内容发送不同的评论~
比如:
附:
PS:说想接入机器人自动回复评论的,只利用AccessibilityService的话就
别想了,你们怕是忘了AccessibilityService是用来优化残障人士的使用体验的吧…
如果要接机器人的话,考虑appuim自动化测试吧~
运行效果图:
是的,就是这么屌~
本节开始讲了下利用类似于按键精灵的套路获得公众号所有历史文章的套路,
接着写了两个很实用的实例,抢红包和朋友圈点赞,关于AccessibilityService
的东东基本也就那么多了,后续再折腾这些就该上xposed了~
附:关键代码(都可以在:https://github.com/coder-pig/WechatHelper 找到):
class HelperService : AccessibilityService() {
private val TAG = "HelperService"
private val handler = Handler()
private var nameList = mutableListOf()
override fun onInterrupt() {
}
override fun onAccessibilityEvent(event: AccessibilityEvent) {
val eventType = event.eventType
val classNameChr = event.className
val className = classNameChr.toString()
Log.d(TAG, event.toString())
when (eventType) {
AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED -> {
if (Hawk.get(Constant.RED_PACKET, false)) {
when (className) {
"com.tencent.mm.ui.LauncherUI" -> openRedPacket()
"com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyReceiveUI" -> clickRedPacket()
"com.tencent.mm.plugin.luckymoney.ui.LuckyMoneyDetailUI" -> performBackClick()
}
//com.tencent.mm:id/ad8
}
if (Hawk.get(Constant.ADD_FRIENDS, false) && Hawk.get(Constant.GROUP_NAME, "") != "") {
when (className) {
"com.tencent.mm.plugin.subapp.ui.friend.FMessageConversationUI" -> addFriends()
"com.tencent.mm.plugin.profile.ui.SayHiWithSnsPermissionUI" -> verifyFriend()
"com.tencent.mm.plugin.profile.ui.ContactInfoUI" -> contactInfo()
"com.tencent.mm.ui.LauncherUI" -> openGroup()
"com.tencent.mm.ui.contact.ChatroomContactUI" -> {
if (nameList.size > 0) searchGroup() else performBackClick()
}
"com.tencent.mm.ui.chatting.ChattingUI" -> openGroupSetting()
"com.tencent.mm.plugin.chatroom.ui.ChatroomInfoUI" -> {
if (nameList.size > 0) addToGroup() else performBackClick()
}
"com.tencent.mm.ui.base.i" -> dialogClick()
}
}
if (Hawk.get(Constant.FRIEND_SQUARE,false)) {
if (className == "com.tencent.mm.plugin.sns.ui.SnsTimeLineUI") {
autoZan()
}
}
}
AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED -> {
if (event.parcelableData != null && event.parcelableData is Notification) {
val notification = event.parcelableData as Notification
val content = notification.tickerText.toString()
if (content.contains("[微信红包]")) {
val pendingIntent = notification.contentIntent
try {
pendingIntent.send()
} catch (e: PendingIntent.CanceledException) {
e.printStackTrace()
}
}
}
}
//滚动的时候也去监听红包,不过有点卡
// AccessibilityEvent.TYPE_VIEW_SCROLLED -> {
// if (className == "android.widget.ListView") {
// openRedPacket()
// }
// }
}
}
//添加好友
private fun addFriends() {
val nodeInfo = rootInActiveWindow
if (nodeInfo != null) {
val list = nodeInfo.findAccessibilityNodeInfosByText("接受")
if (list != null && list.size > 0) {
list[0].performAction(AccessibilityNodeInfo.ACTION_CLICK)
val nameText: List? = list[0].parent.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/b8s")
nameList.add(nameText?.get(0)?.text.toString())
} else {
performBackClick()
}
}
}
//完成验证
private fun verifyFriend() {
val nodeInfo = rootInActiveWindow
if (nodeInfo != null) {
val finishNode = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/hh")[0]
finishNode.performAction(AccessibilityNodeInfo.ACTION_CLICK)
}
}
//好友详细资料页
private fun contactInfo() {
val nodeInfo = rootInActiveWindow
if (nodeInfo != null) {
val nameNode = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/q0")[0]
Log.i(TAG, nameNode.toString())
if (nameList.contains(nameNode.text.toString().trim())) performBackClick()
}
}
//打开群聊
private fun openGroup() {
if (nameList.size > 0) {
val nodeInfo = rootInActiveWindow
if (nodeInfo != null) {
val tabNodes = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/c9f")
for (tabNode in tabNodes) {
if (tabNode.text.toString() == "通讯录") {
tabNode.parent.performAction(AccessibilityNodeInfo.ACTION_CLICK)
handler.postDelayed({
val newNodeInfo = rootInActiveWindow
if (newNodeInfo != null) {
val tagNodes = newNodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/jk")
for (tagNode in tagNodes) {
if (tagNode.text.toString() == "群聊") {
tagNode.parent.parent.performAction(AccessibilityNodeInfo.ACTION_CLICK)
break
}
}
}
}, 500L)
}
}
}
}
}
//搜索群聊
private fun searchGroup() {
val nodeInfo = rootInActiveWindow
if (nodeInfo != null) {
val nodes = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/a9t")
for (info in nodes) {
if (info.text.toString() == Hawk.get(Constant.GROUP_NAME)) {
info.parent.performAction(AccessibilityNodeInfo.ACTION_CLICK)
break
}
}
}
}
//打开群聊设置
private fun openGroupSetting() {
if (nameList.size > 0) {
val nodeInfo = rootInActiveWindow
if (nodeInfo != null) {
nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/hi")[0].performAction(AccessibilityNodeInfo.ACTION_CLICK)
}
}
}
//添加到群聊里
private fun addToGroup() {
if (nameList.size > 0) {
val nodeInfo = rootInActiveWindow
if (nodeInfo != null) {
val listNodes = nodeInfo.findAccessibilityNodeInfosByViewId("android:id/list")
if (listNodes != null && listNodes.size > 0) {
val listNode = listNodes[0]
listNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD)
listNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD)
val scrollNodeInfo = rootInActiveWindow
if (scrollNodeInfo != null) {
handler.postDelayed({
val nodes = scrollNodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/cz1")
for (info in nodes) {
if (info.contentDescription.toString() == "添加成员") {
info.parent.performAction(AccessibilityNodeInfo.ACTION_CLICK)
break
}
}
}, 1000L)
handler.postDelayed({
val editNodes = rootInActiveWindow.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/arx")
if (editNodes != null && editNodes.size > 0) {
val editNode = editNodes[0]
val arguments = Bundle()
arguments.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, nameList[0])
editNode.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments)
nameList.removeAt(0)
}
}, 2300L)
handler.postDelayed({
val cbNodes = rootInActiveWindow.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/l7")
if (cbNodes != null) {
var cbNode: AccessibilityNodeInfo? = null
if (cbNodes.size == 1) {
cbNode = cbNodes[0]
} else if (cbNodes.size == 2) {
cbNode = cbNodes[1]
}
if (cbNode != null) {
cbNode.parent.performAction(AccessibilityNodeInfo.ACTION_CLICK)
val sureNode = rootInActiveWindow.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/hh")[0]
sureNode.performAction(AccessibilityNodeInfo.ACTION_CLICK)
}
}
}, 3000L)
}
}
}
}
}
//对话框处理
private fun dialogClick() {
val inviteNode = rootInActiveWindow.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/all")[0]
inviteNode.performAction(AccessibilityNodeInfo.ACTION_CLICK)
handler.postDelayed({
val sureNodes = rootInActiveWindow.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/all")
if (sureNodes != null && sureNodes.size > 0) {
val sureNode = sureNodes[0]
sureNode.performAction(AccessibilityNodeInfo.ACTION_CLICK)
}
}, 1000L)
}
//自动点赞
private fun autoZan() {
val nodeInfo = rootInActiveWindow
if (nodeInfo != null) {
while (true) {
val rootNode = rootInActiveWindow
if (rootNode != null) {
val listNodes = rootNode.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/ddn")
if (listNodes != null && listNodes.size > 0) {
val listNode = listNodes[0]
val zanNodes = listNode.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/dao")
for (zan in zanNodes) {
zan.performAction(AccessibilityNodeInfo.ACTION_CLICK)
Thread.sleep(300)
val zsNodes = rootInActiveWindow.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/d_m")
Thread.sleep(300)
if (zsNodes != null && zsNodes.size > 0) {
if (zsNodes[0].findAccessibilityNodeInfosByText("赞").size > 0) {
zsNodes[0].performAction(AccessibilityNodeInfo.ACTION_CLICK)
}
}
Thread.sleep(300)
}
listNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD)
}
} else {
break
}
}
}
}
//遍历获得未打开红包
private fun openRedPacket() {
val rootNode = rootInActiveWindow
if(rootNode != null) {
val listNode = rootNode.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/a_c")
if (listNode != null && listNode.size > 0) {
val msgNodes = listNode[0].findAccessibilityNodeInfosByViewId("com.tencent.mm:id/ad8")
if (msgNodes != null && msgNodes.size > 0) {
for(rpNode in msgNodes) {
val rpStatusNode = rpNode.findAccessibilityNodeInfosByText("领取红包")
if (rpStatusNode != null && rpStatusNode.size > 0) {
rpNode.performAction(AccessibilityNodeInfo.ACTION_CLICK)
break
}
}
}
}
}
}
//打开红包
private fun clickRedPacket() {
val nodeInfo = rootInActiveWindow
val clickNode = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/c31")
if (clickNode != null && clickNode.size > 0) {
clickNode[0].performAction(AccessibilityNodeInfo.ACTION_CLICK)
} else {
performBackClick()
}
}
private fun performBackClick() {
handler.postDelayed({ performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK) }, 300L)
}
}
来啊,Py交易啊
想加群一起学习Py的可以加下,智障机器人小Pig
验证通过后会自动发送群聊链接加群链接,点击加入即可
(不要和机器人聊天=-=,就挂着拉人的,有问题到群里讲!)
欢迎各种像我一样的Py初学者,Py大神加入,一起愉快地交流学♂习,van♂转py。