---恢复内容开始---

package com.action;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import javax.management.JMException; import org.bson.Document; import com.model.Model_AnjukeList;
import com.mongodb.BasicDBObject;
import com.util.Constants;
import com.util.GetDate;
import com.util.MD5With32;
import com.util.MongoDBUtil; import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.monitor.SpiderMonitor;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.processor.PageProcessor; public class GetAnjukeListNum implements PageProcessor { public static Model_AnjukeList anjukeList;
public static List<String> list = new ArrayList<String>();
public static List<BasicDBObject> list_insert = new ArrayList<BasicDBObject>();
private Site site = Site.me().setSleepTime(1000).setRetryTimes(3).setCharset("UTF-8")
.setUserAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36");
@Override
public Site getSite() {
// TODO Auto-generated method stub
return this.site;
} @Override
public void process(Page page) {
String found = null;
BasicDBObject doc = null;
page.addTargetRequests(Constants.list_urls);
System.out.println("code:"+page.getStatusCode());
String pg = page.getHtml().toString();
if(pg.length()>100){
Pattern p = Pattern.compile(Constants.anjuke_Reg_Found);
Matcher m = p.matcher(pg);
while(m.find()){
found = m.group(0).replace("\"found\":", "").replace(",", "");
String id = MD5With32.encryption(page.getUrl().toString());
if(!Constants.map_urls.containsKey(id)){
continue;
}
Model_AnjukeList model_AnjukeList = Constants.map_urls.get(id);
//mongo存储!
doc = new BasicDBObject("_id",id)
.append("city", model_AnjukeList.getCity())
.append("towards", model_AnjukeList.getTowards())
.append("zone_urls", model_AnjukeList.getZone_urls())
.append("zone", model_AnjukeList.getZone())
.append("site", model_AnjukeList.getSite())
.append("decoration", model_AnjukeList.getDecoration())
.append("flag", model_AnjukeList.getFlag())
.append("street", model_AnjukeList.getStreet())
.append("type", model_AnjukeList.getType())
.append("page", model_AnjukeList.getPage())
.append("urls", model_AnjukeList.getUrls())
.append("found", found)
.append("update_time", model_AnjukeList.getUpdate_time())
;
list_insert.add(doc);
}
}
} public static void main(String[] args) {
String city = "北京";
String urls = "https://beijing.anjuke.com/sale/";
MongoGetUrls.GetMongoUrls(city);
System.out.println("任务总数:"+Constants.list_urls.size());
Spider.create(new GetAnjukeListNum())
.addUrl(urls)
.addPipeline(new ConsolePipeline())
.thread(30)
.run(); MongoDBUtil.saveMany(..., list_insert);
}
}

第一次用webmagic 很多东西不懂,也没有重写。

很多都是用纯java实现

让我们慢慢发现webmagic的强大吧!

webmagic学习之路-1:采集安居客列表页测试的更多相关文章

  1. webmagic学习之路-3:采集安居客经纪人详情页

    这里希望安居客的同行的轻喷!!单纯的做测试,玩玩. 就这么糟践你们的服务器了!!!sorry! 这次学会了webmagic 设置处理的访问HTML返回代码,因为之前一直404的页面process根本都 ...

  2. webmagic学习之路-2:采集安居客经纪人列表

    相比较 1 稍微成熟了一点,会用的东西多了. 正则用的不好,很多东西不会,大神轻喷! package com.action; import java.util.ArrayList; import ja ...

  3. Bootstrap学习之路(1)---开篇-登陆页

    Bootstrap是现在很流行的一套前端框架,尤其是它的自适应,真的很不错,而且对移动设备也很友好,可以达到快速开发的效果,最近想把自己的网站弄个手机版,很果断的就选用了bootstrap,跟大家分享 ...

  4. 嵌入式Linux驱动学习之路(十九)触摸屏驱动、tslib测试

    触摸屏使用流程: 1. 按下产生中断. 2.在中断处理程序中启动AD转换XY坐标. 3.AD转换结束并产生AD中断. 4. 在AD的中断处理函数中上报信息,启动定时器. 5. 定时器时间到后进入中断, ...

  5. Python学习之路基础篇--05Python基础+列表和元组

    1 list # 增 city = ["wuhan", "shanghai", "chongqing", "changsha&qu ...

  6. Redis学习之路(003)- hiredis安装及测试

    一. hiredis下载地址及C API  github下载:https://github.com/redis/hiredis 安装脚本: #!/bin/zsh git clone https://g ...

  7. Python爬虫实战(3):安居客房产经纪人信息采集

    1, 引言 Python开源网络爬虫项目启动之初,我们就把网络爬虫分成两类:即时爬虫和收割式网络爬虫.为了使用各种应用场景,该项目的整个网络爬虫产品线包含了四类产品,如下图所示: 本实战是上图中的“独 ...

  8. Redis学习之路(000)- 目录

    本文是博主学习整理网上大神的文件以及自学的心得. Redis学习之路(000)- 目录 Redis学习之路(001)- Redis介绍以及安装(Linux) Redis学习之路(002)- Ubunt ...

  9. Django学习案例一(blog):五. 开发主页(博客列表展示)

    主页是一个“博客列表”页.博客要按发布时间的倒序来排列,每个博客都要包含标题.作者.分类.发布时间的显示(年-月-日 时:分)及节选的正文内容(前 100 个字).点击单独的博客可以进入其详情页. 1 ...

随机推荐

  1. HearthBuddy版本收集

    Hearthbuddy-20190811-010-0b563c92.exe   20190810-003 SHA-256: b2a03c10124b038d2c48279cc50947907a55c8 ...

  2. 前端知识点回顾之重点篇——ES6的async函数和module

    async函数 ES2017 标准引入了 async 函数,使得异步操作变得更加方便. async 函数是 Generator 函数的语法糖 什么是语法糖? 意指那些没有给计算机语言添加新功能,而只是 ...

  3. Mac系统下,docker安装nextcloud,打造个人本地网盘

    1.安装docker 推荐下载地址:http://get.daocloud.io/#install-docker-for-mac-windows   2.拉取镜像 $ docker pull next ...

  4. 多目标优化算法(一)NSGA-Ⅱ(NSGA2)(转载)

    多目标优化算法(一)NSGA-Ⅱ(NSGA2) 本文链接:https://blog.csdn.net/qq_40434430/article/details/82876572多目标优化算法(一)NSG ...

  5. vue-cli 构建的项目 webpack 如何配置不 build 出 .map 文件?

    build命令后占体积最大的竟然是.map文件,webpack如何设置不让编译出.map文件呢?

  6. win10下检查nvidia显卡支持的cuda版本

    1.首先将[C:\Program Files\NVIDIA Corporation\NVSMI]添加至系统环境变量[path]中: 2.在powershell中使用命令[nvidia-smi],即可看 ...

  7. 视图解析器InternalResourceViewResolver

    ModelAndView对象中即可以封装真实视图路径名,也可以封装视图路径的逻辑名,springmvc.xml 代码如下: <!-- 视图解析器(框架) --> <bean clas ...

  8. NLP - Log-linear Models

    1.The Language Modeling Problem         现在抛开我们之前讲的马尔科夫模型的假设,对于一门语言的定义,肯定不能简单依赖于每个单词的前两个单词,这是常识.比如英语中 ...

  9. JDK8记FullGC时候Metaspace内存不会被垃圾回收

    本文链接:https://blog.csdn.net/a15939557197/article/details/90635460背景前段时间有一个这样的需求:第三方调用接口,30分钟内调用120W次: ...

  10. [Navicat]把1个库的数据迁移到另1个库--数据库备份

    需求: 将autotest库中所包含的所有表,从连接centOS7复制1份至localhost.autotest 操作步骤: 1.将centOS7.autotest库中表结构及表数据转为sql语句:c ...