Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
webmagic
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
沈俊林
webmagic
Commits
3b00190f
Commit
3b00190f
authored
Oct 09, 2013
by
yihua.huang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
api without implementation for #28: add specific url crawl
parent
719100d6
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
120 additions
and
7 deletions
+120
-7
BaiduBaike.java
...c/main/java/us/codecraft/webmagic/example/BaiduBaike.java
+28
-0
OOSpider.java
...n/src/main/java/us/codecraft/webmagic/model/OOSpider.java
+24
-3
UrlTemplate.java
...a/us/codecraft/webmagic/model/annotation/UrlTemplate.java
+37
-0
Param.java
...c/main/java/us/codecraft/webmagic/model/direct/Param.java
+15
-0
Kr36NewsModel.java
...va/us/codecraft/webmagic/model/samples/Kr36NewsModel.java
+16
-4
No files found.
webmagic-extension/src/main/java/us/codecraft/webmagic/example/BaiduBaike.java
0 → 100644
View file @
3b00190f
package
us
.
codecraft
.
webmagic
.
example
;
import
us.codecraft.webmagic.model.OOSpider
;
import
us.codecraft.webmagic.model.annotation.ExtractBy
;
import
us.codecraft.webmagic.model.annotation.UrlTemplate
;
import
us.codecraft.webmagic.model.direct.Param
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* @author code4crafter@gmail.com
*/
@UrlTemplate
(
"http://baike.baidu.com/search/word?word=${word}&enc=utf8"
)
public
class
BaiduBaike
{
private
String
word
;
@ExtractBy
(
"//div[@id='lemmaContent-0']//div[@class='para']/allText()"
)
private
String
description
;
public
static
void
main
(
String
[]
args
)
{
List
<
Param
>
words
=
new
ArrayList
<
Param
>();
words
.
add
(
new
Param
().
put
(
"word"
,
"红烧肉"
));
OOSpider
.
direct
(
words
,
BaiduBaike
.
class
).
thread
(
10
).
run
();
}
}
webmagic-extension/src/main/java/us/codecraft/webmagic/model/OOSpider.java
View file @
3b00190f
...
@@ -2,8 +2,11 @@ package us.codecraft.webmagic.model;
...
@@ -2,8 +2,11 @@ package us.codecraft.webmagic.model;
import
us.codecraft.webmagic.Site
;
import
us.codecraft.webmagic.Site
;
import
us.codecraft.webmagic.Spider
;
import
us.codecraft.webmagic.Spider
;
import
us.codecraft.webmagic.model.direct.Param
;
import
us.codecraft.webmagic.processor.PageProcessor
;
import
us.codecraft.webmagic.processor.PageProcessor
;
import
java.util.Collection
;
/**
/**
* The spider for page model extractor.<br>
* The spider for page model extractor.<br>
* In webmagic, we call a POJO containing extract result as "page model". <br>
* In webmagic, we call a POJO containing extract result as "page model". <br>
...
@@ -22,13 +25,14 @@ import us.codecraft.webmagic.processor.PageProcessor;
...
@@ -22,13 +25,14 @@ import us.codecraft.webmagic.processor.PageProcessor;
* {@literal @}ExtractBy(value = "//div[@class='BlogTags']/a/text()", multi = true)
* {@literal @}ExtractBy(value = "//div[@class='BlogTags']/a/text()", multi = true)
* private List<String> tags;
* private List<String> tags;
* }
* }
</pre>
*
</pre>
* And start the spider by:
* And start the spider by:
* <pre>
* <pre>
* OOSpider.create(Site.me().addStartUrl("http://my.oschina.net/flashsword/blog")
* OOSpider.create(Site.me().addStartUrl("http://my.oschina.net/flashsword/blog")
* ,new JsonFilePageModelPipeline(), OschinaBlog.class).run();
* ,new JsonFilePageModelPipeline(), OschinaBlog.class).run();
* }
* }
</pre>
* </pre>
*
* @author code4crafter@gmail.com <br>
* @author code4crafter@gmail.com <br>
* @since 0.2.0
* @since 0.2.0
*/
*/
...
@@ -49,6 +53,7 @@ public class OOSpider extends Spider {
...
@@ -49,6 +53,7 @@ public class OOSpider extends Spider {
/**
/**
* create a spider
* create a spider
*
* @param site
* @param site
* @param pageModelPipeline
* @param pageModelPipeline
* @param pageModels
* @param pageModels
...
@@ -57,7 +62,7 @@ public class OOSpider extends Spider {
...
@@ -57,7 +62,7 @@ public class OOSpider extends Spider {
this
(
ModelPageProcessor
.
create
(
site
,
pageModels
));
this
(
ModelPageProcessor
.
create
(
site
,
pageModels
));
this
.
modelPipeline
=
new
ModelPipeline
();
this
.
modelPipeline
=
new
ModelPipeline
();
super
.
addPipeline
(
modelPipeline
);
super
.
addPipeline
(
modelPipeline
);
if
(
pageModelPipeline
!=
null
)
{
if
(
pageModelPipeline
!=
null
)
{
for
(
Class
pageModel
:
pageModels
)
{
for
(
Class
pageModel
:
pageModels
)
{
this
.
modelPipeline
.
put
(
pageModel
,
pageModelPipeline
);
this
.
modelPipeline
.
put
(
pageModel
,
pageModelPipeline
);
}
}
...
@@ -72,6 +77,22 @@ public class OOSpider extends Spider {
...
@@ -72,6 +77,22 @@ public class OOSpider extends Spider {
return
new
OOSpider
(
site
,
pageModelPipeline
,
pageModels
);
return
new
OOSpider
(
site
,
pageModelPipeline
,
pageModels
);
}
}
public
static
OOSpider
direct
(
Site
site
,
PageModelPipeline
pageModelPipeline
,
Class
...
pageModels
)
{
return
new
OOSpider
(
site
,
pageModelPipeline
,
pageModels
);
}
public
static
OOSpider
direct
(
PageModelPipeline
pageModelPipeline
,
Class
...
pageModels
)
{
return
new
OOSpider
(
null
,
pageModelPipeline
,
pageModels
);
}
public
static
OOSpider
direct
(
Class
...
pageModels
)
{
return
new
OOSpider
(
null
,
null
,
pageModels
);
}
public
static
OOSpider
direct
(
Collection
<
Param
>
params
,
Class
...
pageModels
)
{
return
new
OOSpider
(
null
,
null
,
pageModels
);
}
public
OOSpider
addPageModel
(
PageModelPipeline
pageModelPipeline
,
Class
...
pageModels
)
{
public
OOSpider
addPageModel
(
PageModelPipeline
pageModelPipeline
,
Class
...
pageModels
)
{
for
(
Class
pageModel
:
pageModels
)
{
for
(
Class
pageModel
:
pageModels
)
{
modelPageProcessor
.
addPageModel
(
pageModel
);
modelPageProcessor
.
addPageModel
(
pageModel
);
...
...
webmagic-extension/src/main/java/us/codecraft/webmagic/model/annotation/UrlTemplate.java
0 → 100644
View file @
3b00190f
package
us
.
codecraft
.
webmagic
.
model
.
annotation
;
import
java.lang.annotation.ElementType
;
import
java.lang.annotation.Retention
;
import
java.lang.annotation.Target
;
/**
* Define the url patterns for class. <br>
* All urls matching the pattern will be crawled and extracted for new objects. <br>
*
* @author code4crafter@gmail.com <br>
* @since 0.3.3
*/
@Retention
(
java
.
lang
.
annotation
.
RetentionPolicy
.
RUNTIME
)
@Target
({
ElementType
.
TYPE
})
public
@interface
UrlTemplate
{
/**
* The url patterns for class.<br>
* Use regex expression with some changes: <br>
* "." stand for literal character "." instead of "any character". <br>
* "*" stand for any legal character for url in 0-n length ([^"'#]*) instead of "any length". <br>
*
* @return the url patterns for class
*/
String
value
();
/**
* Define the region for url extracting. <br>
* Only support XPath.<br>
* When sourceRegion is set, the urls will be extracted only from the region instead of entire content. <br>
*
* @return the region for url extracting
*/
String
encoding
()
default
"utf8"
;
}
webmagic-extension/src/main/java/us/codecraft/webmagic/model/direct/Param.java
0 → 100644
View file @
3b00190f
package
us
.
codecraft
.
webmagic
.
model
.
direct
;
import
java.util.LinkedHashMap
;
/**
* @author code4crafter@gmail.com
*/
public
class
Param
extends
LinkedHashMap
<
String
,
Object
>{
@Override
public
Param
put
(
String
key
,
Object
value
)
{
super
.
put
(
key
,
value
);
return
this
;
}
}
webmagic-samples/src/main/java/us/codecraft/webmagic/model/samples/Kr36NewsModel.java
View file @
3b00190f
package
us
.
codecraft
.
webmagic
.
model
.
samples
;
package
us
.
codecraft
.
webmagic
.
model
.
samples
;
import
us.codecraft.webmagic.Site
;
import
us.codecraft.webmagic.Site
;
import
us.codecraft.webmagic.model.ConsolePageModelPipeline
;
import
us.codecraft.webmagic.model.OOSpider
;
import
us.codecraft.webmagic.model.OOSpider
;
import
us.codecraft.webmagic.model.annotation.ExtractBy
;
import
us.codecraft.webmagic.model.annotation.ExtractBy
;
import
us.codecraft.webmagic.model.annotation.ExtractByUrl
;
import
us.codecraft.webmagic.model.annotation.ExtractByUrl
;
import
us.codecraft.webmagic.model.annotation.HelpUrl
;
import
us.codecraft.webmagic.model.annotation.HelpUrl
;
import
us.codecraft.webmagic.model.annotation.TargetUrl
;
import
us.codecraft.webmagic.model.annotation.TargetUrl
;
import
us.codecraft.webmagic.pipeline.JsonFilePageModelPipeline
;
/**
/**
* @author code4crafter@gmail.com <br>
* @author code4crafter@gmail.com <br>
...
@@ -18,14 +18,26 @@ public class Kr36NewsModel {
...
@@ -18,14 +18,26 @@ public class Kr36NewsModel {
@ExtractBy
(
"//h1[@class='entry-title sep10']"
)
@ExtractBy
(
"//h1[@class='entry-title sep10']"
)
private
String
title
;
private
String
title
;
@ExtractBy
(
"//div[@class='mainContent sep-10']"
)
@ExtractBy
(
"//div[@class='mainContent sep-10']
/tidyText()
"
)
private
String
content
;
private
String
content
;
@ExtractByUrl
@ExtractByUrl
private
String
url
;
private
String
url
;
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
OOSpider
.
create
(
Site
.
me
().
addStartUrl
(
"http://www.36kr.com/"
),
new
ConsolePageModelPipeline
(),
OOSpider
.
create
(
Site
.
me
().
addStartUrl
(
"http://www.36kr.com/"
).
setSleepTime
(
0
),
new
JsonFilePageModelPipeline
(),
Kr36NewsModel
.
class
).
run
();
Kr36NewsModel
.
class
).
thread
(
20
).
run
();
}
public
String
getTitle
()
{
return
title
;
}
public
String
getContent
()
{
return
content
;
}
public
String
getUrl
()
{
return
url
;
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment