爬取煎蛋网

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. bzoj1874 [BeiJing2009 WinterCamp]取石子游戏

    1874: [BeiJing2009 WinterCamp]取石子游戏 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 925  Solved: 381[ ...

  2. 题解【bzoj1503 [NOI2004]郁闷的出纳员】

    Description 给出一个下限 \(m\) ,要求维护以下操作 插入一个数(如果小于下限就不加) 给每个数加上一个数 给每个数减去一个数,并且删除掉 \(< m\) 的所有数 求目前第 \ ...

  3. ElasticStack系列之十六 & ElasticSearch5.x index/create 和 update 源码分析

    开篇 在ElasticSearch 系列十四中提到的问题即 ElasticStack系列之十四 & ElasticSearch5.x bulk update 中重复 id 性能骤降,继续这个问 ...

  4. python 套接字之select poll epoll

    python下的select模块使用 以及epoll与select.poll的区别 先说epoll与select.poll的区别(总结) select, poll, epoll 都是I/O多路复用的具 ...

  5. JVM加载一个类的过程

    类的加载过程 Java源代码被编译成class字节码,JVM把描述类数据的字节码.Class文件加载到内存,并对数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的java类型,这就是虚拟机 ...

  6. 平铺式窗口管理器 Musca 初体验

    作者: 吴吉庆 Version: 1.0 release: 2009-11-04 update: 2009-11-04 为什么用平铺式窗口管理器? 什么是平铺式窗口管理器(tiling window ...

  7. pandas空值处理与插值

    # coding:utf-8 import pandas as pd import numpy as np import matplotlib.pyplot as plt from scipy.int ...

  8. Python练习-内置函数的应用

    说真的,我感觉这几天egon没有睡好,或者是egon心里有事儿,练习给留的太过简单了 # 编辑者:闫龙 # 用map来处理字符串列表,把列表中所有人都变成sb,比方alex_sb #name=['al ...

  9. Python练习-有点儿意思的用户登录

    Alex大神的需求(说实话他需求真特么多,真难满足他): 编写一个用户登陆接口:输入用户名密码,认证成功后显示欢迎信息,输错三次后锁定; # 编辑者:闫龙 #用户登录功能输入3次以上会被锁定:为了方便 ...

  10. C++ Qt多线程 TcpSocket服务器实例

    服务器: incomming incomming.pro #------------------------------------------------- # # Project created ...