爬取煎蛋网

1、找出页面网址的规律

2、设计页面图片网址的正则

代码:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class SpiderTest { private static ArrayList<String> urlStrs = new ArrayList<String>();
private static String regx = "\"[\\S]*\\.(jpg|gif)"; //读取jpg和gif图片的正则
private static int num = 0; //图片名递增量 public static void main(String[] args)throws Exception{
//String urlStr = "http://jandan.net/ooxx/page-2381#comments"; //要抓取的煎蛋妹子网页示例
String urlStr="";
String dstDir = "d:/dstDir";
int start = 2340; //起始页
int end = 2370; //结束页 for(int i=start;i<=end;i++){
urlStr = "http://jandan.net/ooxx/page-"+i+"#comments";
matchAll(urlStr);
if(urlStrs.size() > 0){
for(String imgStr:urlStrs){
downFile(imgStr,dstDir);
Thread.sleep(300); //休息一会
}
}
urlStrs.clear();
}
System.out.println("网址抓取完毕");
}
/*
* @param:urlStr 要爬取的网址
*/
private static void matchAll(String urlStr)throws Exception{
Pattern p = Pattern.compile(regx);
Matcher m;
URL url;
try {
url = new URL(urlStr);
} catch (MalformedURLException e) {
throw new Exception("网址不存在");
} BufferedReader read= new BufferedReader(new InputStreamReader(url.openStream()));
String line = "";
while((line = read.readLine()) != null){
m = p.matcher(line);
while(m.find()){
System.out.println(m.group());
urlStrs.add("http:"+m.group().substring(1)); //将图片网址添加到ArrayList(过滤第一个双引号)
}
}
read.close();
}
/*下载指定图片网址的图片
* @param:urlStr 图片网址
* @param:dstDir 图片存放目录
*/
private static void downFile(String urlStr,String dstDir)throws Exception{
byte[] bBuf = new byte[1024];
File dir = new File(dstDir);
String fileName = "";
if(!dir.exists()){
dir.mkdir();
}
if(urlStr.endsWith("jpg")){
fileName = (num++) + ".jpg";
}else if(urlStr.endsWith("gif")){
fileName = (num++) + ".gif";
}
File imgFile = new File(dstDir,fileName);
//if(imgFile.exists()){
// TODO..
//}
URL url = new URL(urlStr);
BufferedInputStream in = new BufferedInputStream(url.openStream());
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(imgFile)); System.out.println("开始下载。。");
int len = 0;
while((len = in.read(bBuf)) != -1){
out.write(bBuf,0,len);
}
System.out.println("下载完毕");
in.close();
out.close();
}
/*
* 获取网页源码(此方法没有使用)
*/
private void getSourceCode(String u)throws Exception{
//String u = "http://m.onepiece.cc/post/10001/";
File f = new File("d:/tmp.txt");
if(!f.exists()){
f.createNewFile();
}
URL url = new URL(u);
BufferedReader read = new BufferedReader(new InputStreamReader(url.openStream()));
BufferedWriter write = new BufferedWriter(new FileWriter(f));
String s = "";
while((s=read.readLine()) != null){
write.write(s);
write.write('\n');
}
System.out.println("拷贝完成");
read.close();
write.close();
}
}

java小爬虫的更多相关文章

  1. java正则表达式之java小爬虫

    这个java小爬虫, 功能很简单,只有一个,抓取网上的邮箱.用到了javaI/O,正则表达式. public static void main(String[] args) throws IOExce ...

  2. Java豆瓣电影爬虫——小爬虫成长记(附源码)

    以前也用过爬虫,比如使用nutch爬取指定种子,基于爬到的数据做搜索,还大致看过一些源码.当然,nutch对于爬虫考虑的是十分全面和细致的.每当看到屏幕上唰唰过去的爬取到的网页信息以及处理信息的时候, ...

  3. node.js 开发简易的小爬虫

    node.js  开发简易的小爬虫 最近公司开发一款医药类的软件,所以需要一些药品的基础数据,所以本人就用node.js写一个简易的小爬虫,并写记录这个Demo以供大家参考. 一.开发前的准备: 1, ...

  4. JAVA小项目实例源码—学习娱乐小助手

    代码地址如下:http://www.demodashi.com/demo/11456.html 一.程序实现 项目目录: MyJFrame:实现项目界面样式: AppProcess:实现调用api或爬 ...

  5. 学 Java 网络爬虫,需要哪些基础知识?

    说起网络爬虫,大家想起的估计都是 Python ,诚然爬虫已经是 Python 的代名词之一,相比 Java 来说就要逊色不少.有不少人都不知道 Java 可以做网络爬虫,其实 Java 也能做网络爬 ...

  6. Java 网络爬虫,就是这么的简单

    这是 Java 网络爬虫系列文章的第一篇,如果你还不知道 Java 网络爬虫系列文章,请参看 学 Java 网络爬虫,需要哪些基础知识.第一篇是关于 Java 网络爬虫入门内容,在该篇中我们以采集虎扑 ...

  7. Java 多线程爬虫及分布式爬虫架构探索

    这是 Java 爬虫系列博文的第五篇,在上一篇 Java 爬虫服务器被屏蔽,不要慌,咱们换一台服务器 中,我们简单的聊反爬虫策略和反反爬虫方法,主要针对的是 IP 被封及其对应办法.前面几篇文章我们把 ...

  8. 放养的小爬虫--豆瓣电影入门级爬虫(mongodb使用教程~)

    放养的小爬虫--豆瓣电影入门级爬虫(mongodb使用教程~) 笔者声明:只用于学习交流,不用于其他途径.源代码已上传github.githu地址:https://github.com/Erma-Wa ...

  9. 放养的小爬虫--京东定向爬虫(AJAX获取价格数据)

    放养的小爬虫--京东定向爬虫(AJAX获取价格数据) 笔者声明:只用于学习交流,不用于其他途径.源代码已上传github.githu地址:https://github.com/Erma-Wang/Sp ...

随机推荐

  1. centos install python3 pip3

    yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-dev ...

  2. 21天实战caffe笔记_第一天

    1 深度学习术语 深度学习常用名词:有监督学习.无监督学习.训练数据集.测试数据集.过拟合.泛化.惩罚值(损失loss); 机器自动学习所需三份数据:训练集(机器学习的样例),验证集(机器学习阶段,用 ...

  3. nginx启用stream日志配置文件

    主配置文件/etc/nginx/nginx.conf增加内容: stream { log_format proxy '$remote_addr [$time_local] ' '$protocol $ ...

  4. 题解 P1967 【货车运输】

    树链剖分+线段树 思路 貌似题解里没有树链剖分和线段树的,贡献一发. 首先明确题目要求:一辆车走某条路从x城到y城的边权最小值 我们把要求分开来看: 从x城到y城:我们需要走的路径将两点联通 边权最小 ...

  5. nova-compute源码分析

    源码版本:H版 首先看启动脚本如下: /usr/bin/nova-compute import sys from nova.cmd.compute import main if __name__ == ...

  6. 对硬盘进行分区时,GPT和MBR有什么区别?

    在Windows 8或8.1中设置新磁盘时,系统会询问你是想要使用MBR还是GPT分区.GPT是一种新的标准,并在逐渐取代MBR. GPT带来了很多新特性,但MBR仍然拥有最好的兼容性.GPT并不是W ...

  7. 深入剖析linq的联接

    内联接 代码如下 from a in new List<string[]>{ ]{"张三","男"}, ]{"李四"," ...

  8. bzoj千题计划136:bzoj3931: [CQOI2015]网络吞吐量

    http://www.lydsy.com/JudgeOnline/problem.php?id=3931 在最短路网络上跑最大流 #include<queue> #include<c ...

  9. python 压缩每周生成的数据文件

    为了便于整理部分业务数据,以及存储管理, 写了此脚本.后期如果有需求,再改一下. #!/usr/bin/env python #coding:utf8 import os,sys,time,comma ...

  10. nodejs express 上传文件 (格式 FormData)

    前台代码使用jQuery的ajax: <script type="text/javascript"> $(function(){ $('#file_upload').c ...