A.需求
1.利用浏览器取得网页的源码
2.解析源码,获取图片地址(这里使用了java的一个库来解析html)
3.获取资源,生成plist目录
B.实现步骤
1.打开一个网页,最好里面是包含了静态布局图片内容的
最好不要使用safari,不方便提取源码,这里我使用火狐
2.这里就要提取所有“力量英雄”的图片,并且附带英雄名
对着一张图片右击,选择“查看元素”
3.找到所需要提取的区域的html源码,复制
4.使用eclipse新建项目,创建一个html文件并黏贴源码,补全html格式
5.引入用来解析网页的jsoup jar包
6.使用java解析html源码,提取图片地址和相关信息
使用java代码生成一个OC数组,包含了上述信息,给OC项目使用
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 stub
13 // 指定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 ];
7.复制数组源码到OC项目,输出图片到文件
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.图片URL
41 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 }
8.生成plist文件
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];
out: