




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Java培训专家-传智播客 上一节URLConnection入门 我们简单的介绍了URLConnection类的使用。今天我们将在此基础上完成一个小功能-爬虫原理:获得下载资源,使用程序将此资源保存到本地步骤:1、分析下载站点 下载内容列表页:http:/sc.*.com/tubiao/index.html2012-11-8 11:32 上传下载附件 (115.46 KB) 下载内容详细页:/tubiao/120602202460.htm2012-11-8 11:32 上传下载附件 (45.92 KB) 下载地址: http:/*.com/Files/download/icon3/4620.rar2、通过程序实现以上分析过程,并获得下载地址 使用循环获得所有的下载列表页的链接 通过链接将列表页下载,并通过正则表达式,获得当前列表页中所有的详细页链接 下载详细页,仍通过正则表达式,获得下载地址链接。3、将内容保存到本地 通过下载地址链接,将需要下载的内容保存到本地package cn.itcast.download;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.io.OutputStream;import java.io.UnsupportedEncodingException;import .MalformedURLException;import .URL;import .URLConnection;import java.util.ArrayList;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;/* 简单爬虫* author 梁桐*/public class ICOLoad public static void main(String args) throws Exception /遍历测试站点的所有列表页。测试站点列表页的第一页比较测试,所以单独处理 for (int i = 1; i = 266; i+) String url; if (i = 1) url = http:/sc.*.com/tubiao/index.html; else url = http:/sc.*.com/tubiao/index_ + i + .html; System.out.println(# + url); /获得当前列表页中,所有可用详细页链接 List paths = ICOLoad.getPaths(url, 1); for (String path : paths) System.out.println(获取 + path); /获得当前详细页的指定的下载链接 List downURLs = ICOLoad.getPaths(path, 2); for (String downURL : downURLs) System.out.println(下载中 . + downURL); / 下载 ICOLoad.downIcon(downURL); System.out.println(downURL + 下载完成); /* * 通过给定的解析方法,解析需要的路径 * return * throws Exception */ public static List getPaths(String url, int count) List paths = new ArrayList(); try URL icoUrl = new URL(url); URLConnection conn = icoUrl.openConnection(); /测试网站使用的gb2312编码 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), gb2312); String line = null; while (line = reader.readLine() != null) /此处直接使用if简化操作 / 处理列表页面 if (count = 1) String path = findPath(line); if (path != null) /列表页中的详细页地址都是相对路径,此处获得完整路径 String completeUrl = new URL(icoUrl, path).toString(); if (!paths.contains(completeUrl) / 过滤已有,测试网站图标和链接出各有一个所需链接 paths.add(completeUrl); / 处理下载地址 if (count = 2) String path = findDownURL(line); if (path != null) paths.add(path); reader.close(); return paths; catch (MalformedURLException e) e.printStackTrace(); catch (UnsupportedEncodingException e) e.printStackTrace(); catch (IOException e) e.printStackTrace(); return paths; /* * 处理列表页 * href=/tubiao/120602202460.htm */ private static Pattern pathPattern = Ppile(.*(/tubiao/d+.htm).*); public static String findPath(String str) / String str = 网站小图标下载; Matcher matcher = pathPattern.matcher(str); if (matcher.find() return matcher.group(1); return null; /* * 处理下载页,仅下载icon页面 * ICO中国网通下载 * ICO中国网通下载 */ private static Pattern downPattern = Ppile(.*(http:/*.com/.*icon.*.rar).*); public static String findDownURL(String str) Matcher matcher = downPattern.matcher(str); if (matcher.find() return matcher.group(1); return null; /* * 下载文件所在位置 */ private static File downFile = new File(D:ImageicoDown); static if (!downFile.exists() downFile.mkdirs(); /* * 下载指定内容 * param path */ public static void downIcon(String path) try URL url = new URL(path); String icoFileUrl = url.getFile(); String fileName = getFileName(icoFileUrl.substring(1 + icoFileUrl .lastIndexOf(/); File icoFile = new File(downFile + File.separator + fileName); if (!icoFile.exists() icoFile.createNewFile(); URLConnection conn = url.openConnection(); InputStream is = conn.getInputStream(); OutputStream out = new FileOutputStream(icoFile); byte buf = new byte1024; int len = -1; while (len = is.read(buf) -1) out.write(buf, 0, len); out.close(); is.close(); catch (MalformedURLException e) e.printStackTrace(); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace(); /* * 获得下载文件本地名称,如果重名自动加1 * param fileName * return */ public static String getFileName(String fileName) File file = new File(downFile + File.separator + fileName); if (file.exists() String fileNum = fileName.substring(0, fileName.i
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 客运驾驶员管理制度
- 家俱定制厂管理制度
- 家政保洁员管理制度
- 当家塘长效管理制度
- 影视后期部管理制度
- 德克士员工管理制度
- 心电图院感管理制度
- 快递存放点管理制度
- 怎样制作群管理制度
- 总承包施工管理制度
- 五年级读书分享课件
- 仓库管理基础知识培训
- 自闭症孤独症儿童语言与沟通评估表
- 钢网架结构安装、拼装施工方案
- DB32989-2007低压电气装置规程
- 部编版六年级下册道德与法治知识点大汇总
- T∕CSTE 0008-2020 污水处理用碳源液体乙酸钠
- Q∕GDW 46 10022.9-2020 主进水阀本体运检导则
- 姿态动力学作业(共9页)
- 电网公司竣工决算报告编制规范与竣工决算资料的要求
- 城市主干道工程监理大纲
评论
0/150
提交评论