[iOS基础控件 - 6.9.4] 抓取网页图片资源

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

 import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements; public class FetchDataUtil { public static void main(String[] args) {
// TODO Auto-generated method stub
// 指定html文件地址
String path = "/Users/hellovoidworld/Study/java/EclipseWrokspace/Test/FetchDataFromHtml/src/pic.html"; try {
// 1.读取html文件
Document doc = Jsoup.parse(new File(path), "gb2312"); // 2.解析出包含了图片和信息的li元素
Elements lis = doc.select("li"); // 创建OC数组格式的字符串
System.out.println("@["); // 3.遍历所有li元素,提取内容
for (Element li : lis) {
// 3.1 提取图片元素
Element img = li.select("img").get(0); // 3.2 提取图片路径
String imgUrl = img.attr("src"); // 3.3 提取英雄名
Element p = li.select("p").get(0);
String heroName = p.text(); // 4.拼接OC数组
System.out.println("@{@\"heroName\":@\"" + heroName + "\",@\"imgUrl\":@\"" + imgUrl + "\"},");
} System.out.println("];");
}
catch (Exception e)
{
e.printStackTrace();
} finally {
} } }
输出:
手动修改一下不符合格式的部分
  NSArray *imgs = @[
@{@"heroName":@"斧王",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/FW.gif"},
@{@"heroName":@"钢背兽",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/H008.jpg"},
@{@"heroName":@"兽王",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/swsw.jpg"},
@{@"heroName":@"发条",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/FTDJ.gif"},
@{@"heroName":@"混沌骑士",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/hdqs.gif"},
@{@"heroName":@"船长",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/CZCZ.jpg"},
@{@"heroName":@"人马",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/brmm.jpg"},
@{@"heroName":@"龙骑士",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/Hlgr.gif"},
@{@"heroName":@"末日守卫",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/mrsz.gif"},
@{@"heroName":@"撼地神牛",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/hdsn.jpg"},
@{@"heroName":@"凤凰",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/hzfh.gif"},
@{@"heroName":@"炼金",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/N01H.jpg"},
@{@"heroName":@"神灵",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/H00Q.gif"},
@{@"heroName":@"海牙",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/jyhm.jpg"},
@{@"heroName":@"地狱领主",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/DXLZ.gif"},
@{@"heroName":@"狼人",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/Bane.gif"},
@{@"heroName":@"猛犸",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/UC11.gif"},
@{@"heroName":@"食尸鬼",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/Naix.gif"},
@{@"heroName":@"夜魔",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/AYMW.gif"},
@{@"heroName":@"全能骑士",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/qnqs.gif"},
@{@"heroName":@"熊猫酒仙",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/xmjx.gif"},
@{@"heroName":@"深渊魔王",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/SYLZ.gif"},
@{@"heroName":@"裂魂人",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/O00J.jpg"},
@{@"heroName":@"鱼人守卫",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/XESW.gif"},
@{@"heroName":@"沙王",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/U00K.gif"},
@{@"heroName":@"骷髅王",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/klw.gif"},
@{@"heroName":@"流浪剑客",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/lljk.jpg"},
@{@"heroName":@"牛头人",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/ntrr.jpg"},
@{@"heroName":@"屠夫",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/U00F.gif"},
@{@"heroName":@"潮汐",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/Ofar.gif"},
@{@"heroName":@"山岭",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/sljr.jpg"},
@{@"heroName":@"树精",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/sjws.gif"},
@{@"heroName":@"尸王",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/BXSW.gif"},
@{@"heroName":@"精灵守卫",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/jlsw.jpg"}
];
7.复制数组源码到OC项目,输出图片到文件
 - (void) fetchImage{
NSArray *imgs = @[
@{@"heroName":@"斧王",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/FW.gif"},
@{@"heroName":@"钢背兽",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/H008.jpg"},
@{@"heroName":@"兽王",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/swsw.jpg"},
@{@"heroName":@"发条",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/FTDJ.gif"},
@{@"heroName":@"混沌骑士",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/hdqs.gif"},
@{@"heroName":@"船长",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/CZCZ.jpg"},
@{@"heroName":@"人马",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/brmm.jpg"},
@{@"heroName":@"龙骑士",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/Hlgr.gif"},
@{@"heroName":@"末日守卫",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/mrsz.gif"},
@{@"heroName":@"撼地神牛",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/hdsn.jpg"},
@{@"heroName":@"凤凰",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/hzfh.gif"},
@{@"heroName":@"炼金",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/N01H.jpg"},
@{@"heroName":@"神灵",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/H00Q.gif"},
@{@"heroName":@"海牙",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/jyhm.jpg"},
@{@"heroName":@"地狱领主",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/DXLZ.gif"},
@{@"heroName":@"狼人",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/Bane.gif"},
@{@"heroName":@"猛犸",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/UC11.gif"},
@{@"heroName":@"食尸鬼",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/Naix.gif"},
@{@"heroName":@"夜魔",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/AYMW.gif"},
@{@"heroName":@"全能骑士",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/qnqs.gif"},
@{@"heroName":@"熊猫酒仙",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/xmjx.gif"},
@{@"heroName":@"深渊魔王",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/SYLZ.gif"},
@{@"heroName":@"裂魂人",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/O00J.jpg"},
@{@"heroName":@"鱼人守卫",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/XESW.gif"},
@{@"heroName":@"沙王",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/U00K.gif"},
@{@"heroName":@"骷髅王",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/klw.gif"},
@{@"heroName":@"流浪剑客",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/lljk.jpg"},
@{@"heroName":@"牛头人",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/ntrr.jpg"},
@{@"heroName":@"屠夫",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/U00F.gif"},
@{@"heroName":@"潮汐",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/Ofar.gif"},
@{@"heroName":@"山岭",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/sljr.jpg"},
@{@"heroName":@"树精",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/sjws.gif"},
@{@"heroName":@"尸王",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/BXSW.gif"},
@{@"heroName":@"精灵守卫",@"imgUrl":@"http://dotadb.uuu9.com/UploadFiles/Dota/Hero/jlsw.jpg"}
]; for (NSDictionary *dict in imgs) {
// 1.图片URL
NSString *imgUrl = dict[@"imgUrl"]; // 2.图片数据
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgUrl]]; // 3.截取图片名
NSString *imgName = [imgUrl lastPathComponent]; // 最后的路径名 // 4.存放路径
NSString *imgSavedPath = [NSString stringWithFormat:@"/Users/hellovoidworld/Study/iOS/0403/FetchImage/FetchImage/heroImg/%@", imgName]; // 5.保存图片
[imgData writeToFile:imgSavedPath atomically:YES];
} }
[iOS基础控件 - 6.9.4] 抓取网页图片资源
8.生成plist文件
  // 保存信息到plist文件
NSMutableArray *heroData = [NSMutableArray array];
for (NSDictionary *dict in imgs) {
// 1.英雄名
NSString *heroName = dict[@"heroName"]; // 2.图片URL
NSString *imgUrl = dict[@"imgUrl"]; // 3.截取图片名
NSString *imgName = [imgUrl lastPathComponent]; // 最后的路径名 NSDictionary *hero = @{@"heroName":heroName, @"img":imgName};
[heroData addObject:hero];
} [heroData writeToFile:@"/Users/hellovoidworld/Study/iOS/0403/FetchImage/FetchImage/hero.plist" atomically:YES];
out:
[iOS基础控件 - 6.9.4] 抓取网页图片资源