内容简介:需要实现PageProcessor接口编写main函数
引入包
https://search.maven.org/artifact/us.codecraft/webmagic-core/0.7.3/jar
<dependencies>
<dependency>
<groupId>us.codecraft</groupId>
<artifactId>webmagic-extension</artifactId>
<version>0.7.3</version>
</dependency>
<dependency>
<groupId>us.codecraft</groupId>
<artifactId>webmagic-core</artifactId>
<version>0.7.3</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
编写最基本的爬虫
需要实现PageProcessor接口
package reptile;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import java.util.List;
/**
* @author ming
*
*/
public class RepoPageProcessor implements PageProcessor {
/*
爬虫相关配置
设置抓取间隔 重试次数
*/
private Site site = Site.me().setRetryTimes(10).setTimeOut(100000);
/**
* process the page, extract urls to fetch, extract the data and store
* 此处是爬虫的核心逻辑
* @param page page
*/
public void process(Page page) {
// 获取到页面html
page.putField("title", page.getHtml().$("div.content").toString());
// 添加url到队列中
List<String> urls = page.getHtml().$("h3").links().all();
// 从页面后续,继续读取url来获取
page.addTargetRequests(urls);
}
/**
* get the site settings
*
* @return site
* @see Site
*/
public Site getSite() {
return this.site;
}
}
编写main函数
import reptile.RepoPageProcessor;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.JsonFilePipeline;
public class main {
public static void main(String[] args){
Spider.create(new RepoPageProcessor())
// 确定开始爬的地址
.addUrl("https://www.iming.info/")
// 保存为json
.addPipeline(new JsonFilePipeline("./result"))
// 开启五个线程爬取
.thread(100)
// 启动爬虫
.run();
}
}
运行以后可以看到输出
爬虫监控
获取到单例以后,爬虫注入进入即可
import reptile.RepoPageProcessor;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.monitor.SpiderMonitor;
import us.codecraft.webmagic.pipeline.JsonFilePipeline;
import javax.management.JMException;
public class main {
public static void main(String[] args){
Spider spider = Spider.create(new RepoPageProcessor())
// 确定开始爬的地址
.addUrl("https://www.iming.info/")
// 保存为json
.addPipeline(new JsonFilePipeline("./result"));
try {
SpiderMonitor.instance().register(spider);
} catch (JMException e) {
e.printStackTrace();
}
spider.start();
}
}
接着启动监控
可以在运行的时候,查看jvm监控
注解爬虫
先编写model层
package Model;
public class iming {
private String title;
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
}
然后,所有的类,规定从那个页面中获取
package Model;
import us.codecraft.webmagic.model.annotation.HelpUrl;
import us.codecraft.webmagic.model.annotation.TargetUrl;
// 最终要抓取的url 最终想要的数据在这里,使用正则匹配需要的url
@TargetUrl("https://github.com/\\w+/\\w+")
// 需要从那个页面中获取到url
@HelpUrl("https://github.com/\\w+")
public class GithubRepo {
private String name;
private String author;
private String readme;
}
然后,再次使用ExtractBy注解,表示抽取的样式规则.
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- TiDB入门(四):从入门到“跑路”
- MyBatis从入门到精通(一):MyBatis入门
- MyBatis从入门到精通(一):MyBatis入门
- Docker入门(一)用hello world入门docker
- 赵童鞋带你入门PHP(六) ThinkPHP框架入门
- 初学者入门 Golang 的学习型项目,go入门项目
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
算法交易与套利交易
赵胜民 / 厦门大学出版社 / 2010-9 / 35.00元
《算法交易与套利交易》主要介绍算法交易和一些套利交易的策略,以便于读者对相关方面的内容进行阅读和学习。在《算法交易与套利交易》的第一部分,我们回顾了投资学一些相关的基本内容。其中,前两章介绍了证券投资的收益和风险等特征,以及马可维茨的最优资产配置模型。第3章则介绍了股票投资分析当中常用的资本资产定价模型(CAPM)、套利定价模型(APT),以及因素模型。然后,第4、5章分别讲到了金融证券估值模型、......一起来看看 《算法交易与套利交易》 这本书的介绍吧!