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
7fbe18b8
Commit
7fbe18b8
authored
May 05, 2014
by
yihua.huang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implementation of PageMapper #120
parent
5dc9fe95
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
6 deletions
+33
-6
GithubRepo.java
...c/main/java/us/codecraft/webmagic/example/GithubRepo.java
+13
-0
GithubRepoPageMapper.java
...a/us/codecraft/webmagic/example/GithubRepoPageMapper.java
+9
-4
PageMapper.java
...src/main/java/us/codecraft/webmagic/model/PageMapper.java
+11
-2
No files found.
webmagic-extension/src/main/java/us/codecraft/webmagic/example/GithubRepo.java
View file @
7fbe18b8
...
...
@@ -78,4 +78,17 @@ public class GithubRepo implements HasKey {
public
int
getFork
()
{
return
fork
;
}
@Override
public
String
toString
()
{
return
"GithubRepo{"
+
"name='"
+
name
+
'\''
+
", author='"
+
author
+
'\''
+
", readme='"
+
readme
+
'\''
+
", language="
+
language
+
", star="
+
star
+
", fork="
+
fork
+
", url='"
+
url
+
'\''
+
'}'
;
}
}
webmagic-extension/src/main/java/us/codecraft/webmagic/example/GithubRepoPage
Processo
r.java
→
webmagic-extension/src/main/java/us/codecraft/webmagic/example/GithubRepoPage
Mappe
r.java
View file @
7fbe18b8
...
...
@@ -10,9 +10,9 @@ import us.codecraft.webmagic.processor.PageProcessor;
* @author code4crafter@gmail.com <br>
* @since 0.3.2
*/
public
class
GithubRepoPage
Processo
r
implements
PageProcessor
{
public
class
GithubRepoPage
Mappe
r
implements
PageProcessor
{
private
Site
site
=
Site
.
me
().
setRetryTimes
(
3
);
private
Site
site
=
Site
.
me
().
setRetryTimes
(
3
)
.
setSleepTime
(
0
)
;
private
PageMapper
<
GithubRepo
>
githubRepoPageMapper
=
new
PageMapper
<
GithubRepo
>(
GithubRepo
.
class
);
...
...
@@ -21,7 +21,12 @@ public class GithubRepoPageProcessor implements PageProcessor {
page
.
addTargetRequests
(
page
.
getHtml
().
links
().
regex
(
"(https://github\\.com/\\w+/\\w+)"
).
all
());
page
.
addTargetRequests
(
page
.
getHtml
().
links
().
regex
(
"(https://github\\.com/\\w+)"
).
all
());
GithubRepo
githubRepo
=
githubRepoPageMapper
.
get
(
page
);
page
.
putField
(
"repo"
,
githubRepo
);
if
(
githubRepo
==
null
)
{
page
.
setSkip
(
true
);
}
else
{
page
.
putField
(
"repo"
,
githubRepo
);
}
}
@Override
...
...
@@ -30,6 +35,6 @@ public class GithubRepoPageProcessor implements PageProcessor {
}
public
static
void
main
(
String
[]
args
)
{
Spider
.
create
(
new
GithubRepoPage
Processo
r
()).
addUrl
(
"https://github.com/code4craft"
).
thread
(
5
).
run
();
Spider
.
create
(
new
GithubRepoPage
Mappe
r
()).
addUrl
(
"https://github.com/code4craft"
).
thread
(
5
).
run
();
}
}
\ No newline at end of file
webmagic-extension/src/main/java/us/codecraft/webmagic/model/PageMapper.java
View file @
7fbe18b8
...
...
@@ -2,6 +2,8 @@ package us.codecraft.webmagic.model;
import
us.codecraft.webmagic.Page
;
import
java.util.List
;
/**
* @author code4crafer@gmail.com
* @since 0.5.2
...
...
@@ -10,11 +12,18 @@ public class PageMapper<T> {
private
Class
<
T
>
clazz
;
private
PageModelExtractor
pageModelExtractor
;
public
PageMapper
(
Class
<
T
>
clazz
)
{
this
.
clazz
=
clazz
;
this
.
pageModelExtractor
=
PageModelExtractor
.
create
(
clazz
);
}
public
T
get
(
Page
page
)
{
return
(
T
)
pageModelExtractor
.
process
(
page
);
}
public
T
get
(
Page
page
)
{
return
null
;
public
List
<
T
>
getAll
(
Page
page
)
{
return
(
List
<
T
>)
pageModelExtractor
.
process
(
page
)
;
}
}
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