java批量采集豌豆莢網(wǎng)站Android應(yīng)用圖標和包名
Android主題開發(fā)者做的主題,如果想代替第三方應(yīng)用圖標,就必須要知道應(yīng)用的包名。其實想知道應(yīng)用的包名很簡單,直接在瀏覽器打開Google Play或豌豆莢,打開某應(yīng)用的頁面,看網(wǎng)址你就會發(fā)現(xiàn),網(wǎng)址最后“/”字符后接的就是應(yīng)用的包名!
估計有人想把常用應(yīng)用的圖標和包名都搞下來,所以用java寫了個小程序,批量抓取了豌豆莢上“全部軟件”按總下載量排名里1到20頁的應(yīng)用圖標與包名。
所有圖標都用包名來命名的,里面還有一個packageName.txt文件,包含了應(yīng)用名稱對應(yīng)的包名,方便查找。
java源碼
分享這個java小程序,注意,如果豌豆莢的網(wǎng)頁結(jié)構(gòu)變了(估計很少改變吧),這個小程序就需要修改一下了,如果看得懂的話,修改很簡單的咯。
以下代碼可能已失效,僅作參考!
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;
/**
* 獲取豌豆莢網(wǎng)頁上安卓軟件全部軟件
* 注意:執(zhí)行程序前,一定要在這個工程目錄下創(chuàng)建icon文件夾
* 所有圖標將下載到icon這個文件夾中
* 應(yīng)用名稱與包名寫到了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頁的應(yīng)用圖標
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目錄(請事先創(chuàng)建icon目錄)
download(entry.getValue(), "icon/" + entry.getKey() + ".png");
}catch(Exception e) {
System.out.println("【下載出錯】:" + entry.getKey());
e.printStackTrace();
}
}
System.out.println("【下載進度】:第" + i + "頁下載完成");
}
}
/**
* 獲取url網(wǎng)頁里的所有應(yīng)用的應(yīng)用名及其圖標網(wǎng)址
* @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");
//獲取圖標網(wǎng)址
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 + "下的圖標網(wǎng)址已經(jīng)獲取成功");
return apps;
}
/**
* 下載文件到本地
*
* @param urlString
* 被下載的文件地址
* @param filename
* 本地文件名
* @throws Exception
* 各種異常
*/
private static void download(String urlString, String filename) throws Exception {
System.out.println("【下載進度】:正在下載" + filename);
// 構(gòu)造URL
URL url = new URL(urlString);
// 打開連接
URLConnection con = url.openConnection();
// 輸入流
InputStream is = con.getInputStream();
// 1K的數(shù)據(jù)緩沖
byte[] bs = new byte[1024];
// 讀取到的數(shù)據(jù)長度
int len;
// 輸出的文件流
OutputStream os = new FileOutputStream(filename);
// 開始讀取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
// 完畢,關(guān)閉所有鏈接
os.close();
is.close();
System.out.println("【下載進度】:" + filename + "下載成功");
}
/**
* 根據(jù)URL獲得所有的html信息
* @param url
* @return html
*/
private static String getHtmlByUrl(String url){
String html = null;
//創(chuàng)建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) {
//獲得相應(yīng)實體
HttpEntity entity = responce.getEntity();
if (entity!=null) {
//獲得html源代碼
html = EntityUtils.toString(entity);
}
}
} catch (Exception e) {
System.out.println("訪問【"+url+"】出現(xiàn)異常!");
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();
}
//參數(shù)true表示將輸出追加到文件內(nèi)容的末尾而不覆蓋原來的內(nèi)容
writer = new BufferedWriter(new FileWriter(file, true));
//輸出內(nèi)容
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();
}
}
}
}
}
相關(guān)文章
解決mybatis resultMap根據(jù)type找不到對應(yīng)的包問題
這篇文章主要介紹了解決mybatis resultMap根據(jù)type找不到對應(yīng)的包問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08SpringBoot調(diào)用公共模塊的自定義注解失效的解決
這篇文章主要介紹了SpringBoot調(diào)用公共模塊的自定義注解失效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02SpringBoot中@Insert、@Update實現(xiàn)批量新增更新的使用示例
本文主要介紹了SpringBoot中@Insert、@Update實現(xiàn)批量新增更新的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-10-10spring boot與redis 實現(xiàn)session共享教程
這篇文章主要介紹了spring boot與redis 實現(xiàn)session共享教程,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-04-04