fix dep of slf4j-log4j12
parent
a34e92d11a
commit
a1c7e826f7
47
README.md
47
README.md
|
@ -50,33 +50,35 @@ WebMagic use slf4j with slf4j-log4j12 implementation. If you customized your slf
|
||||||
|
|
||||||
### First crawler:
|
### First crawler:
|
||||||
|
|
||||||
Write a class implements PageProcessor:
|
Write a class implements PageProcessor. For example, I wrote a crawler of github repository infomation.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public class OschinaBlogPageProcesser implements PageProcessor {
|
public class GithubRepoPageProcessor implements PageProcessor {
|
||||||
|
|
||||||
private Site site = Site.me().setDomain("my.oschina.net");
|
private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);
|
||||||
|
|
||||||
@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();
|
page.addTargetRequests(page.getHtml().links().regex("(https://github\\.com/\\w+/\\w+)").all());
|
||||||
page.addTargetRequests(links);
|
page.putField("author", page.getUrl().regex("https://github\\.com/(\\w+)/.*").toString());
|
||||||
page.putField("title", page.getHtml().xpath("//div[@class='BlogEntity']/div[@class='BlogTitle']/h1").toString());
|
page.putField("name", page.getHtml().xpath("//h1[@class='entry-title public']/strong/a/text()").toString());
|
||||||
page.putField("content", page.getHtml().$("div.content").toString());
|
if (page.getResultItems().get("name")==null){
|
||||||
page.putField("tags",page.getHtml().xpath("//div[@class='BlogTags']/a/text()").all());
|
//skip this page
|
||||||
|
page.setSkip(true);
|
||||||
|
}
|
||||||
|
page.putField("readme", page.getHtml().xpath("//div[@id='readme']/tidyText()"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Site getSite() {
|
public Site getSite() {
|
||||||
return site;
|
return site;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Spider.create(new OschinaBlogPageProcesser()).addUrl("http://my.oschina.net/flashsword/blog")
|
Spider.create(new GithubRepoPageProcessor()).addUrl("https://github.com/code4craft").thread(5).run();
|
||||||
.addPipeline(new ConsolePipeline()).run();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
* `page.addTargetRequests(links)`
|
* `page.addTargetRequests(links)`
|
||||||
|
@ -86,22 +88,23 @@ public class OschinaBlogPageProcesser implements PageProcessor {
|
||||||
You can also use annotation way:
|
You can also use annotation way:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@TargetUrl("http://my.oschina.net/flashsword/blog/\\d+")
|
@TargetUrl("https://github.com/\\w+/\\w+")
|
||||||
public class OschinaBlog {
|
@HelpUrl("https://github.com/\\w+")
|
||||||
|
public class GithubRepo {
|
||||||
|
|
||||||
@ExtractBy("//title")
|
@ExtractBy(value = "//h1[@class='entry-title public']/strong/a/text()", notNull = true)
|
||||||
private String title;
|
private String name;
|
||||||
|
|
||||||
@ExtractBy(value = "div.BlogContent",type = ExtractBy.Type.Css)
|
@ExtractByUrl("https://github\\.com/(\\w+)/.*")
|
||||||
private String content;
|
private String author;
|
||||||
|
|
||||||
@ExtractBy(value = "//div[@class='BlogTags']/a/text()", multi = true)
|
@ExtractBy("//div[@id='readme']/tidyText()")
|
||||||
private List<String> tags;
|
private String readme;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
OOSpider.create(
|
OOSpider.create(Site.me().setSleepTime(1000)
|
||||||
Site.me(),
|
, new ConsolePageModelPipeline(), GithubRepo.class)
|
||||||
new ConsolePageModelPipeline(), OschinaBlog.class).addUrl("http://my.oschina.net/flashsword/blog").run();
|
.addUrl("https://github.com/code4craft").thread(5).run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
|
@ -11,7 +11,7 @@ import us.codecraft.webmagic.processor.PageProcessor;
|
||||||
*/
|
*/
|
||||||
public class GithubRepoPageProcessor implements PageProcessor {
|
public class GithubRepoPageProcessor implements PageProcessor {
|
||||||
|
|
||||||
private Site site = Site.me().setRetryTimes(3).setSleepTime(100);
|
private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void process(Page page) {
|
public void process(Page page) {
|
||||||
|
|
|
@ -23,12 +23,6 @@
|
||||||
<groupId>us.codecraft</groupId>
|
<groupId>us.codecraft</groupId>
|
||||||
<artifactId>webmagic-core</artifactId>
|
<artifactId>webmagic-core</artifactId>
|
||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
<exclusions>
|
|
||||||
<exclusion>
|
|
||||||
<groupId>org.slf4j</groupId>
|
|
||||||
<artifactId>slf4j-log4j12</artifactId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
|
|
Loading…
Reference in New Issue