google Logo下载

本文详细介绍了如何使用Jsoup库从Google网站抓取并下载特定的logo图像,包括设置超时、代理、循环获取下一页logo链接等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


// String html = CrawlHtmlTools.getInstance().getPageContent(
// "http://blog.sina.com.cn/s/blog_63ca10e60102dspm.html?tj=1");
//
String host = "http://www.google.com.hk";
String logos = "http://www.google.com.hk/logos";
Document doc = null;
try {
doc = Jsoup
.connect("http://www.google.com.hk/logos/logos00-1.html")
.userAgent(RequestHeader.FireFox.valueOf()).timeout(
Integer.valueOf(PropertiesConfig
.getValue("timeout"))).get();

} catch (IOException e) {
e.printStackTrace();
}

Map<String, String> imgMap = new HashMap<String, String>();
// String html = doc.toString();
// Document htmlDoc = Jsoup.parse(html,
// "http://www.google.com.hk");
boolean ok = true;
while (ok) {

Elements els = doc.select("dd:has(p)");
for (Element e : els) {
imgMap.put(e.select("p").text()+".gif", host
+ e.select("p").select("img").attr("src"));
// System.out.println(e.select("p"));
// System.out.println(host
// + e.select("p").select("img").attr("src"));
}

try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e2) {
e2.printStackTrace();
}

Pattern p = Pattern.compile("<a\\s+href=\"(.+?)\">下一頁 &raquo;</a>");
Matcher match = p.matcher(doc.toString());
if (match.find()) {
String temp = logos + "/" + match.group(1);
try {
doc = Jsoup.connect(temp).userAgent(
RequestHeader.FireFox.valueOf()).timeout(
Integer.valueOf(PropertiesConfig
.getValue("timeout"))).get();
} catch (NumberFormatException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
} else {
ok = false;
break;
}

}

Set<Map.Entry<String,String>> set = imgMap.entrySet();
for(Iterator<Map.Entry<String,String>> it = set.iterator(); it.hasNext();){
Map.Entry<String, String> entry = it.next();
DownloadFile df = new DownloadFile();
df.setDownloadUrl(entry.getValue());
df.setPath("f:/img/"+entry.getKey());
DefaultDownload dd = new DefaultDownload();
dd.execute(df);
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值