java 下载百度搜索的图片

本文介绍了一个简单的Java程序,用于从百度图片搜索结果中抓取图片并保存至本地。该程序通过解析网页源代码,提取图片链接,并将图片及对应链接信息记录在文本文件中。

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

自己的一个小小需求,写了小段代码,来下载百度搜索的图片。


import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SaveImg {

private String webUrl="";
private String imgPath = "f:/img";// 保存文件路径
private String imgContent = "content.txt";// 保存下载的图片名地址对应 文件名:Url列表


public SaveImg(String imgurl) {
webUrl=imgurl;
}


//网页源码到string
public String getHtmlCode(String httpUrl) throws IOException {
if(httpUrl==null || httpUrl.equals(""))
httpUrl=this.webUrl;
StringBuffer content = new StringBuffer();
URL url = new URL(httpUrl);
URLConnection urlconn= url.openConnection();
Reader read = new InputStreamReader(new BufferedInputStream(urlconn
.getInputStream()));
int c;
while((c=read.read())!=-1)
content.append((char)c);
read.close();
return content.toString().replaceAll("\\s*", "");
}

//提取图片url 到list
private List extractImgUrl(String content){
List imgList = new ArrayList();
String imgReg="objURL\":\"http://[\\s\\S]*?(.jpg|.JPG|.png|.PNG|.gif|.GIF)\"";
Matcher imgurlMatcher =Pattern.compile(imgReg).matcher(content);
while (imgurlMatcher.find()) {
imgList.add(imgurlMatcher.group().split("\"")[2]);
}
return imgList;
}



//写图片名,url 到文件。
// imageName:URL
private void writeNameUrl(List imgUrlList) {
try {
File filePath = new File(imgPath);
if (!filePath.exists())
filePath.mkdir();
File f = new File(filePath, "content.txt");
if (!f.exists())
f.createNewFile();

BufferedReader input = new BufferedReader(new FileReader(f));
StringBuffer s1 = new StringBuffer();
String s = new String();
while ((s=input.readLine()) != null) {
s1.append(s+System.getProperty("line.separator"));
}
input.close();
UUID uuid = null;
for(int i=0;i<imgUrlList.size();i++){
uuid=UUID.randomUUID();
this.saveImg(uuid.toString(),imgUrlList.get(i).toString());//保存图片
s1.append(uuid+":"+imgUrlList.get(i)+System.getProperty("line.separator"));
}
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(s1.toString());
output.close();

} catch (IOException e) {
System.err.println(e);
}
}

//保存图片
private boolean saveImg(String imgName,String imgUrl){
URL url;
BufferedInputStream in;
FileOutputStream file;
try {
String fileName = imgName+imgUrl
.substring(imgUrl.lastIndexOf("."));
url = new URL(imgUrl);

in = new BufferedInputStream(url.openStream());

file = new FileOutputStream(new File(imgPath,fileName));
int end;
while ((end = in.read()) != -1) {
file.write(end);
}
file.close();
in.close();
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public static void main(String[] args) {
SaveImg sim = new SaveImg(
"http://image.baidu.com/i?tn=baiduimage&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1333734805096_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&word=%CE%EF%C0%ED%CA%B5%D1%E9&s=0#pn=15");
try {
sim.writeNameUrl(sim.extractImgUrl(sim.getHtmlCode(null)));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值