Python獲取apk文件URL地址實(shí)例
工作中經(jīng)常需要提取apk文件的特定URL地址,如是想到用Python腳本進(jìn)行自動(dòng)處理。
需要用到的Python基礎(chǔ)知識(shí)如下:
os.walk()
函數(shù)聲明:os.walk(top,topdown=True,onerror=None)
(1)參數(shù)top表示需要遍歷的頂級(jí)目錄的路徑。
(2)參數(shù)topdown的默認(rèn)值是“True”表示首先返回頂級(jí)目錄下的文件,然后再遍歷子目錄中的文件。當(dāng)topdown的值為"False"時(shí),表示先遍歷子目錄中的文件,然后再返回頂級(jí)目錄下的文件。
(3)參數(shù)onerror默認(rèn)值為"None",表示忽略文件遍歷時(shí)的錯(cuò)誤。如果不為空,則提供一個(gè)自定義函數(shù)提示錯(cuò)誤信息后繼續(xù)遍歷或拋出異常中止遍歷。
返回值:函數(shù)返回一個(gè)元組,含有三個(gè)元素。這三個(gè)元素分別是:每次遍歷的路徑名、路徑下子目錄列表、目錄下文件列表。
os.walk使用實(shí)例:刪除某個(gè)文件夾(當(dāng)然可以通過os.listdir的遞歸調(diào)用刪除)
#! /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)
#調(diào)用
Remove_dir(r"C:\Users\Administrator\Desktop\abc")
Python執(zhí)行系統(tǒng)命令的方法 os.system(),os.popen(),commands.getstatusoutput()
os.system()無法獲得到輸出和返回值;
通過os.popen() 返回的是 file read 的對(duì)象,對(duì)其進(jìn)行讀取 read() 的操作可以看到執(zhí)行的輸出,但是得不到返回值。
通過 commands.getstatusoutput() 方法就可以獲得到返回值和輸出
(status, output) = commands.getstatusoutput('cat /proc/cpuinfo')
3. Python中operator模塊的contains(...) 函數(shù)
contains(a, b) -- Same as b in a (note reversed operands). 判斷b是否被a包含
基礎(chǔ)知識(shí)介紹完了,可以上代碼了:
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
#獲取命令執(zhí)行結(jié)果及返回值
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 = ''
相關(guān)文章
基于Python實(shí)現(xiàn)簡單的人臉識(shí)別系統(tǒng)
這篇文章主要介紹了如何通過Python實(shí)現(xiàn)一個(gè)簡單的人臉識(shí)別系統(tǒng),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定的幫助,感興趣的可以跟隨小編一起試一試2022-01-01python3+PyQt5+Qt Designer實(shí)現(xiàn)界面可視化
本文主要介紹了python3+PyQt5+Qt Designer實(shí)現(xiàn)界面可視化,Qt Designer,用鼠標(biāo)拖拖就能完成窗體設(shè)計(jì),感興趣的可以了解一下2021-06-06python 使用pandas同時(shí)對(duì)多列進(jìn)行賦值
這篇文章主要介紹了python 使用pandas同時(shí)對(duì)多列進(jìn)行賦值操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03Python使用PyCrypto實(shí)現(xiàn)AES加密功能示例
這篇文章主要介紹了Python使用PyCrypto實(shí)現(xiàn)AES加密功能,結(jié)合具體實(shí)例形式分析了PyCrypto實(shí)現(xiàn)AES加密的操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-05-05Python基于pandas爬取網(wǎng)頁表格數(shù)據(jù)
這篇文章主要介紹了Python基于pandas獲取網(wǎng)頁表格數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05