如何利用Airtest爬取指定好友的微信朋友圈

本文介绍如何用Airtest爬取指定好友的微信朋友圈,爬取内容为文字,表情,缩略图,最后输出html,主要应用div标签

  1. 用数据线将手机与电脑连起来

  1. 将Airtest与手机连接(点connect)

  1. 打开一个好友的朋友圈,这里打开一全是广告的

  1. 新建一个air文件贴入如下代码

# -*- encoding=utf8 -*-
__author__ = "123456"#随便起名
import sys
import re
from airtest.core.api import *
# 报错
# import sys,io
# sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030')

sys.path.append(r'C:\app\AirtestIDE-win-1.2.14\script_airtest1\airtest_app1.air\wechat_moment_friends2.air')#html_文字的储存目录
name_customer="abc老师"
str_LogdirPath=r"C:\app\AirtestIDE-win-1.2.14\script_airtest1\airtest_app1.air\wechat_moment_friends2.air\\"+name_customer
set_logdir(str_LogdirPath)#html_图片的储存目录
from tmoji import EMOJI_UNICODE
auto_setup(__file__)
from airtest.aircv import *
from poco.drivers.unity3d import UnityPoco
poco = UnityPoco()
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)
poco.swipe([0.1,0.5],[0.1,0.242],duration=0.01)
html_name=name_customer+".html"
f = open(html_name,'w')
message = """

"""
f.write(message)
f.close()
k=0
last_li_IdBr8_day=''
last_li_IdBr8_month=''
last_li_IdBr8_year=''
last_li_IdBr8_year_html=''
last_li_IdBr8_text=''
last_li_text=''
List_last_content=['1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1']
Tail_item=''
while k==0:
    li_IdBr8=poco(name="com.tencent.mm:id/br8")#获得单元
    i=0
    j=0
    l=0
    content_print=''
    content_html=''
    while i<len(li_IdBr8):
        if (poco("com.tencent.mm:id/kai").child("android.widget.FrameLayout").child("android.widget.LinearLayout").offspring("com.tencent.mm:id/jv8").child("com.tencent.mm:id/br8")[i].attr("pos")[1]-(poco("com.tencent.mm:id/kai").child("android.widget.FrameLayout").child("android.widget.LinearLayout").offspring("com.tencent.mm:id/jv8").child("com.tencent.mm:id/br8")[i].attr("size")[1]/2))>0.138 and (poco("com.tencent.mm:id/kai").child("android.widget.FrameLayout").child("android.widget.LinearLayout").offspring("com.tencent.mm:id/jv8").child("com.tencent.mm:id/br8")[i].attr("pos")[1]+(poco("com.tencent.mm:id/kai").child("android.widget.FrameLayout").child("android.widget.LinearLayout").offspring("com.tencent.mm:id/jv8").child("com.tencent.mm:id/br8")[i].attr("size")[1]/2))<(1-0.11958333333333333):        
            IdBr8_str_day=""
            IdBr8_str_month=""
            IdBr8_str_year=""
            IdBr8_text=""
            IdBr8_link_text=""
            IdBr8_pic_address=""
            IdBr8_str_year_html=""
            List_current_content=[]
#             print("游标:",i)            
            if li_IdBr8[i].offspring(name="com.tencent.mm:id/jsu"):
                IdBr8_str_day=li_IdBr8[i].offspring(name="com.tencent.mm:id/jsu").get_text()
#                 print("如果有日:",IdBr8_str_day)
                last_li_IdBr8_day=IdBr8_str_day
            if li_IdBr8[i].offspring(name="com.tencent.mm:id/juc"):
                IdBr8_str_month=li_IdBr8[i].offspring(name="com.tencent.mm:id/juc").get_text()
#                 print("如果有月:",IdBr8_str_month)
                last_li_IdBr8_month=IdBr8_str_month
            if poco("com.tencent.mm:id/kai").child("android.widget.FrameLayout").child("android.widget.LinearLayout").offspring("com.tencent.mm:id/jv8").child("com.tencent.mm:id/br8")[i].offspring("com.tencent.mm:id/jxl"):
                IdBr8_str_year=poco("com.tencent.mm:id/kai").child("android.widget.FrameLayout").child("android.widget.LinearLayout").offspring("com.tencent.mm:id/jv8").child("com.tencent.mm:id/br8")[i].offspring("com.tencent.mm:id/jxl").get_text()+'\n'
                IdBr8_str_year_html="""<p style="font-weight: bold;font-style: italic;">"""+IdBr8_str_year+"""&nbsp;&nbsp;&nbsp;</p>"""
            else:
                IdBr8_str_year=''
            if poco("com.tencent.mm:id/kai").child("android.widget.FrameLayout").child("android.widget.LinearLayout").offspring("com.tencent.mm:id/jv8").child("com.tencent.mm:id/br8")[i].offspring("com.tencent.mm:id/c2h"):
                IdBr8_text=poco("com.tencent.mm:id/kai").child("android.widget.FrameLayout").child("android.widget.LinearLayout").offspring("com.tencent.mm:id/jv8").child("com.tencent.mm:id/br8")[i].offspring("com.tencent.mm:id/c2h").get_text()
            if poco("com.tencent.mm:id/kai").child("android.widget.FrameLayout").child("android.widget.LinearLayout").offspring("com.tencent.mm:id/jv8").child("com.tencent.mm:id/br8")[i].child("com.tencent.mm:id/fpg").offspring("com.tencent.mm:id/c22"):
                IdBr8_text=poco("com.tencent.mm:id/kai").child("android.widget.FrameLayout").child("android.widget.LinearLayout").offspring("com.tencent.mm:id/jv8").child("com.tencent.mm:id/br8")[i].child("com.tencent.mm:id/fpg").offspring("com.tencent.mm:id/c22").get_text()
            if poco("com.tencent.mm:id/kai").child("android.widget.FrameLayout").child("android.widget.LinearLayout").offspring("com.tencent.mm:id/jv8").child("com.tencent.mm:id/br8")[i].offspring("com.tencent.mm:id/bs4").offspring("com.tencent.mm:id/gq2").offspring("com.tencent.mm:id/kpq"):
                IdBr8_text=poco("com.tencent.mm:id/kai").child("android.widget.FrameLayout").child("android.widget.LinearLayout").offspring("com.tencent.mm:id/jv8").child("com.tencent.mm:id/br8")[i].offspring("com.tencent.mm:id/bs4").offspring("com.tencent.mm:id/gq2").offspring("com.tencent.mm:id/kpq").get_text()
            if li_IdBr8[i].child(name="com.tencent.mm:id/fpg").child(name="android.widget.LinearLayout").offspring(name="com.tencent.mm:id/f5t"):  
                li_IdBr8_Picview=li_IdBr8[i].child(name="com.tencent.mm:id/fpg").child(name="android.widget.LinearLayout").offspring(name="com.tencent.mm:id/f5t")
                LeftAndTop_X=li_IdBr8_Picview.attr("pos")[0]-li_IdBr8_Picview.attr("size")[0]/2
                LeftAndTop_Y=li_IdBr8_Picview.attr("pos")[1]-li_IdBr8_Picview.attr("size")[1]/2
                RightAndBottom_X=li_IdBr8_Picview.attr("pos")[0]+li_IdBr8_Picview.attr("size")[0]/2
                RightAndBottom_Y=li_IdBr8_Picview.attr("pos")[1]+li_IdBr8_Picview.attr("size")[1]/2
                w,h=device().get_current_resolution()    
                screen1 = G.DEVICE.snapshot()
                screen1 = aircv.crop_image(screen1, (LeftAndTop_X*w,LeftAndTop_Y*h,RightAndBottom_X*w,RightAndBottom_Y*h))
                pil_img = cv2_2_pil(screen1)
                filename_pic=r"pic_"+"%(time)d.jpg" % {'time': time.time() * 1000}
                pil_img.save(str_LogdirPath+"\\"+filename_pic, quality=99, optimize=True)
                IdBr8_pic_address=name_customer+"\\"+filename_pic 
            if poco("com.tencent.mm:id/kai").child("android.widget.FrameLayout").child("android.widget.LinearLayout").offspring("com.tencent.mm:id/jv8").child("com.tencent.mm:id/br8")[i].offspring("com.tencent.mm:id/bs4").offspring("com.tencent.mm:id/f4p"):
                li_IdBr8_Picview=poco("com.tencent.mm:id/kai").child("android.widget.FrameLayout").child("android.widget.LinearLayout").offspring("com.tencent.mm:id/jv8").child("com.tencent.mm:id/br8")[i].offspring("com.tencent.mm:id/bs4").offspring("com.tencent.mm:id/f4p")
                LeftAndTop_X=li_IdBr8_Picview.attr("pos")[0]-li_IdBr8_Picview.attr("size")[0]/2
                LeftAndTop_Y=li_IdBr8_Picview.attr("pos")[1]-li_IdBr8_Picview.attr("size")[1]/2
                RightAndBottom_X=li_IdBr8_Picview.attr("pos")[0]+li_IdBr8_Picview.attr("size")[0]/2
                RightAndBottom_Y=li_IdBr8_Picview.attr("pos")[1]+li_IdBr8_Picview.attr("size")[1]/2
                w,h=device().get_current_resolution()    
                screen1 = G.DEVICE.snapshot()
                screen1 = aircv.crop_image(screen1, (LeftAndTop_X*w,LeftAndTop_Y*h,RightAndBottom_X*w,RightAndBottom_Y*h))
                pil_img = cv2_2_pil(screen1)
                filename_pic=r"pic_"+"%(time)d.jpg" % {'time': time.time() * 1000}
                pil_img.save(str_LogdirPath+"\\"+filename_pic, quality=99, optimize=True)
#                 screen1 = G.DEVICE.snapshot()
#                 screen1 = aircv.crop_image(screen1, (LeftAndTop_X*w,LeftAndTop_Y*h,RightAndBottom_X*w,RightAndBottom_Y*h))
#                 r1 = try_log_screen(screen1, quality=99)
                IdBr8_pic_address=name_customer+"\\"+filename_pic                
#                 print("缩略图大小:",li_IdBr8_Picview.attr("size"))
#                 print("缩略图位置:",li_IdBr8_Picview.attr("pos"))
            for key, value in EMOJI_UNICODE.items():
                if IdBr8_text.find(value)!=-1:
                    IdBr8_text=IdBr8_text.replace(value,'['+key+']') 
            if IdBr8_text.find('\u200b')!=-1:
                IdBr8_text=IdBr8_text.replace('\u200b','')
            if IdBr8_text.find('\xa0')!=-1:
                IdBr8_text=IdBr8_text.replace('\xa0',' ')
#             if List_last_content[i]==""
#                 List_last_content[i]=IdBr8_str_day+IdBr8_str_month+IdBr8_text
            if List_last_content.count(IdBr8_str_day+IdBr8_str_month+IdBr8_text)==0:
                List_last_content[i]=IdBr8_str_day+IdBr8_str_month+IdBr8_text
                content_print+=IdBr8_str_year+IdBr8_str_day+IdBr8_str_month+IdBr8_text+'\n'
                content_html+="""<div style="width:100%;float:left">
<div style="">"""+IdBr8_str_year_html+"""
<strong style="font-size:24">"""+IdBr8_str_day+"""</strong>&nbsp;<strong>"""+IdBr8_str_month+"""</strong></div>
<img style="float:left" src="
"""+IdBr8_pic_address+""""/>
<div style="">"""+IdBr8_text+"""</div>
</div><br>"""
#             print("如果有文字:",IdBr8_text)
#             print("每个元素:"+IdBr8_str_day+IdBr8_str_month+IdBr8_text);
#             for key,value in List_last_content:
#                 print("数组第"+str(i)+"次打印"+value)    
            j=i
    #todo:在打印之前测试是否在上一个数组里head
    #todo:在打印之前测试是否在上一个数组里tail
#             List_current_content.append(IdBr8_str_day+IdBr8_str_month+IdBr8_text)把当的item压入List_current_content里
#             List_last_content.append(IdBr8_str_day+IdBr8_str_month+IdBr8_text)
#             print('打印列表:')
#             print(List_last_content[0:])
            l+=1
        i+=1
    print(content_print)
    f = open(html_name,'a',encoding='utf-8')
    message = content_html
    f.write(message)
    f.close()
    #在小循环结束后用List_current_content代替List_last_content
    if poco("com.tencent.mm:id/kai").child("android.widget.FrameLayout").child("android.widget.LinearLayout").offspring("com.tencent.mm:id/jv8").child("android.widget.LinearLayout").offspring("android.widget.ImageView"):
        k=1
    poco.swipe([0.1,(poco("com.tencent.mm:id/kai").child("android.widget.FrameLayout").child("android.widget.LinearLayout").offspring("com.tencent.mm:id/jv8").child("com.tencent.mm:id/br8")[j+1].attr("pos")[1]-(poco("com.tencent.mm:id/kai").child("android.widget.FrameLayout").child("android.widget.LinearLayout").offspring("com.tencent.mm:id/jv8").child("com.tencent.mm:id/br8")[j+1].attr("size")[1]/2))],[0.1,0.138],duration=0.01)
Last_li_IdBr8=poco(name="com.tencent.mm:id/br8")[len(poco(name="com.tencent.mm:id/br8"))-1]
if Last_li_IdBr8.offspring("com.tencent.mm:id/jxl"):
    last_li_IdBr8_year=Last_li_IdBr8.offspring("com.tencent.mm:id/jxl").get_text()+'\n'
    last_li_IdBr8_year_html="""<p style="font-weight: bold;font-style: italic;">"""+last_li_IdBr8_year+"""&nbsp;&nbsp;&nbsp;</p>"""
if Last_li_IdBr8.offspring(name="com.tencent.mm:id/jsu"):
    IdBr8_str_day=Last_li_IdBr8.offspring(name="com.tencent.mm:id/jsu").get_text()
#     print("如果有日:",IdBr8_str_day)
    last_li_IdBr8_day=IdBr8_str_day
if Last_li_IdBr8.offspring(name="com.tencent.mm:id/juc"):
    IdBr8_str_month=Last_li_IdBr8.offspring(name="com.tencent.mm:id/juc").get_text()
    last_li_IdBr8_month=IdBr8_str_month
#     print("如果有月:",IdBr8_str_month)
if poco("com.tencent.mm:id/kai").child("android.widget.FrameLayout").child("android.widget.LinearLayout").offspring("com.tencent.mm:id/jv8").child("com.tencent.mm:id/br8")[len(poco(name="com.tencent.mm:id/br8"))-1].offspring("com.tencent.mm:id/c2h"):
    last_li_IdBr8_text=poco("com.tencent.mm:id/kai").child("android.widget.FrameLayout").child("android.widget.LinearLayout").offspring("com.tencent.mm:id/jv8").child("com.tencent.mm:id/br8")[len(poco(name="com.tencent.mm:id/br8"))-1].offspring("com.tencent.mm:id/c2h").get_text()
if poco("com.tencent.mm:id/kai").child("android.widget.FrameLayout").child("android.widget.LinearLayout").offspring("com.tencent.mm:id/jv8").child("com.tencent.mm:id/br8")[0].offspring("com.tencent.mm:id/jxl"):
    IdBr8_str_year=poco("com.tencent.mm:id/kai").child("android.widget.FrameLayout").child("android.widget.LinearLayout").offspring("com.tencent.mm:id/jv8").child("com.tencent.mm:id/br8")[0].offspring("com.tencent.mm:id/jxl").get_text()+'\n'
if poco("com.tencent.mm:id/kai").child("android.widget.FrameLayout").child("android.widget.LinearLayout").offspring("com.tencent.mm:id/jv8").child("com.tencent.mm:id/br8")[len(poco(name="com.tencent.mm:id/br8"))-1].child("com.tencent.mm:id/fpg").offspring("com.tencent.mm:id/c22"):
    last_li_IdBr8_text=poco("com.tencent.mm:id/kai").child("android.widget.FrameLayout").child("android.widget.LinearLayout").offspring("com.tencent.mm:id/jv8").child("com.tencent.mm:id/br8")[len(poco(name="com.tencent.mm:id/br8"))-1].child("com.tencent.mm:id/fpg").offspring("com.tencent.mm:id/c22").get_text()
IdBr8_pic_address=""
if Last_li_IdBr8.child(name="com.tencent.mm:id/fpg").child(name="android.widget.LinearLayout").offspring(name="com.tencent.mm:id/f5t"):  
    li_IdBr8_PicView=Last_li_IdBr8.child(name="com.tencent.mm:id/fpg").child(name="android.widget.LinearLayout").offspring(name="com.tencent.mm:id/f5t")
    LeftAndTop_X=li_IdBr8_Picview.attr("pos")[0]-li_IdBr8_Picview.attr("size")[0]/2
    LeftAndTop_Y=li_IdBr8_Picview.attr("pos")[1]-li_IdBr8_Picview.attr("size")[1]/2
    RightAndBottom_X=li_IdBr8_Picview.attr("pos")[0]+li_IdBr8_Picview.attr("size")[0]/2
    RightAndBottom_Y=li_IdBr8_Picview.attr("pos")[1]+li_IdBr8_Picview.attr("size")[1]/2
    w,h=device().get_current_resolution()    
    screen1 = G.DEVICE.snapshot()
    screen1 = aircv.crop_image(screen1, (LeftAndTop_X*w,LeftAndTop_Y*h,RightAndBottom_X*w,RightAndBottom_Y*h))
    pil_img = cv2_2_pil(screen1)
    filename_pic=r"pic_"+"%(time)d.jpg" % {'time': time.time() * 1000}
    pil_img.save(str_LogdirPath+"\\"+filename_pic, quality=99, optimize=True)
    IdBr8_pic_address=name_customer+"\\"+filename_pic    
#     print("缩略图大小:",li_IdBr8_PicView.attr("size"))
#     print("缩略图位置:",li_IdBr8_PicView.attr("pos"))
if Last_li_IdBr8.offspring("com.tencent.mm:id/bs4").offspring("com.tencent.mm:id/f4p"):
    li_IdBr8_PicView=Last_li_IdBr8.offspring("com.tencent.mm:id/bs4").offspring("com.tencent.mm:id/f4p")
    LeftAndTop_X=li_IdBr8_Picview.attr("pos")[0]-li_IdBr8_Picview.attr("size")[0]/2
    LeftAndTop_Y=li_IdBr8_Picview.attr("pos")[1]-li_IdBr8_Picview.attr("size")[1]/2
    RightAndBottom_X=li_IdBr8_Picview.attr("pos")[0]+li_IdBr8_Picview.attr("size")[0]/2
    RightAndBottom_Y=li_IdBr8_Picview.attr("pos")[1]+li_IdBr8_Picview.attr("size")[1]/2
    w,h=device().get_current_resolution()    
    screen1 = G.DEVICE.snapshot()
    screen1 = aircv.crop_image(screen1, (LeftAndTop_X*w,LeftAndTop_Y*h,RightAndBottom_X*w,RightAndBottom_Y*h))
    pil_img = cv2_2_pil(screen1)
    filename_pic=r"pic_"+"%(time)d.jpg" % {'time': time.time() * 1000}
    pil_img.save(str_LogdirPath+"\\"+filename_pic, quality=99, optimize=True)
    IdBr8_pic_address=name_customer+"\\"+filename_pic 
    #r1 = try_log_screen(screen1, quality=99)
    #IdBr8_pic_address=r1['screen']
#     print("缩略图大小:",li_IdBr8_PicView.attr("size"))
#     print("缩略图位置:",li_IdBr8_PicView.attr("pos"))
for key, value in EMOJI_UNICODE.items():
    if last_li_IdBr8_text.find(value)!=-1:
        last_li_IdBr8_text=last_li_IdBr8_text.replace(value,'['+key+']') 
if last_li_IdBr8_text.find('\u200b')!=-1:
    last_li_IdBr8_text=last_li_IdBr8_text.replace('\u200b','')
if last_li_IdBr8_text.find('\xa0')!=-1:
    last_li_IdBr8_text=last_li_IdBr8_text.replace('\xa0',' ')
# print("如果有文字:",IdBr8_text)
Tail_item=last_li_IdBr8_year+last_li_IdBr8_day+IdBr8_str_month+IdBr8_text
Tail_item_html="""<div style="width:100%;float:left">
<div style="">"""+last_li_IdBr8_year_html+"""
<strong style="font-size:24">"""+IdBr8_str_day+"""</strong>&nbsp;<strong>"""+last_li_IdBr8_month+"""</strong></div>
<img style="float:left" src="
"""+IdBr8_pic_address+""""/>
<div style="height:100px;float:left">"""+last_li_IdBr8_text+"""</div>
</div><br>"""
f = open(html_name,'a',encoding='utf-8')
message = Tail_item_html
f.write(message)
f.close()
# print("Tail_item:"+IdBr8_str_day+IdBr8_str_month+IdBr8_text);
#             for key,value in List_last_content:
#                 print("数组第"+str(i)+"次打印"+value)    
print(Tail_item);

  1. 跑起来,直到完全跑完,在底部有判断,如果到底则停止滑动,程序停止

  1. 得到如下结果,一个是html,一个是装有缩略图的文件夹

  1. 双击html,图片是链接到文件夹里的,如果相对位置不变在手机上可以打开,效果如下,快把结果发给你的女朋友吧!(由于都是广告不打码过不了审)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值