java解析xml文件练习——通过应用包名获取应用图标即其他信息(基于魅族应用商店)
Posted 云中志
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java解析xml文件练习——通过应用包名获取应用图标即其他信息(基于魅族应用商店)相关的知识,希望对你有一定的参考价值。
1、解析包名数据文件(txt文件),并生成包名数组:
package jsouphtml; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class GetPackageName { public static String[] getPackageName(String filename){ String packagename[]=null; BufferedReader iStream=null; String line = null; File packagenames = new File(filename);//"D:\\packagenames.txt" try { StringBuffer content=new StringBuffer(); iStream = new BufferedReader(new FileReader(packagenames)); while ((line=iStream.readLine())!=null) { content.append(line.trim()); } packagename=content.toString().split(","); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(iStream!=null) try { iStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return packagename; } }
2、抓取数据,生成图标文件(基于魅族应用商店):
package jsouphtml; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.net.URLConnection; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class MzTheme { public static void main(String[] args) { InputStream inputStream = null; OutputStream outputStream = null; try { String url = "http://app.flyme.cn/apps/public/detail?package_name="; String packagenames[] = GetPackageName.getPackageName("D:\\packagenames.txt"); System.out.println(packagenames.length); for (int i =1;i<packagenames.length;i++) { System.out.println(i); Document doc = Jsoup.connect(url +packagenames[i]).get(); if(doc.getElementsByClass("mz_content mz_container clearfix").isEmpty()){ Element cElement = doc.getElementById("theme_content"); Elements imgs = cElement.getElementsByClass("app_img"); String src = imgs.first().attr("src"); System.out.println(src); System.out.println(packagenames[i]); File icon = new File("D:\\icons\\" + packagenames[i] + ".png");//图标存放目录,可根据情况修改 // 1.获取网址 URL u = new URL(src); // 2.打开连接 URLConnection conn = u.openConnection(); // 3.获取输入流与写出流 inputStream = conn.getInputStream(); outputStream = new FileOutputStream(icon); // 4.将源代码写入内存(设置编码) byte[] b = new byte[2048]; int len = 0; while ((len = inputStream.read(b)) != -1) { outputStream.write(b, 0, len); } }else{ System.out.println(packagenames[i]+",No_Exit"); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { // 关闭I/o try { if (outputStream != null) outputStream.close(); if (inputStream != null) inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
生成图标文件(基于应用宝):
package jsouphtml; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.net.URLConnection; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class JsoupHtml { public static void main(String[] args) { InputStream inputStream = null; OutputStream outputStream = null; try { //String url = "http://app.flyme.cn/apps/public/detail?package_name="; //String url = "http://app.mi.com/details?id="; //String url ="http://www.wandoujia.com/apps/"; String url = "http://sj.qq.com/myapp/detail.htm?apkName="; String packagenames[] = GetPackageName.getPackageName("D:\\packagenames.txt"); System.out.println(packagenames.length); for (int i =1;i<packagenames.length;i++) { System.out.println(i); Document doc = Jsoup.connect(url +packagenames[i]).get(); Element content = doc.getElementById("det-icon"); //Elements imgs = doc.getElementsByClass("app_img"); //魅族应用商店 //Elements imgs = doc.getElementsByClass("yellow-flower"); if(content.isBlock()){ Elements imgs = doc.getElementsByTag("img"); String src = imgs.first().attr("src"); System.out.println(src); System.out.println(packagenames[i]); File icon = new File("D:\\yingyongbao\\" + packagenames[i] + ".png"); // 1.获取网址 URL u = new URL(src); // 2.打开连接 URLConnection conn = u.openConnection(); // 3.获取输入流与写出流 inputStream = conn.getInputStream(); outputStream = new FileOutputStream(icon); // 4.将源代码写入内存(设置编码) byte[] b = new byte[2048]; int len = 0; while ((len = inputStream.read(b)) != -1) { outputStream.write(b, 0, len); } }else{ System.out.println(packagenames[i]+",No_Exit"); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { // 关闭I/o try { if (outputStream != null) outputStream.close(); if (inputStream != null) inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
包名数据(meizu.txt):
air.fyzb3,air.tv.douyu.android,android.zhibo8,bubei.tingshu,cc.fotoplace.app,chinatelecom.mwallet,cld.navi.mainframe,cmb.pb,cmccwm.mobilemusic,cn.amazon.mShop.android,cn.andouya,cn.banshenggua.aichang,cn.buding.martin,cn.cj.pe,cn.cntv,cn.colorv,cn.com.fetion,cn.com.kuting.activity,cn.com.nd.s,cn.com.opda.android.clearmaster,cn.eclicks.drivingtest,cn.eclicks.wzsearch,cn.ibuka.manga.ui,cn.j.guang,cn.j.hers,cn.jingling.motu.photowonder,cn.jj,cn.kuwo.player,cn.kuwo.tingshu,cn.ledongli.ldl,cn.mama.activity,cn.mucang.drunkremind.android,cn.mucang.kaka.android,cn.mucang.xiaomi.android,cn.mucang.xiaomi.android.wz,cn.opda.a.phonoalbumshoushou,cn.relian99,cn.wps.moffice_eng,com.A17zuoye.mobile.homework,com.achievo.vipshop,com.adobe.flashplayer,com.adobe.reader,com.akazam.android.wlandialer,com.alensw.PicFolder,com.alex.lookwifipassword,com.ali.money.shield,com.alibaba.android.rimet,com.alibaba.wireless,com.android.bankabc,com.android.cheyooh,com.android.chrome,com.android.comicsisland.activity,com.android.dazhihui,com.Android56,com.androidesk,com.androidesk.livewallpaper,com.anguanjia.safe,com.anguanjia.security,com.anjuke.android.app,com.antutu.ABenchMark,com.anzogame.game,com.anzogame.lol,com.aoratec.wifimanager,com.autoconnectwifi.app,com.autohome.mycar,com.autonavi.cmccmap,com.autonavi.minimap,com.autonavi.xmgd.navigator,com.babytree.apps.pregnancy,com.baidu.BaiduMap,com.baidu.baidutranslate,com.baidu.browser.apps,com.baidu.easyroot,com.baidu.hao123,com.baidu.homework,com.baidu.iknow,com.baidu.input,com.baidu.lbs.waimai,com.baidu.netdisk,com.baidu.news,com.baidu.searchbox,com.baidu.tieba,com.baidu.video,com.baozoumanhua.android,com.baozun.customer.main,com.beastudio.sflashlight,com.besttone.elocal,com.besttone.hall,com.bilin.huijiao.activity,com.blackbean.cnmeach,com.brianbaek.popstar,com.browser2345,com.budejie.www,com.calendar.UI,com.centurysoft.fruityrobo,com.changba,com.chaozh.iReaderFree,com.chinamobile.cmccwifi,com.chinamobile.contacts.im,com.chinamworld.bocmbci,com.chinamworld.main,com.chinatelecom.bestpayclient,com.chinatelecom.pim,com.cinema2345,com.cleanmaster.mguard_cn,com.cleanmaster.security_cn,com.cmbchina.ccd.pluto.cmbActivity,com.cmcc.cmvideo,com.codoon.gps,com.coohuaclient,com.cootek.smartdialer,com.corp21cn.flowpay,com.ct.client,com.cubic.autohome,com.cubic.choosecar,com.culiu.purchase,com.culiu.tenpics,com.culiukeji.huanletao,com.dangdang.buy2,com.daohang2345,com.dawx.seafloor,com.DBGame.DiabloLOL,com.dewmobile.kuaiya,com.dianping.v1,com.dianxinos.dxhome,com.dianxinos.superuser,com.didapinche.booking,com.dou_pai.DouPai,com.douguo.recipe,com.dp.android.elong,com.dsi.ant.plugins.antplus,com.duoduo.child.story,com.duokan.reader,com.duomi.android,com.duowan.bi,com.duowan.groundhog.mctools,com.duowan.kiwi,com.duowan.lolbox,com.duowan.mobile,com.dw.btime,com.eastmoney.android.berlin,com.eg.android.AlipayGphone,com.erdo.android.FJDXCartoon,com.esbook.reader,com.estrongs.android.pop,com.evernote,com.fanli.android.apps,com.fenbi.android.gaozhong,com.fenbi.android.solar,com.flightmanager.view,com.fone.player,com.frego.flashlight,com.funshion.video.mobile,com.gameloft.android.ANMP.GloftA8CN,com.gameloft.android.ANMP.GloftDMCN,com.ganji.android,com.gau.go.launcherex,com.geili.koudai,com.gift.android,com.google.android.apps.docs,com.google.android.apps.maps,com.google.android.inputmethod.pinyin,com.google.android.street,com.gotokeep.keep,com.gozap.labi.android,com.gtgj.view,com.gwsoft.imusic.controller,com.haishiydmm.fish,com.handsgo.jiakao.android,com.haobao.wardrobe,com.haodou.recipe,com.happyteam.dubbingshow,com.hexin.plat.android,com.hf,com.hipu.yidian,com.hisunflytone.android,com.hjwordgames,com.hola.launcher,com.huaqian,com.huati,com.huawei.fans,com.huawei.hidisk,com.huluxia.mctool,com.hunantv.imgo.activity,com.husor.beibei,com.husor.mizhe,com.ibox.flashlight,com.icbc,com.icbc.im,com.icoolme.android.weather,com.ifeng.news2,com.ifeng.newvideo,com.iflytek.cmcc,com.iflytek.inputmethod,com.iflytek.ringdiyclient,com.ifreetalk.ftalk,com.ijinshan.browser_fast,com.ijinshan.duba,com.ijinshan.kbatterydoctor,com.imangi.templerun2,com.immomo.momo,com.imusic.iting,com.instamag.activity,com.intsig.BizCardReader,com.intsig.camscanner,com.iooly.android.lockscreen,com.itings.myradio,com.iyd.reader.ReadingJoy,com.jiasoft.swreader,com.jingdian.tianxiameishi.android,com.jingdong.app.mall,com.jiongji.andriod.card,com.jiubang.goscreenlock,com.jiuyan.infashion,com.jm.android.jumei,com.jsmcc,com.juanpi.ui,com.jxedt,com.kandian.vodapp,com.kiloo.subwaysurf,com.kingreader.framework,com.kingroot.kinguser,com.kingroot.master,com.kingsoft,com.koudai.weidian.buyer,com.koudai.weishop,com.kuaikan.comic,com.kugou.android,com.kugou.android.ringtone,com.kunpeng.babyting,com.kxshow,com.lbe.security,com.le123.ysdq,com.lectek.android.sfreader,com.leixun.taofen8,com.lejent.zuoyeshenqi.afanti,com.lemon.faceu,com.lenovo.anyshare,com.lenovo.calendar,com.lenovo.safecenter,com.lesports.glivesports,com.letv.android.client,com.lingan.seeyou,com.lingdong.client.android,com.liveaa.education,com.longzhu.tga,com.lovebizhi.wallpaper,com.ludashi.benchmark,com.m4399.gamecenter,com.manboker.headportrait,com.mapbar.android.mapbarmap,com.mci.smagazine,com.meelive.ingkee,com.meilishuo,com.meitu.makeup,com.meitu.meipaimv,com.meitu.meiyancamera,com.meitu.wheecam,com.melot.meshow,com.miantan.myoface,com.microsoft.office.onenote,com.MobileTicket,com.mogujie,com.moji.mjweather,com.moxiu.launcher,com.mt.mtxx.mtxx,com.mydream.wifi,com.mymoney,com.mymoney.sms,com.nd.android.pandahome2,com.nd.android.pandareader,com.netease.cloudmusic,com.netease.mail,com.netease.newsreader.activity,com.netease.pris,com.nice.main,com.nuomi,com.ophone.reader.ui,com.oupeng.mini.android,com.p1.mobile.putong,com.panda.videoliveplatform,com.pingan.lifeinsurance,com.pingan.pabank.activity,com.pingan.papd,com.pingan.pinganwifi,com.pingco.android.mix,com.pingco.jc258cup,com.pipcamera.activity,com.pplive.androidphone,com.qianshoulian.app,com.qidian.QDReader,com.qigame.lock,com.qihoo.cleandroid_cn,com.qiyi.video,com.qq.ac.android,com.qq.qcloud,com.qq.reader,com.quanleimu.activity,com.Qunar,com.quvideo.xiaoying,com.qzone,com.renren.mobile.android,com.rinzz.noonedies,com.rongcai.show,com.rrh.jdb,com.sankuai.meituan,com.sankuai.meituan.takeoutnew,com.sankuai.movie,com.sdu.didi.gsui,com.sdu.didi.psnger,com.sec.chaton,com.sec.pcw,com.sg.sledog,com.shoujiduoduo.ringtone,com.shoujiduoduo.wallpaper,com.shuame.mobile,com.shuqi.controller,com.sina.news,com.sina.weibo,com.sinovatech.unicom.ui,com.slanissue.apps.mobile.erge,com.smile.gifmaker,com.snda.wifilocating,com.soft.blued,com.sogou.activity.src,com.sogou.map.android.maps,com.sogou.novel,com.sohu.inputmethod.sogou,com.sohu.newsclient,com.sohu.sohuvideo,com.soufun.app,com.soulgame.bubble,com.ss.android.article.news,com.ss.android.essay.joke,com.sskj.flashlight,com.storm.smart,com.sumavision.talktv2,com.suning.mobile.ebuy,com.supercell.clashofclans.wdj,com.syezon.wifi,com.tadu.android,com.taobao.ju.android,com.taobao.qianniu,com.taobao.taobao,com.taobao.trip,com.telecom.video,com.telecom.video.ikan4g,com.tencent.androidqqmail,com.tencent.clover,com.tencent.gallerymanager,com.tencent.hexkog,com.tencent.karaoke,com.tencent.KiHan,com.tencent.lightalk,com.tencent.map,com.tencent.mm,com.tencent.mobileqq,com.tencent.mobileqqi,com.tencent.mtt,com.tencent.news,com.tencent.now,com.tencent.pao,com.tencent.pb,com.tencent.peng,com.tencent.portfolio,com.tencent.qlauncher,com.tencent.qlauncher.lite,com.tencent.qqgame.xq,com.tencent.qqlite,com.tencent.qqlive,com.tencent.qqmusic,com.tencent.qqpim,com.tencent.qqpimsecure,com.tencent.qqpinyin,com.tencent.qt.qtl,com.tencent.radio,com.tencent.reading,com.tencent.tmgp.carrot3,com.tencent.tmgp.cf,com.tencent.tmgp.gods,com.tencent.tmgp.RunGame,com.tencent.tmgp.sgame,com.tencent.tmgp.ttcz,com.tencent.token,com.tencent.ttpic,com.tencent.unipay,com.tencent.WBlog,com.tencent.WeFire,com.tencent.wifimanager,com.tencent.zebra,com.tenpay.android,com.thestore.main,com.tianqi2345,com.tieyou.train.ark,com.tigerknows,com.ting.mp3.android,com.tiqiaa.icontrol,com.tmall.wireless,com.tongcheng.android,com.tripadvisor.tripadvisor.daodao,com.tuan800.tao800,com.tudou.android,com.tujia.hotel,com.tuniu.app.ui,com.ucamera.ucam,com.unionpay.uppay,com.ushaqi.zhuishushenqi,com.uu.uunavi,com.vlocker.locker,com.vmall.client,com.wacai365,com.wangzhi.MaMaHelp,com.wantu.activity,com.weihua.superphone,com.welove520.welove,com.wenba.bangbang,com.whatsapp,com.when.coco,com.wififreekey.wifi,com.wochacha,com.wuba,com.xg.nine,com.xiachufang,com.xiaobanlong.main,com.xiaoenai.app,com.xiaomi.hm.health,com.xiaomi.shop,com.ximalaya.ting.android,com.xingjiabi.shengsheng,com.xinmei365.font,com.xiudang.jiukuaiyou.ui,com.xtuone.android.syllabus,com.xunlei.downloadprovider,com.xunlei.kankan,com.xunmeng.pinduoduo,com.ygkj.chelaile.standard,com.yhouse.code,com.yibasan.lizhifm,com.yiche.autoeasy,com.yiche.price,com.yipiao,com.yixia.videoeditor,com.yixia.xiaokaxiu,com.yoloho.dayima,com.youan.universal,com.youba.barcode,com.youdao.dict,com.youdao.note,com.youku.phone,com.youloft.calendar,com.yourdream.app.android,com.youyuan.yyhl,com.yuedong.sport,com.yulong.android.coolmall,com.yulong.android.coolyou,com.yx,com.yy.yymeet,com.zdworks.android.zdclock,com.zhangdan.app,com.zhihu.android,com.zhiqupk.root,com.zte.heartyservice,com.ztgame.bob,com.zuobao.xiaobao,ctrip.android.view,dopool.player,flipboard.cn,fm.qingting.qtradio,fm.xiami.main,howbuy.android.piggy,InternetRadio.all,me.chunyu.ChunyuDoctor,me.ele,my.beautyCamera,oms.mmc.app.almanac_inland,org.cocos2d.fishingjoy3,org.fungo.fungolive,qsbk.app,sina.mobile.tianqitong,smskb.com,sogou.mobile.explorer,tv.danmaku.bili,tv.pps.mobile,viva.reader,vStudio.Android.Camera360,wb.gc.xmxx.zxb
本文只是提供一个思路,你可以根据自己的需求进一步开发,比如获取下载量等……
以上是关于java解析xml文件练习——通过应用包名获取应用图标即其他信息(基于魅族应用商店)的主要内容,如果未能解决你的问题,请参考以下文章