抓取网页数据、下载网络图片

来源:互联网 发布:贵阳大数据会议 编辑:程序博客网 时间:2024/06/09 23:42
通过jsoup抓取网页数数据<pre name="code" class="java">Document doc = Jsoup.connect("http://www.chinau.cc/cata/detail?cid=2060").timeout(5000000).get();Elements url = doc.select(".content .dleft .x_img").select("tbody td img");//就想像jquery那样去找到对应的元素(Element)或者多个元素(Elements)去做相应的操作即可//attr(..),text(),html()对应这jquery的方法基本都有 
<pre code_snippet_id="402777" snippet_file_name="blog_20140623_7_6691604" name="code" class="java">
下载网络图片
/** * 下载网络图片 * @param url 网络路径 * @param path 本地保存路径 * @param fileName 保存文件名 * @throws IOException * */public static void downUrl(String urlStr,String path,String fileName) throws IOException{URL url = new URL(urlStr); //构建URLURLConnection con = url.openConnection(); //打开连接con.setConnectTimeout(5*10*1000);//设置超时连接//读入网络图片流InputStream inStream = con.getInputStream();File file = new File(path);//保存路径if (!file.exists()) {file.mkdirs();}//图片输出流OutputStream outStream = new FileOutputStream(file+"\\"+fileName);byte[] b = new byte[1024];int len = 0 ;while ((len = inStream.read(b)) != -1) {outStream.write(b, 0, len);}//关闭数据流outStream.close();inStream.close();}


0 0
原创粉丝点击