master
yihua.huang 2014-04-01 08:04:43 +08:00
parent 7038c00a9a
commit b13f1da039
1 changed files with 51 additions and 51 deletions

View File

@ -38,27 +38,27 @@ webmagic的github地址[https://github.com/code4craft/webmagic](https://githu
webmagic使用maven管理依赖在项目中添加对应的依赖即可使用webmagic webmagic使用maven管理依赖在项目中添加对应的依赖即可使用webmagic
```xml ```xml
<dependency> <dependency>
<groupId>us.codecraft</groupId> <groupId>us.codecraft</groupId>
<artifactId>webmagic-core</artifactId> <artifactId>webmagic-core</artifactId>
<version>0.4.3</version> <version>0.4.3</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>us.codecraft</groupId> <groupId>us.codecraft</groupId>
<artifactId>webmagic-extension</artifactId> <artifactId>webmagic-extension</artifactId>
<version>0.4.3</version> <version>0.4.3</version>
</dependency> </dependency>
``` ```
WebMagic 使用slf4j-log4j12作为slf4j的实现.如果你自己定制了slf4j的实现请在项目中去掉此依赖。 WebMagic 使用slf4j-log4j12作为slf4j的实现.如果你自己定制了slf4j的实现请在项目中去掉此依赖。
```xml ```xml
<exclusions> <exclusions>
<exclusion> <exclusion>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId> <artifactId>slf4j-log4j12</artifactId>
</exclusion> </exclusion>
</exclusions> </exclusions>
``` ```
#### 项目结构 #### 项目结构
@ -96,30 +96,30 @@ webmagic还包含两个可用的扩展包因为这两个包都依赖了比较
PageProcessor是webmagic-core的一部分定制一个PageProcessor即可实现自己的爬虫逻辑。以下是抓取osc博客的一段代码 PageProcessor是webmagic-core的一部分定制一个PageProcessor即可实现自己的爬虫逻辑。以下是抓取osc博客的一段代码
```java ```java
public class OschinaBlogPageProcesser implements PageProcessor { public class OschinaBlogPageProcesser implements PageProcessor {
private Site site = Site.me().setDomain("my.oschina.net"); private Site site = Site.me().setDomain("my.oschina.net");
@Override @Override
public void process(Page page) { public void process(Page page) {
List<String> links = page.getHtml().links().regex("http://my\\.oschina\\.net/flashsword/blog/\\d+").all(); List<String> links = page.getHtml().links().regex("http://my\\.oschina\\.net/flashsword/blog/\\d+").all();
page.addTargetRequests(links); page.addTargetRequests(links);
page.putField("title", page.getHtml().xpath("//div[@class='BlogEntity']/div[@class='BlogTitle']/h1").toString()); page.putField("title", page.getHtml().xpath("//div[@class='BlogEntity']/div[@class='BlogTitle']/h1").toString());
page.putField("content", page.getHtml().$("div.content").toString()); page.putField("content", page.getHtml().$("div.content").toString());
page.putField("tags",page.getHtml().xpath("//div[@class='BlogTags']/a/text()").all()); page.putField("tags",page.getHtml().xpath("//div[@class='BlogTags']/a/text()").all());
}
@Override
public Site getSite() {
return site;
}
public static void main(String[] args) {
Spider.create(new OschinaBlogPageProcesser()).addUrl("http://my.oschina.net/flashsword/blog")
.addPipeline(new ConsolePipeline()).run();
}
} }
@Override
public Site getSite() {
return site;
}
public static void main(String[] args) {
Spider.create(new OschinaBlogPageProcesser()).addUrl("http://my.oschina.net/flashsword/blog")
.addPipeline(new ConsolePipeline()).run();
}
}
``` ```
@ -134,24 +134,24 @@ Spider是爬虫的入口类。Pipeline是结果输出和持久化的接口
webmagic-extension包括了注解方式编写爬虫的方法只需基于一个POJO增加注解即可完成一个爬虫。以下仍然是抓取oschina博客的一段代码功能与OschinaBlogPageProcesser完全相同 webmagic-extension包括了注解方式编写爬虫的方法只需基于一个POJO增加注解即可完成一个爬虫。以下仍然是抓取oschina博客的一段代码功能与OschinaBlogPageProcesser完全相同
```java ```java
@TargetUrl("http://my.oschina.net/flashsword/blog/\\d+") @TargetUrl("http://my.oschina.net/flashsword/blog/\\d+")
public class OschinaBlog { public class OschinaBlog {
@ExtractBy("//title") @ExtractBy("//title")
private String title; private String title;
@ExtractBy(value = "div.BlogContent",type = ExtractBy.Type.Css) @ExtractBy(value = "div.BlogContent",type = ExtractBy.Type.Css)
private String content; private String content;
@ExtractBy(value = "//div[@class='BlogTags']/a/text()", multi = true) @ExtractBy(value = "//div[@class='BlogTags']/a/text()", multi = true)
private List<String> tags; private List<String> tags;
public static void main(String[] args) { public static void main(String[] args) {
OOSpider.create( OOSpider.create(
Site.me(), Site.me(),
new ConsolePageModelPipeline(), OschinaBlog.class).addUrl("http://my.oschina.net/flashsword/blog").run(); new ConsolePageModelPipeline(), OschinaBlog.class).addUrl("http://my.oschina.net/flashsword/blog").run();
} }
} }
``` ```
这个例子定义了一个Model类Model类的字段'title'、'content'、'tags'均为要抽取的属性。这个类在Pipeline里是可以复用的。 这个例子定义了一个Model类Model类的字段'title'、'content'、'tags'均为要抽取的属性。这个类在Pipeline里是可以复用的。