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
df8ca8ad
Commit
df8ca8ad
authored
Nov 10, 2013
by
yihua.huang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add scripts
parent
c2e04ea5
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
165 additions
and
0 deletions
+165
-0
logo-simple.jpg
asserts/logo-simple.jpg
+0
-0
pom.xml
pom.xml
+1
-0
PlainText.java
...c/main/java/us/codecraft/webmagic/selector/PlainText.java
+10
-0
Selectable.java
.../main/java/us/codecraft/webmagic/selector/Selectable.java
+17
-0
pom.xml
webmagic-scripts/pom.xml
+35
-0
ScriptProcessor.java
...java/us/codecraft/webmagic/processor/ScriptProcessor.java
+61
-0
defines.rb
webmagic-scripts/src/main/resources/ruby/defines.rb
+11
-0
oschina.rb
webmagic-scripts/src/main/resources/ruby/oschina.rb
+5
-0
TestJRubyCall.java
.../test/java/us/codecraft/webmagic/jruby/TestJRubyCall.java
+25
-0
No files found.
asserts/logo-simple.jpg
0 → 100644
View file @
df8ca8ad
8.45 KB
pom.xml
View file @
df8ca8ad
...
@@ -48,6 +48,7 @@
...
@@ -48,6 +48,7 @@
<modules>
<modules>
<module>
webmagic-core
</module>
<module>
webmagic-core
</module>
<module>
webmagic-extension/
</module>
<module>
webmagic-extension/
</module>
<module>
webmagic-scripts
</module>
</modules>
</modules>
<dependencyManagement>
<dependencyManagement>
...
...
webmagic-core/src/main/java/us/codecraft/webmagic/selector/PlainText.java
View file @
df8ca8ad
...
@@ -45,6 +45,16 @@ public class PlainText implements Selectable {
...
@@ -45,6 +45,16 @@ public class PlainText implements Selectable {
throw
new
UnsupportedOperationException
();
throw
new
UnsupportedOperationException
();
}
}
@Override
public
Selectable
css
(
String
selector
)
{
return
$
(
selector
);
}
@Override
public
Selectable
css
(
String
selector
,
String
attrName
)
{
return
$
(
selector
,
attrName
);
}
@Override
@Override
public
Selectable
smartContent
()
{
public
Selectable
smartContent
()
{
throw
new
UnsupportedOperationException
();
throw
new
UnsupportedOperationException
();
...
...
webmagic-core/src/main/java/us/codecraft/webmagic/selector/Selectable.java
View file @
df8ca8ad
...
@@ -35,6 +35,23 @@ public interface Selectable {
...
@@ -35,6 +35,23 @@ public interface Selectable {
*/
*/
public
Selectable
$
(
String
selector
,
String
attrName
);
public
Selectable
$
(
String
selector
,
String
attrName
);
/**
* select list with css selector
*
* @param selector css selector expression
* @return new Selectable after extract
*/
public
Selectable
css
(
String
selector
);
/**
* select list with css selector
*
* @param selector css selector expression
* @param attrName attribute name of css selector
* @return new Selectable after extract
*/
public
Selectable
css
(
String
selector
,
String
attrName
);
/**
/**
* select smart content with ReadAbility algorithm
* select smart content with ReadAbility algorithm
*
*
...
...
webmagic-scripts/pom.xml
0 → 100644
View file @
df8ca8ad
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<parent>
<artifactId>
webmagic-parent
</artifactId>
<groupId>
us.codecraft
</groupId>
<version>
0.4.1-SNAPSHOT
</version>
</parent>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
us.codecraft
</groupId>
<artifactId>
webmagic-scripts
</artifactId>
<version>
0.4.1-SNAPSHOT
</version>
<dependencies>
<dependency>
<groupId>
org.jruby
</groupId>
<artifactId>
jruby
</artifactId>
<version>
1.7.6
</version>
</dependency>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
us.codecraft
</groupId>
<artifactId>
webmagic-core
</artifactId>
<version>
${project.version}
</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
webmagic-scripts/src/main/java/us/codecraft/webmagic/processor/ScriptProcessor.java
0 → 100644
View file @
df8ca8ad
package
us
.
codecraft
.
webmagic
.
processor
;
import
org.apache.commons.io.IOUtils
;
import
us.codecraft.webmagic.Page
;
import
us.codecraft.webmagic.Site
;
import
us.codecraft.webmagic.Spider
;
import
javax.script.ScriptContext
;
import
javax.script.ScriptEngine
;
import
javax.script.ScriptEngineManager
;
import
javax.script.ScriptException
;
import
java.io.IOException
;
import
java.io.InputStream
;
/**
* @author code4crafter@gmail.com
*/
public
class
ScriptProcessor
implements
PageProcessor
{
private
ScriptEngine
rubyEngine
;
private
String
defines
;
ScriptProcessor
(){
ScriptEngineManager
manager
=
new
ScriptEngineManager
();
rubyEngine
=
manager
.
getEngineByName
(
"jruby"
);
InputStream
resourceAsStream
=
this
.
getClass
().
getClassLoader
().
getResourceAsStream
(
"ruby/defines.rb"
);
try
{
defines
=
IOUtils
.
toString
(
resourceAsStream
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
@Override
public
void
process
(
Page
page
)
{
ScriptContext
context
=
rubyEngine
.
getContext
();
context
.
setAttribute
(
"page"
,
page
,
ScriptContext
.
ENGINE_SCOPE
);
String
script
;
try
{
InputStream
resourceAsStream
=
this
.
getClass
().
getClassLoader
().
getResourceAsStream
(
"ruby/oschina.rb"
);
try
{
script
=
IOUtils
.
toString
(
resourceAsStream
);
rubyEngine
.
eval
(
defines
+
script
,
context
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
catch
(
ScriptException
e
)
{
e
.
printStackTrace
();
}
}
@Override
public
Site
getSite
()
{
return
Site
.
me
();
}
public
static
void
main
(
String
[]
args
)
{
Spider
.
create
(
new
ScriptProcessor
()).
addUrl
(
"http://my.oschina.net/flashsword/blog"
).
run
();
}
}
webmagic-scripts/src/main/resources/ruby/defines.rb
0 → 100644
View file @
df8ca8ad
def
xpath
str
$page
.
getHtml
().
xpath
(
str
).
toString
()
end
def
css
str
$page
.
getHtml
().
css
(
str
).
toString
()
end
def
urls
str
links
=
$page
.
getHtml
().
links
().
regex
(
str
).
all
();
$page
.
addTargetRequests
(
links
);
end
webmagic-scripts/src/main/resources/ruby/oschina.rb
0 → 100644
View file @
df8ca8ad
title
=
css
"div.BlogTitle h1"
content
=
css
"div.BlogContent"
urls
"http://my
\\
.oschina
\\
.net/flashsword/blog/
\\
d+"
puts
title
puts
content
\ No newline at end of file
webmagic-scripts/src/test/java/us/codecraft/webmagic/jruby/TestJRubyCall.java
0 → 100644
View file @
df8ca8ad
package
us
.
codecraft
.
webmagic
.
jruby
;
import
org.junit.Test
;
import
javax.script.ScriptContext
;
import
javax.script.ScriptEngine
;
import
javax.script.ScriptEngineManager
;
import
javax.script.ScriptException
;
/**
* @author code4crafter@gmail.com
*/
public
class
TestJRubyCall
{
@Test
public
void
test
()
throws
ScriptException
{
ScriptEngineManager
manager
=
new
ScriptEngineManager
();
ScriptEngine
rubyEngine
=
manager
.
getEngineByName
(
"jruby"
);
ScriptContext
context
=
rubyEngine
.
getContext
();
context
.
setAttribute
(
"a"
,
"sad"
,
ScriptContext
.
ENGINE_SCOPE
);
// rubyEngine.eval("", context);
rubyEngine
.
eval
(
"b=1; puts b"
,
context
);
}
}
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