Skip to content

Instantly share code, notes, and snippets.

@dawand
Last active December 9, 2023 01:46
Show Gist options
  • Save dawand/7b4308d568c6b955b645dd7e707e5cf1 to your computer and use it in GitHub Desktop.
Save dawand/7b4308d568c6b955b645dd7e707e5cf1 to your computer and use it in GitHub Desktop.
Download APK files from Google Play Store with Python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
File name: download_apk.py
Author: Dawand Sulaiman
Download APK files from Google Play Store with Python
This script scraps https://apkpure.com to get the apk download link
Make sure you have BeautifulSoup and urllib libraries
"""
from bs4 import BeautifulSoup
from urllib.parse import quote_plus
import requests
def search(query):
res = requests.get('https://apkpure.com/search?q={}&region='.format(quote_plus(query)), headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.5 (KHTML, like Gecko) '
'Version/9.1.2 Safari/601.7.5 '
}).text
soup = BeautifulSoup(res, "html.parser")
search_result = soup.find('div', {'id': 'search-res'}).find('dl', {'class': 'search-dl'})
app_tag = search_result.find('p', {'class': 'search-title'}).find('a')
download_link = 'https://apkpure.com' + app_tag['href']
return download_link
def download(link):
res = requests.get(link + '/download?from=details', headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.5 (KHTML, like Gecko) '
'Version/9.1.2 Safari/601.7.5 '
}).text
soup = BeautifulSoup(res, "html.parser").find('a', {'id': 'download_link'})
if soup['href']:
r = requests.get(soup['href'], stream=True, headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.5 (KHTML, like Gecko) '
'Version/9.1.2 Safari/601.7.5 '
})
with open(link.split('/')[-1] + '.apk', 'wb') as file:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
file.write(chunk)
def download_apk(app_id):
download_link = search(app_id)
if download_link is not None:
print('Downloading {}.apk ...'.format(download_link))
download(download_link)
print('Download completed!')
else:
print('No results')
# Test it
download_apk('org.moire.opensudoku')
@Jogogoloboy
Copy link

Not sure if you would consider this alternative "better" but I scripted a ui automator that downloads the APKs from the official Play store client app using an actual emulator/device.

You can use adb shell am start market://details?id=<YOUR_APP_HANDLE> to open the apps detail page in the store client, get the install button coordinates with an xml dump from the uiautomator API with adb exec-out uiautomator dump /dev/tty and press it with adb shell input tap <COORDINATES>.

My goal was to actually install the app on the device but if you just want to get the APK you can just pull it from the device. It is located in the store client's cache.

@Dareen978
Copy link

When I tried to run this code, I had this error. Does anyone know what the problem is?

Traceback (most recent call last):
File "/home/kali/Desktop/./download_apk.py", line 54, in
download_apk('org.moire.opensudoku')
File "/home/kali/Desktop/./download_apk.py", line 44, in download_apk
download_link = search(app_id)
^^^^^^^^^^^^^^
File "/home/kali/Desktop/./download_apk.py", line 22, in search
search_result = soup.find('div', {'id': 'search-res'}).find('dl', {'class': 'search-dl'})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'find'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment