相关动态
baidu.rar_baidu_baidu api_搜索引擎api_百度搜索api_百度的API
2024-11-10 18:20
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; }
    以上就是本篇文章【baidu.rar_baidu_baidu api_搜索引擎api_百度搜索api_百度的API】的全部内容了,欢迎阅览 ! 文章地址:http://sjzytwl.xhstdz.com/news/5174.html 
     栏目首页      相关文章      动态      同类文章      热门文章      网站地图      返回首页 物流园资讯移动站 http://mip.xhstdz.com/ , 查看更多   
最新文章
电信卡怎么异地销户 销户流程是什么手机卡停机多久会被注销「电信卡怎么异地销户 销户流程是什么」
随着手机普及率越来越高,人们因为更工作、学习等各种原因更换手机号的频率也是越来越高,对于不用的手机号我们都需要进行销户,
梦金园去年净利同比降17.8%至1.89亿元 海外增700%
《科创板日报》3月29日讯(记者 徐赐豪)近期,黄金行情不断上涨,引发资本市场高度关注,黄金相关的企业也备受瞩目。昨晚公布其
怎么找回qq密码qq密码忘记了手机号也换了怎么办「怎么找回qq密码」
在日常的数字生活中,QQ作为我们常用的通讯工具之一,其账号安全显得尤为重要。然而,偶尔我们可能会遇到忘记密码的尴尬情况。别
大屏智能千元机 酷派5860价格欲破1100元酷派5860手机「大屏智能千元机 酷派5860价格欲破1100元」
(中关村在线手机频道行情报道)2012年3月6日,酷派5860(行货)在“ZOL商城”促销,最新报价为1181元,含发票。本款的配件为:
计步器怎么删掉手机自动计步器「计步器怎么删掉」
计步器作为现代智能设备的一部分,已经成为许多人日常健康管理的得力助手。然而,在某些情况下,用户可能需要删除计步器中的数据
三星今天上午宣布Note7爆炸原因 已导致损失超10亿美元手机爆炸「三星今天上午宣布Note7爆炸原因 已导致损失超10亿美元」
2016年8月, Galaxy Note 7智能手机在上市后,“引爆”了全球对于手机市场安全问题的关注,上百起电池燃烧事故让三星频繁陷入舆
CBA八强出炉!下半区比上半区惨烈,9-12最终排名确定!
在附加赛全部结束之后,八强的对阵已经全部出来了。其中上半区:广厦-青岛、辽宁-新疆。广厦和青岛的比赛,大家肯定都看好广厦。
财知道 | 爆卖上百万件!巴掌大的小东西,是年轻人的“到此一游”
或是四四方方、圆圆造型,或是形态各异,一块硬板刻着各式内容,再配上一块方寸大小的磁力贴片,曾被人吐槽“又土又贵”的冰箱贴
Z趋势:4G高速时代 机会与挑战并存4g手机价格「Z趋势:4G高速时代 机会与挑战并存」
2013年12月4日下午,工业和信息化部向中国联通、中国电信、中国移动正式发放了第四代移动通信业务牌照(即4G牌照),三家均获得T
华为手机工程模式怎么进入(华为手机工程模式怎么进入桌面)手机工程模式「华为手机工程模式怎么进入(华为手机工程模式怎么进入桌面)」
华为手机工程模式怎么进入相信很多小伙伴还不知道,现在让我们一起来看看吧!华为手机工程模式怎么进入(华为手机工程模式怎么进入