推广 热搜: 行业  设备    系统  参数  经纪    教师  机械  中国 

baidu.rar_baidu_baidu api_搜索引擎api_百度搜索api_百度的API

   日期:2024-11-10     作者:n19v1    caijiyuan   评论:0    移动:http://mip.xhstdz.com/mobile/news/5174.html
核心提示:import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundExceptio
import java.io.BufferedInputStream;

baidu.rar_baidu_baidu api_搜索引擎api_百度搜索api_百度的API

import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.params.HttpClientParams; import org.apache.commons.httpclient.params.HttpMethodParams; import org.jsoup.Jsoup; import org.jsoup.nodes.document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class BatchDownload { public static void main(String[] args) { //百度图片 String fromUrl = "http://image.baidu.com/i?tn=baiduimage&ct=201326592&cl=2&lm=-1&fr=&fmq=&pv=&ic=0&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&word=%B3%B5&s=0"; StringBuffer pageContents = new StringBuffer(); //System.out.println("asdasdas"); try{ URL startUrl = new URL(fromUrl); pageContents = downloadPage(startUrl); //System.out.println(pageContents); // if(pageContents.length()==0) System.out.println("asdasdas"); List<String> imgUrls = getImageUrls(pageContents); //System.out.println(imgUrls.size()); //downloadImages(pageContents); //if(pageContents.length()==0) System.out.println("asdasdas"); //System.out.println(pageContents); } catch(Exception e) { } } public static StringBuffer downloadPage(URL httpUrl)throws MalformedURLException,IOException { StringBuffer data = new StringBuffer(); String currentLine; // 打开输入流 BufferedReader reader = new BufferedReader(new InputStreamReader(getInputStream(httpUrl), "GBK")); // 读取数据 while ((currentLine = reader.readLine()) != null) { data.append(currentLine); } reader.close(); return data; } public static File downloadFile(String httpUrl, String fileSavePath)throws MalformedURLException, IOException { File file = new File(fileSavePath); if (!file.exists()) { file.createNewFile(); } URL url = new URL(httpUrl); // 打开输入流 BufferedInputStream in = new BufferedInputStream( getInputStream(url)); // 打开输出流 FileOutputStream out = new FileOutputStream(file); byte[] buff = new byte[1]; // 读取数据 while (in.read(buff) > 0) { out.write(buff); } out.flush(); out.close(); in.close(); return file; } private static void downloadImages(StringBuffer pageContents)throws MalformedURLException, IOException { // 获取html页面 StringBuffer page = pageContents; // 获取页面中的地址 List<String> imgUrls = getImageUrls(page); // 保存图片,返回文件列表 List<File> fileList = new ArrayList<File>(); String imgSaveDir="E:"; int i = 1; for (String url : imgUrls) { String fileName = url.substring(url.lastIndexOf("/") + 1); File file = downloadFile(url, imgSaveDir + "\" + fileName); System.out.println(file.getPath()+ " 下载完成!"); fileList.add(file); i++; } } private static InputStream getInputStream(URL httpUrl) throws IOException { // 网页Url URL url = httpUrl; URLConnection uc = url.openConnection(); uc.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); return uc.getInputStream(); } public static List<String> getImageUrls(StringBuffer html) { List<String> result = new ArrayList<String>(); // 将字符串解析为html文档 document doc = Jsoup.parse(html.toString()); // 获取img标签 Elements es =doc.getElementsByTag("img"); //Element es = doc.getElementById("script"); //Elements ss = new Elements(); //ss.add(es); //System.out.println(es.size()); //doc.getElementsByTag("img"); // 获取每一个img标签src的内容,也就是图片地址 for (Iterator<Element> i = es.iterator(); i.hasNext();) { Element e = i.next(); String r = e.attr("src"); Pattern p = Pattern.compile("http://.+\.(jpg|jpeg)"); Matcher m = p.matcher(r); if (m.matches()) { result.add(r); } } return result; }
本文地址:http://mip.xhstdz.com/news/5174.html    物流园资讯网 http://mip.xhstdz.com/ , 查看更多

特别提示:本信息由相关用户自行提供,真实性未证实,仅供参考。请谨慎采用,风险自负。

 
 
更多>同类最新文章
0相关评论

文章列表
相关文章
最新动态
推荐图文
最新文章
点击排行
网站首页  |  关于我们  |  联系方式  |  使用协议  |  版权隐私  |  网站地图  |  排名推广  |  广告服务  |  积分换礼  |  网站留言  |  RSS订阅  |  违规举报  |  鄂ICP备2020018471号