1 import java.io.File; 2 3 import org.jsoup.Jsoup; 4 import org.jsoup.nodes.Document; 5 import org.jsoup.nodes.Element; 6 import org.jsoup.select.Elements; 7 8 9 public class FetchDataUtil {10 11 public static void main(String[] args) {12 // TODO Auto-generated method stub13 // 指定html文件地址14 String path = "/Users/hellovoidworld/Study/java/EclipseWrokspace/Test/FetchDataFromHtml/src/pic.html";15 16 try {17 // 1.读取html文件18 Document doc = Jsoup.parse(new File(path), "gb2312");19 20 // 2.解析出包含了图片和信息的li元素21 Elements lis = doc.select("li");22 23 // 创建OC数组格式的字符串24 System.out.PRintln("@[");25 26 // 3.遍历所有li元素,提取内容27 for (Element li : lis) {28 // 3.1 提取图片元素29 Element img = li.select("img").get(0);30 31 // 3.2 提取图片路径32 String imgUrl = img.attr("src");33 34 // 3.3 提取英雄名35 Element p = li.select("p").get(0);36 String heroName = p.text();37 38 // 4.拼接OC数组39 System.out.println("@{@/"heroName/":@/"" + heroName + "/",@/"imgUrl/":@/"" + imgUrl + "/"},");40 }41 42 System.out.println("];");43 }44 catch (Exception e)45 {46 e.printStackTrace();47 } finally {48 }49 50 }51 52 }
1 NSArray *imgs = @[ 2 @{@"heroName":@"斧王",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/FW.gif"}, 3 @{@"heroName":@"钢背兽",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/H008.jpg"}, 4 @{@"heroName":@"兽王",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/swsw.jpg"}, 5 @{@"heroName":@"发条",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/FTDJ.gif"}, 6 @{@"heroName":@"混沌骑士",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/hdqs.gif"}, 7 @{@"heroName":@"船长",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/CZCZ.jpg"}, 8 @{@"heroName":@"人马",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/brmm.jpg"}, 9 @{@"heroName":@"龙骑士",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/Hlgr.gif"},10 @{@"heroName":@"末日守卫",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/mrsz.gif"},11 @{@"heroName":@"撼地神牛",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/hdsn.jpg"},12 @{@"heroName":@"凤凰",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/hzfh.gif"},13 @{@"heroName":@"炼金",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/N01H.jpg"},14 @{@"heroName":@"神灵",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/H00Q.gif"},15 @{@"heroName":@"海牙",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/jyhm.jpg"},16 @{@"heroName":@"地狱领主",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/DXLZ.gif"},17 @{@"heroName":@"狼人",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/Bane.gif"},18 @{@"heroName":@"猛犸",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/UC11.gif"},19 @{@"heroName":@"食尸鬼",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/Naix.gif"},20 @{@"heroName":@"夜魔",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/AYMW.gif"},21 @{@"heroName":@"全能骑士",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/qnqs.gif"},22 @{@"heroName":@"熊猫酒仙",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/xmjx.gif"},23 @{@"heroName":@"深渊魔王",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/SYLZ.gif"},24 @{@"heroName":@"裂魂人",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/O00J.jpg"},25 @{@"heroName":@"鱼人守卫",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/XESW.gif"},26 @{@"heroName":@"沙王",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/U00K.gif"},27 @{@"heroName":@"骷髅王",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/klw.gif"},28 @{@"heroName":@"流浪剑客",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/lljk.jpg"},29 @{@"heroName":@"牛头人",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/ntrr.jpg"},30 @{@"heroName":@"屠夫",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/U00F.gif"},31 @{@"heroName":@"潮汐",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/Ofar.gif"},32 @{@"heroName":@"山岭",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/sljr.jpg"},33 @{@"heroName":@"树精",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/sjws.gif"},34 @{@"heroName":@"尸王",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/BXSW.gif"},35 @{@"heroName":@"精灵守卫",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/jlsw.jpg"}36 ];
1 - (void) fetchImage{ 2 NSArray *imgs = @[ 3 @{@"heroName":@"斧王",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/FW.gif"}, 4 @{@"heroName":@"钢背兽",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/H008.jpg"}, 5 @{@"heroName":@"兽王",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/swsw.jpg"}, 6 @{@"heroName":@"发条",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/FTDJ.gif"}, 7 @{@"heroName":@"混沌骑士",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/hdqs.gif"}, 8 @{@"heroName":@"船长",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/CZCZ.jpg"}, 9 @{@"heroName":@"人马",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/brmm.jpg"},10 @{@"heroName":@"龙骑士",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/Hlgr.gif"},11 @{@"heroName":@"末日守卫",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/mrsz.gif"},12 @{@"heroName":@"撼地神牛",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/hdsn.jpg"},13 @{@"heroName":@"凤凰",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/hzfh.gif"},14 @{@"heroName":@"炼金",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/N01H.jpg"},15 @{@"heroName":@"神灵",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/H00Q.gif"},16 @{@"heroName":@"海牙",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/jyhm.jpg"},17 @{@"heroName":@"地狱领主",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/DXLZ.gif"},18 @{@"heroName":@"狼人",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/Bane.gif"},19 @{@"heroName":@"猛犸",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/UC11.gif"},20 @{@"heroName":@"食尸鬼",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/Naix.gif"},21 @{@"heroName":@"夜魔",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/AYMW.gif"},22 @{@"heroName":@"全能骑士",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/qnqs.gif"},23 @{@"heroName":@"熊猫酒仙",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/xmjx.gif"},24 @{@"heroName":@"深渊魔王",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/SYLZ.gif"},25 @{@"heroName":@"裂魂人",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/O00J.jpg"},26 @{@"heroName":@"鱼人守卫",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/XESW.gif"},27 @{@"heroName":@"沙王",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/U00K.gif"},28 @{@"heroName":@"骷髅王",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/klw.gif"},29 @{@"heroName":@"流浪剑客",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/lljk.jpg"},30 @{@"heroName":@"牛头人",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/ntrr.jpg"},31 @{@"heroName":@"屠夫",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/U00F.gif"},32 @{@"heroName":@"潮汐",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/Ofar.gif"},33 @{@"heroName":@"山岭",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/sljr.jpg"},34 @{@"heroName":@"树精",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/sjws.gif"},35 @{@"heroName":@"尸王",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/BXSW.gif"},36 @{@"heroName":@"精灵守卫",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/jlsw.jpg"}37 ];38 39 for (NSDictionary *dict in imgs) {40 // 1.图片URL41 NSString *imgUrl = dict[@"imgUrl"];42 43 // 2.图片数据44 NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgUrl]];45 46 // 3.截取图片名47 NSString *imgName = [imgUrl lastPathComponent]; // 最后的路径名48 49 // 4.存放路径50 NSString *imgSavedPath = [NSString stringWithFormat:@"/Users/hellovoidworld/Study/iOS/0403/FetchImage/FetchImage/heroImg/%@", imgName];51 52 // 5.保存图片53 [imgData writeToFile:imgSavedPath atomically:YES];54 }55 56 }
1 // 保存信息到plist文件 2 NSMutableArray *heroData = [NSMutableArray array]; 3 for (NSDictionary *dict in imgs) { 4 // 1.英雄名 5 NSString *heroName = dict[@"heroName"]; 6 7 // 2.图片URL 8 NSString *imgUrl = dict[@"imgUrl"]; 9 10 // 3.截取图片名11 NSString *imgName = [imgUrl lastPathComponent]; // 最后的路径名12 13 NSDictionary *hero = @{@"heroName":heroName, @"img":imgName};14 [heroData addObject:hero];15 }16 17 [heroData writeToFile:@"/Users/hellovoidworld/Study/iOS/0403/FetchImage/FetchImage/hero.plist" atomically:YES];
新闻热点
疑难解答