Python获取apk文件URL地址实例
admin
2023-07-31 02:02:21
0

工作中经常需要提取apk文件的特定URL地址,如是想到用Python脚本进行自动处理。
需要用到的Python基础知识如下:
os.walk()
函数声明:os.walk(top,topdown=True,onerror=None)
(1)参数top表示需要遍历的顶级目录的路径。
(2)参数topdown的默认值是“True”表示首先返回顶级目录下的文件,然后再遍历子目录中的文件。当topdown的值为\”False\”时,表示先遍历子目录中的文件,然后再返回顶级目录下的文件。
(3)参数onerror默认值为\”None\”,表示忽略文件遍历时的错误。如果不为空,则提供一个自定义函数提示错误信息后继续遍历或抛出异常中止遍历。
返回值:函数返回一个元组,含有三个元素。这三个元素分别是:每次遍历的路径名、路径下子目录列表、目录下文件列表。
os.walk使用实例:删除某个文件夹(当然可以通过os.listdir的递归调用删除)

复制代码 代码如下:
#! /usr/bin/env python
#coding=utf-8
import os

def Remove_dir(top_dir):
    if os.path.exists(top_dir)==False:
        print \”not exists\”
        return
    if os.path.isdir(top_dir)==False:
        print \”not a dir\”
        return
    for dir_path,subpaths,files in os.walk(top_dir,False):
        for file in files:
            file_path=os.path.join(dir_path,file)
            print \”delete file:%s\”  %file_path
            os.remove(file_path)
        print \”delete dir:%s\” %dir_path
        os.rmdir(dir_path)

#调用
Remove_dir(r\”C:\\Users\\Administrator\\Desktop\\abc\”)

Python执行系统命令的方法 os.system(),os.popen(),commands.getstatusoutput() 
os.system()无法获得到输出和返回值;
通过os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出,但是得不到返回值。
通过 commands.getstatusoutput() 方法就可以获得到返回值和输出  
(status, output) = commands.getstatusoutput(\’cat /proc/cpuinfo\’) 
3.  Python中operator模块的contains(…) 函数
contains(a, b) — Same as b in a (note reversed operands). 判断b是否被a包含 
基础知识介绍完了,可以上代码了:
复制代码 代码如下:
import os
import operator
import commands
#from signature import *

inputdir = \”./tmp\”

for path, dir, files in os.walk(inputdir):
    for file in files:
    if not file.endswith(\’.apk\’):
        #print \”not apk file.\”
        continue
    apkpath = os.path.join(inputdir, file)
    cmd = \’./xxx -d %s\’ %apkpath
    output = os.popen(cmd)
    s = set()
    #按行查找URL
    for line in output:
        if operator.contains(line, \”http://\”):
            #print tmp
            start = line.index(\’\’\’http://\’\’\’)
            end = line.index(\’\’\’\”\’\’\’,start)
            url = line[start:end]
            s.add(url)
    cmd = \’./yyy -t a.expense.mdk.a.tvd %s\’ %apkpath
    #获取命令执行结果及返回值
    status, output = commands.getstatusoutput(cmd)
#    print output

    if output.startswith(\’find\’):
        print output

        for url in s:
            if url.find(\’imei\’)!=-1:
                print \’url is %s\’ %url.strip()
        #print \’=========================\’
    s = \’\’

相关内容

热门资讯

500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
Prometheus+Graf... 一,Prometheus概述 1,什么是Prometheus?Prometheus是最初在Sound...
python绘图库Matplo... 本文简单介绍了Python绘图库Matplotlib的安装,简介如下: matplotlib是pyt...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...