国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python獲取apk文件URL地址實例

2019-11-25 18:38:29
字體:
來源:轉載
供稿:網友

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁强县| 天祝| 金湖县| 马龙县| 铁岭县| 垫江县| 临夏市| 普陀区| 鹿泉市| 孟津县| 揭东县| 博爱县| 安达市| 泰安市| 安阳县| 阳曲县| 桃江县| 江华| 德令哈市| 临清市| 巴林右旗| 铁岭市| 阿拉善盟| 石阡县| 霍邱县| 奉化市| 晋中市| 衡山县| 息烽县| 磐安县| 张家川| 开阳县| 凌云县| 乐山市| 朝阳市| 炎陵县| 云阳县| 洛川县| 济源市| 定兴县| 海安县|