相关动态
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://sjzytwl.xhstdz.com/mobile/ , 查看更多   
最新文章
苹果手机怎么恢复备份?详细攻略为你整理好了!云备份怎么恢复到手机「苹果手机怎么恢复备份?详细攻略为你整理好了!」
随着智能手机和互联网的普及,手机中存储的个人信息、照片、视频、聊天记录等数据会变得越来越多。一旦手机丢失、损坏或系统出现
李亚鹏正式息影 投资50亿丽江当老板金立手机老板「李亚鹏正式息影 投资50亿丽江当老板」
  华西都市报4月28日讯 昨日,李亚鹏打造的“云南省文化产业试验园”项目北京签约“我每年只选一部戏来拍,这已经严格遵循了10
怎样采用比特币挖矿?手机挖矿「怎样采用比特币挖矿?」
好吧,废话不多说了,其实比特币 - Bitcoin 出现时间已经很久了,不过对于新手来说,还是需要讲解一下的。关于比特币。FORECE 在
定了!苹果 2022 秋季发布会定档,9月8日见iPhone 14 来了?苹果手机发布会「定了!苹果 2022 秋季发布会定档,9月8日见iPhone 14 来了?」
又是九月,丰收的季节,今年的苹果又该熟了.......正如此前传闻那样,今晚苹果正式向外界发送了邀请函,宣布将于当地时间 9月7日
苹果手机微信闪退怎么回事苹果手机闪退是什么原因「苹果手机微信闪退怎么回事」
  品牌型号:iPhone12  系统版本:IOS14  苹果手机如果出现微信闪退的状况,首先我们得寻找原因,接下来小编就带着你们来
ZArchiver手机解压缩工具chm文件手机怎么打开「ZArchiver手机解压缩工具」
ZArchiver是一款功能强大的解压缩工具APP,可用于解压文件或压缩文件,软件支持多种文件格式的压缩和解压缩,包括ZIP、RAR、7Z、
【原】一部手机最长能用几年?主要有三个因素,决定了手机的寿命什么手机寿命最长「【原】一部手机最长能用几年?主要有三个因素,决定了手机的寿命」
一部手机最长能用几年?主要取决于以下这几个重要因素,才能决定手机的使用寿命。在当今社会,智能手机已经成为我们生活中不可或
iQOO 3评测:旗舰机还能在哪提升?这部手机给了答案机械手机「iQOO 3评测:旗舰机还能在哪提升?这部手机给了答案」
  文/晓光 于泽 瑞豪 视频/贾乾 图/苏航  从去年成立至今,iQOO这品牌发布了不到十款产品,并成功在互联网渠道占据了自己的
2013-4-7发布华为模拟器eNSP月度更新版(V2.0)支持全系列AR路由器模拟华为手机模拟器「2013-4-7发布华为模拟器eNSP月度更新版(V2.0)支持全系列AR路由器模拟」
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本文链接:https://blog.csdn.ne
ADB驱动源码深度解析,从原理探究到实践应用android手机驱动「ADB驱动源码深度解析,从原理探究到实践应用」
摘要:,,本文深入解析ADB(Android Debug Bridge)驱动源码,从原理到实践全面阐述。文章首先介绍了ADB的基本原理和主要功能,
相关文章