zoukankan      html  css  js  c++  java
  • Python获取apk文件URL地址

      工作中经常需要提取apk文件的特定URL地址,如是想到用Python脚本进行自动处理。

      需要用到的Python基础知识如下:

    1. os.walk()

      函数声明:os.walk(top,topdown=True,onerror=None)

      (1)参数top表示需要遍历的顶级目录的路径。

      (2)参数topdown的默认值是“True”表示首先返回顶级目录下的文件,然后再遍历子目录中的文件。当topdown的值为"False"时,表示先遍历子目录中的文件,然后再返回顶级目录下的文件。

      (3)参数onerror默认值为"None",表示忽略文件遍历时的错误。如果不为空,则提供一个自定义函数提示错误信息后继续遍历或抛出异常中止遍历。

      返回值:函数返回一个元组,含有三个元素。这三个元素分别是:每次遍历的路径名、路径下子目录列表、目录下文件列表

      os.walk使用实例:删除某个文件夹(当然可以通过os.listdir的递归调用删除)

       1 #! /usr/bin/env python
       2 #coding=utf-8
       3 import os
       4 
       5 def Remove_dir(top_dir):
       6     if os.path.exists(top_dir)==False:
       7         print "not exists"
       8         return
       9     if os.path.isdir(top_dir)==False:
      10         print "not a dir"
      11         return
      12     for dir_path,subpaths,files in os.walk(top_dir,False):
      13         for file in files:
      14             file_path=os.path.join(dir_path,file)
      15             print "delete file:%s"  %file_path
      16             os.remove(file_path)
      17         print "delete dir:%s" %dir_path
      18         os.rmdir(dir_path)
      19 
      20 #调用
      21 Remove_dir(r"C:\Users\Administrator\Desktop\abc")
    2. Python执行系统命令的方法 os.system(),os.popen(),commands.getstatusoutput() 

    • os.system()无法获得到输出和返回值;会打出cmd在linux上执行的信息。
    • 通过os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出,但是得不到返回值。不会打出cmd在linux上执行的信息。
    • 通过 commands.getstatusoutput() 方法就可以获得到返回值和输出。不会打出cmd在linux上执行的信息。  
    (status, output) = commands.getstatusoutput('cat /proc/cpuinfo') 

      只获得output和status的方法:

    commands.getoutput("ls")  
    commands.getstatus("ls")  

      3.  Python中operator模块的contains(...) 函数

      contains(a, b) -- Same as b in a (note reversed operands). 判断b是否被a包含 

      基础知识介绍完了,可以上代码了:

      •  1 import os
         2 import operator
         3 import commands
         4 #from signature import *
         5 
         6 inputdir = "./tmp"
         7 
         8 for path, dir, files in os.walk(inputdir):
         9     for file in files:
        10     if not file.endswith('.apk'):
        11         #print "not apk file."
        12         continue
        13     apkpath = os.path.join(inputdir, file)
        14     cmd = './xxx -d %s' %apkpath
        15     output = os.popen(cmd)
        16     s = set()
        17     #按行查找URL
        18     for line in output:
        19         if operator.contains(line, "http://"):
        20             #print tmp
        21             start = line.index('''http://''')
        22             end = line.index('''"''',start) 
        23             url = line[start:end]
        24             s.add(url)
        25     cmd = './yyy -t a.expense.mdk.a.tvd %s' %apkpath
        26     #获取命令执行结果及返回值
        27     status, output = commands.getstatusoutput(cmd)
        28 #    print output
        29     
        30     if output.startswith('find'):
        31         print output
        32         
        33         for url in s:
        34             if url.find('imei')!=-1:
        35                 print 'url is %s' %url.strip()
        36         #print '========================='
        37     s = ''
  • 相关阅读:
    vue中Axios的封装和API接口的管理
    解决Vue报错:TypeError: Cannot read property 'scrollHeight' of undefined
    JS-scrollTop、scrollHeight、clientTop、clientHeight、offsetTop、offsetHeight的理解
    理解Vue中的ref和$refs
    理解Vue中的nextTick
    CSS——overflow的参数以及使用
    JS数据结构——队列
    Vue中实现聊天窗口overflow:auto自动滚动到底部,实现显示当前最新聊天消息
    Vue中无法检测到数组的变动
    JS-观察者模式
  • 原文地址:https://www.cnblogs.com/goodhacker/p/3117295.html
Copyright © 2011-2022 走看看