Android主題開發者做的主題,如果想代替第三方應用圖標,就必須要知道應用的包名。其實想知道應用的包名很簡單,直接在瀏覽器打開Google Play或豌豆莢,打開某應用的頁面,看網址你就會發現,網址最后“/”字符后接的就是應用的包名!
估計有人想把常用應用的圖標和包名都搞下來,所以用java寫了個小程序,批量抓取了豌豆莢上“全部軟件”按總下載量排名里1到20頁的應用圖標與包名。
所有圖標都用包名來命名的,里面還有一個packageName.txt文件,包含了應用名稱對應的包名,方便查找。
java源碼
分享這個java小程序,注意,如果豌豆莢的網頁結構變了(估計很少改變吧),這個小程序就需要修改一下了,如果看得懂的話,修改很簡單的咯。
以下代碼可能已失效,僅作參考!
package im.garth.AppIconDownloader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map.Entry;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
* 獲取豌豆莢網頁上安卓軟件全部軟件
* 注意:執行程序前,一定要在這個工程目錄下創建icon文件夾
* 所有圖標將下載到icon這個文件夾中
* 應用名稱與包名寫到了icon下的packageName.txt文件里
*
* 這個程序用到的jar包:
* commons-logging-1.1.3.jar
* httpclient-4.1.2.jar
* httpcore-4.3.jar
* jsoup-1.6.1.jar
*
*
*/
public class AppIconDownloader {
/**
* @param args
*/
public static void main(String[] args) {
String rootUrl = "http://www.wandoujia.com/tag/全部軟件/total?page=";
//String rootUrl = "http://www.wandoujia.com/tag/全部游戲/total?page=";
//下載1到20頁的應用圖標
for(int i = 1; i < = 20; i++) {
System.out.println("【下載進度】:準備下載第" + i + "頁");
String currentUrl = rootUrl + i;
HashMap<String, String> apps = new HashMap<string , String>();
apps = getAppImageUrl(currentUrl);
//遍歷HashMap逐個下載圖標
for(Entry</string><string , String> entry : apps.entrySet()) {
try{
//下載圖標,存儲到當前工程目錄下的icon目錄(請事先創建icon目錄)
download(entry.getValue(), "icon/" + entry.getKey() + ".png");
}catch(Exception e) {
System.out.println("【下載出錯】:" + entry.getKey());
e.printStackTrace();
}
}
System.out.println("【下載進度】:第" + i + "頁下載完成");
}
}
/**
* 獲取url網頁里的所有應用的應用名及其圖標網址
* @param appPackageName
* @return
*/
private static HashMap</string><string , String> getAppImageUrl(String url) {
HashMap</string><string , String> apps = new HashMap</string><string , String>();
String appPackageName = "";
String appImageUrl = "";
String appName = "";
String html = getHtmlByUrl(url);
Document doc = Jsoup.parse(html);
Elements elements = doc.select("div.container.clearfix>section.main-col>div.app-blocks>div.app-block>ul.app-list.clearfix>li.app>a.icon-area");
Elements nameElements = doc.select("div.container.clearfix>section.main-col>div.app-blocks>div.app-block>ul.app-list.clearfix>li.app>div.operate>a.name>span.txt");
Elements imageEle;
int i = 0;
for(Element ele : elements) {
//獲取包名
appPackageName = ele.attr("data-pn");
//獲取圖標網址
imageEle = ele.select("img.icon");
appImageUrl = imageEle.get(0).attr("src").replace("68_68", "256_256");
//加入apps
apps.put(appPackageName, appImageUrl);
//獲取app名稱
appName = nameElements.get(i).text();
//把app名稱和包名輸出到文件
write2file("【" + appName + "】" + appPackageName);
i++;
}
System.out.println("【下載進度】:" + url + "下的圖標網址已經獲取成功");
return apps;
}
/**
* 下載文件到本地
*
* @param urlString
* 被下載的文件地址
* @param filename
* 本地文件名
* @throws Exception
* 各種異常
*/
private static void download(String urlString, String filename) throws Exception {
System.out.println("【下載進度】:正在下載" + filename);
// 構造URL
URL url = new URL(urlString);
// 打開連接
URLConnection con = url.openConnection();
// 輸入流
InputStream is = con.getInputStream();
// 1K的數據緩沖
byte[] bs = new byte[1024];
// 讀取到的數據長度
int len;
// 輸出的文件流
OutputStream os = new FileOutputStream(filename);
// 開始讀取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
// 完畢,關閉所有鏈接
os.close();
is.close();
System.out.println("【下載進度】:" + filename + "下載成功");
}
/**
* 根據URL獲得所有的html信息
* @param url
* @return html
*/
private static String getHtmlByUrl(String url){
String html = null;
//創建httpClient對象
HttpClient httpClient = new DefaultHttpClient();
//以get方式請求該URL
HttpGet httpget = new HttpGet(url);
try {
//得到responce對象
HttpResponse responce = httpClient.execute(httpget);
//返回碼
int resStatu = responce.getStatusLine().getStatusCode();
//200正常 其他就不對
if (resStatu==HttpStatus.SC_OK) {
//獲得相應實體
HttpEntity entity = responce.getEntity();
if (entity!=null) {
//獲得html源代碼
html = EntityUtils.toString(entity);
}
}
} catch (Exception e) {
System.out.println("訪問【"+url+"】出現異常!");
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
return html;
}
private static void write2file(String content) {
File file = new File("icon/packageName.txt");
BufferedWriter writer = null;
try{
if(!file.exists()) {
file.createNewFile();
}
//參數true表示將輸出追加到文件內容的末尾而不覆蓋原來的內容
writer = new BufferedWriter(new FileWriter(file, true));
//輸出內容
writer.write(content);
//換行
writer.newLine();
} catch(IOException e){
System.out.println("輸出出錯");
e.printStackTrace();
} finally {
if( writer != null) {
try {
writer.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
}
}