文章标题
来源:互联网 发布:ipad换壁纸软件 编辑:程序博客网 时间:2024/06/14 20:55
爬虫
`
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Spider {
private String base = “http://meinv666.com“; //基地址
private String sourceUrl; //源网页链接
private StringBuilder html; //网页文本
private List imgUrl = new ArrayList<>();
private static int count = 0;public Spider(String sourceUrl) throws IOException { this.sourceUrl = sourceUrl; getHtml(); getImgURL();}/*得到网页源码*/public void getHtml() throws IOException { URL url = new URL(sourceUrl); Scanner scanner = new Scanner(url.openStream()); html = new StringBuilder(); while (scanner.hasNext()){ html.append(scanner.nextLine() + "\n"); }}/*找出图片链接*/public List<String> getImgURL() throws IOException { Pattern pattern = Pattern.compile("<img.*?src=\\\"([^\"]*?)\\\"\\s*alt=.*?>"); Matcher m = pattern.matcher(html); while (m.find()){ imgUrl.add(base + m.group(1)); } return imgUrl;}/*下一页*/public String getPage(){ Pattern pattern = Pattern.compile("<a\\s*href=\\\"([^\"]*?)\\\">»</a>"); Matcher m = pattern.matcher(html); if (m.find()) sourceUrl = base + m.group(1); return sourceUrl;}/*下载图片*/public void downLoad() throws IOException, InterruptedException { Iterator<String> iter = imgUrl.iterator(); while (iter.hasNext()){ String temp = iter.next(); String name = temp.substring(temp.lastIndexOf("/") + 1,temp.length()); URL url = new URL(temp); OutputStream ops = new FileOutputStream(new File("D:\\Downloads\\pic\\" + name)); InputStream ips = url.openStream(); int num = 0; while ((num = ips.read()) != -1){ ops.write(num); } ips.close(); ops.close(); System.out.println("完成" + ++count + "张!"); Thread.sleep(2000); }}
}
`
主方法
import java.io.IOException;
public class Demo {
public static void main(String[] args) throws IOException, InterruptedException {
String source = “http://meinv666.com/sex“;
int count = 8; while (count-- > 0 && source != null){ Spider spider = new Spider(source); spider.downLoad(); source = spider.getPage(); } System.out.println("完成惹。。");}
}
阅读全文
0 0
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- BZOJ 1087 互不侵犯的国王 状态压缩dp
- PHP接口
- truntact 和 delete 的区别
- 开启cdn后无法显示字体图标——CDN服务器跨域问题
- 逻辑结构定义及其分类
- 文章标题
- 【C语言 数据结构】汉诺塔(hanoi)的原理
- nodejs npm 安装 设置国内镜像
- Webrtc delay-base-bwe代码分析(2): InterArrival模块
- java基础代码练习
- 最长回文子串——Manacher 算法
- 怎么修改列的定义
- 如何解决SpringBoot与ODataJClient版本冲突的问题?
- 英雄联盟开发例子生动形象地为你讲解StrategyPattern------策略模式