Python获取apk文件URL地址实例

yipeiwu_com5年前Python基础

工作中经常需要提取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 = ''

相关文章

Python中eval带来的潜在风险代码分析

0x00 前言 eval是Python用于执行python表达式的一个内置函数,使用eval,可以很方便的将字符串动态执行。比如下列代码: >>> eval("1...

Python写的贪吃蛇游戏例子

第一次用Python写这种比较实用且好玩的东西,权当练手吧 游戏说明: * P键控制“暂停/开始”* 方向键控制贪吃蛇的方向 源代码如下:复制代码 代码如下:from Tkinter i...

pytorch 彩色图像转灰度图像实例

pytorch 库 pytorch 本身具有载入cifar10等数据集的函数,但是载入的是3*200*200的张量,当碰到要使用灰度图像时,可以使用他本身的函数进行修改,以较快速的完成彩...

Python实现密码薄文件读写操作

制作一个"密码薄",其可以存储一个网址,和一个密码(如 123456),请编写程序完成这个“密码薄”的增删改查功能,并且实现文件存储功能 D:\pytest_day\mimab...

Python3中常用的处理时间和实现定时任务的方法的介绍

无论哪种编程语言,时间肯定都是非常重要的部分,今天来看一下python如何来处理时间和python定时任务,注意咯:本篇所讲是python3版本的实现,在python2版本中的实现略有不...