Python批量下载apk
生活随笔
收集整理的這篇文章主要介紹了
Python批量下载apk
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Python 3.6
主要庫:
openpyxl:對excel文件進行讀、寫;
requests:下載文件
從excel中取出下載鏈接,通過requests.head(url)得到頭信息,過濾非apk文件或大于100.00M的文件(可按需修改),再利用requests下載文件。
import openpyxl import re import requests import os.path from sys import argv from openpyxl import Workbook from datetime import datetimeurl = '' count = 0def download_apk(num, url):"""下載文件:param url:下載鏈接:param num:索引值"""global countsucceed = 'Succeed'failure = 'Failure'print('第' + str(num) + '條url:\n' + url)filename = os.path.basename(url)filename = filename.split('?')[0]response = requests.head(url)filesize = round(float(response.headers['Content-Length']) / 1048576, 2)apk_format = 'application/vnd.android.package-archive'# 過濾非zip文件或大于100.00M的文件# TODO 可按需修改if response.headers['Content-Type'] == apk_format and filesize < 100.00:print('文件類型:' + response.headers['Content-Type'] + "\n" +'文件大小:' + str(filesize) + 'M' + "\n" +'文件名:' + str(filename))# 下載文件headers = {'User-Agent': 'Mozilla/5.0 (Linux; U; Android 4.0.3; zh-cn; M032 Build/IML74K) ''AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30','Connection': 'keep-alive', }file = requests.get(url, headers=headers, timeout=10)with open(filename, 'wb') as apk:apk.write(file.content)print(succeed + "\n")count += 1# 返回內容dicts = [url, succeed, filename]return dictselse:print('文件類型:' + response.headers['Content-Type'] + "\n" +'文件大小:' + str(filesize) + 'M' + "\n" +failure + "\n")dicts = [url, failure, failure]return dictsdef deal_excel(path):"""從Excel中獲取下載鏈接:param path: 表格的絕對路徑"""global urlstart = datetime.now()wb = openpyxl.load_workbook(path)table = wb.get_sheet_by_name('Sheet1')max_row = table.max_rowprint('共' + str(max_row) + '條url.\n')wb2 = Workbook()ws = wb2.activefor row in range(1, max_row + 1):url = table.cell(row=row, column=1).value# 檢查http協議頭if not re.match(r'^https?:/{2}\w.+$', url):url = "http://" + urldicts = download_apk(row, url)# 新建Excel表,記錄原始地址、下載結果和文件名ws['A' + str(row)] = dicts[0]ws['B' + str(row)] = dicts[1]ws['C' + str(row)] = dicts[2]wb2.save('record.xlsx')print('共' + str(max_row) + '條下載鏈接。\n成功:' + str(count) + '\n失敗:' + str(max_row - count))end = datetime.now() - startprint('用時:' + str(end))if __name__ == '__main__':deal_excel(argv[1])運行截圖:
總結
以上是生活随笔為你收集整理的Python批量下载apk的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 正则表达式 匹配点号_Javascrip
- 下一篇: 写一款,汽车养护宝,通过微信小程序来管理