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
1148450f
Commit
1148450f
authored
Aug 17, 2013
by
yihua.huang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update filecache to more useful
parent
7829c8fe
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
141 additions
and
4 deletions
+141
-4
SimplePageProcessor.java
.../us/codecraft/webmagic/processor/SimplePageProcessor.java
+1
-3
FileCache.java
...main/java/us/codecraft/webmagic/downloader/FileCache.java
+122
-0
FileCacheTest.java
.../java/us/codecraft/webmagic/downloader/FileCacheTest.java
+17
-0
FileDownloader-cmnt.xml
.../us/codecraft/webmagic/downloader/FileDownloader-cmnt.xml
+1
-1
No files found.
webmagic-core/src/main/java/us/codecraft/webmagic/processor/SimplePageProcessor.java
View file @
1148450f
...
@@ -16,13 +16,11 @@ public class SimplePageProcessor implements PageProcessor {
...
@@ -16,13 +16,11 @@ public class SimplePageProcessor implements PageProcessor {
private
String
urlPattern
;
private
String
urlPattern
;
private
static
final
String
UA
=
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31"
;
private
Site
site
;
private
Site
site
;
public
SimplePageProcessor
(
String
startUrl
,
String
urlPattern
)
{
public
SimplePageProcessor
(
String
startUrl
,
String
urlPattern
)
{
this
.
site
=
Site
.
me
().
addStartUrl
(
startUrl
).
this
.
site
=
Site
.
me
().
addStartUrl
(
startUrl
).
setDomain
(
UrlUtils
.
getDomain
(
startUrl
))
.
setUserAgent
(
UA
)
;
setDomain
(
UrlUtils
.
getDomain
(
startUrl
));
//compile "*" expression to regex
//compile "*" expression to regex
this
.
urlPattern
=
"("
+
urlPattern
.
replace
(
"."
,
"\\."
).
replace
(
"*"
,
"[^\"'#]*"
)+
")"
;
this
.
urlPattern
=
"("
+
urlPattern
.
replace
(
"."
,
"\\."
).
replace
(
"*"
,
"[^\"'#]*"
)+
")"
;
...
...
webmagic-extension/src/main/java/us/codecraft/webmagic/downloader/File
Downloader
.java
→
webmagic-extension/src/main/java/us/codecraft/webmagic/downloader/File
Cache
.java
View file @
1148450f
...
@@ -3,42 +3,45 @@ package us.codecraft.webmagic.downloader;
...
@@ -3,42 +3,45 @@ package us.codecraft.webmagic.downloader;
import
org.apache.commons.codec.digest.DigestUtils
;
import
org.apache.commons.codec.digest.DigestUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.log4j.Logger
;
import
org.apache.log4j.Logger
;
import
us.codecraft.webmagic.Page
;
import
us.codecraft.webmagic.*
;
import
us.codecraft.webmagic.Request
;
import
us.codecraft.webmagic.pipeline.Pipeline
;
import
us.codecraft.webmagic.Task
;
import
us.codecraft.webmagic.processor.PageProcessor
;
import
us.codecraft.webmagic.processor.SimplePageProcessor
;
import
us.codecraft.webmagic.selector.Html
;
import
us.codecraft.webmagic.selector.Html
;
import
us.codecraft.webmagic.selector.PlainText
;
import
us.codecraft.webmagic.selector.PlainText
;
import
us.codecraft.webmagic.utils.FilePersistentBase
;
import
us.codecraft.webmagic.utils.UrlUtils
;
import
java.io.*
;
import
java.io.*
;
/**
/**
* 使用缓存到本地的文件来模拟下载,可以在Spider框架中仅进行抽取工作。<br>
* Download file and saved to file for cache.<br>
* @author code4crafer@gmail.com
*
* Date: 13-6-24
*
* Time: 上午7:24
* @author code4crafter@gmail.com
* @since 0.2.1
*/
*/
public
class
FileDownloader
implements
Downloader
{
public
class
FileCache
extends
FilePersistentBase
implements
Downloader
,
Pipeline
,
PageProcessor
{
private
String
path
=
"/data/temp/webmagic/"
;
private
Downloader
downloaderWhenFileMiss
;
private
Downloader
downloaderWhenFileMiss
;
private
final
PageProcessor
pageProcessor
;
private
Logger
logger
=
Logger
.
getLogger
(
getClass
());
private
Logger
logger
=
Logger
.
getLogger
(
getClass
());
public
File
Downloader
(
)
{
public
File
Cache
(
String
startUrl
,
String
urlPattern
)
{
this
(
"/data/temp/webmagic/"
,
null
);
this
(
startUrl
,
urlPattern
,
"/data/webmagic/temp/"
);
}
}
public
FileDownloader
(
String
path
)
{
public
FileCache
(
String
startUrl
,
String
urlPattern
,
String
path
)
{
this
(
path
,
null
);
this
.
pageProcessor
=
new
SimplePageProcessor
(
startUrl
,
urlPattern
);
setPath
(
path
);
downloaderWhenFileMiss
=
new
HttpClientDownloader
();
}
}
public
FileDownloader
(
String
path
,
Downloader
downloaderWhenFileMiss
)
{
public
FileCache
setDownloaderWhenFileMiss
(
Downloader
downloaderWhenFileMiss
)
{
if
(!
path
.
endsWith
(
"/"
)&&!
path
.
endsWith
(
"\\"
)){
path
+=
"/"
;
}
this
.
path
=
path
;
this
.
downloaderWhenFileMiss
=
downloaderWhenFileMiss
;
this
.
downloaderWhenFileMiss
=
downloaderWhenFileMiss
;
return
this
;
}
}
@Override
@Override
...
@@ -46,16 +49,15 @@ public class FileDownloader implements Downloader {
...
@@ -46,16 +49,15 @@ public class FileDownloader implements Downloader {
String
path
=
this
.
path
+
"/"
+
task
.
getUUID
()
+
"/"
;
String
path
=
this
.
path
+
"/"
+
task
.
getUUID
()
+
"/"
;
Page
page
=
null
;
Page
page
=
null
;
try
{
try
{
final
File
file
=
new
File
(
path
+
DigestUtils
.
md5Hex
(
request
.
getUrl
()));
final
File
file
=
get
File
(
path
+
DigestUtils
.
md5Hex
(
request
.
getUrl
()));
BufferedReader
bufferedReader
=
new
BufferedReader
(
new
FileReader
(
file
));
BufferedReader
bufferedReader
=
new
BufferedReader
(
new
FileReader
(
file
));
String
line
=
null
;
String
line
=
bufferedReader
.
readLine
();
line
=
bufferedReader
.
readLine
();
if
(
line
.
equals
(
"url:\t"
+
request
.
getUrl
()))
{
if
(
line
.
equals
(
"url:\t"
+
request
.
getUrl
()))
{
final
String
html
=
getHtml
(
bufferedReader
);
final
String
html
=
getHtml
(
bufferedReader
);
page
=
new
Page
();
page
=
new
Page
();
page
.
setRequest
(
request
);
page
.
setRequest
(
request
);
page
.
setUrl
(
PlainText
.
create
(
request
.
getUrl
()));
page
.
setUrl
(
PlainText
.
create
(
request
.
getUrl
()));
page
.
setHtml
(
Html
.
create
(
html
));
page
.
setHtml
(
Html
.
create
(
UrlUtils
.
fixAllRelativeHrefs
(
html
,
request
.
getUrl
())
));
}
}
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
if
(
e
instanceof
FileNotFoundException
)
{
if
(
e
instanceof
FileNotFoundException
)
{
...
@@ -77,11 +79,11 @@ public class FileDownloader implements Downloader {
...
@@ -77,11 +79,11 @@ public class FileDownloader implements Downloader {
private
String
getHtml
(
BufferedReader
bufferedReader
)
throws
IOException
{
private
String
getHtml
(
BufferedReader
bufferedReader
)
throws
IOException
{
String
line
;
String
line
;
StringBuilder
htmlBuilder
=
new
StringBuilder
();
StringBuilder
htmlBuilder
=
new
StringBuilder
();
line
=
bufferedReader
.
readLine
();
line
=
bufferedReader
.
readLine
();
line
=
StringUtils
.
removeStart
(
line
,
"html:\t"
);
line
=
StringUtils
.
removeStart
(
line
,
"html:\t"
);
htmlBuilder
.
append
(
line
);
htmlBuilder
.
append
(
line
);
while
((
line
=
bufferedReader
.
readLine
())!=
null
)
{
while
((
line
=
bufferedReader
.
readLine
())
!=
null
)
{
htmlBuilder
.
append
(
line
);
htmlBuilder
.
append
(
line
);
}
}
return
htmlBuilder
.
toString
();
return
htmlBuilder
.
toString
();
...
@@ -94,4 +96,27 @@ public class FileDownloader implements Downloader {
...
@@ -94,4 +96,27 @@ public class FileDownloader implements Downloader {
}
}
return
page
;
return
page
;
}
}
@Override
public
void
process
(
ResultItems
resultItems
,
Task
task
)
{
String
path
=
this
.
path
+
PATH_SEPERATOR
+
task
.
getUUID
()
+
PATH_SEPERATOR
;
try
{
PrintWriter
printWriter
=
new
PrintWriter
(
new
FileWriter
(
getFile
(
path
+
DigestUtils
.
md5Hex
(
resultItems
.
getRequest
().
getUrl
())
+
".html"
)));
printWriter
.
println
(
"url:\t"
+
resultItems
.
getRequest
().
getUrl
());
printWriter
.
println
(
"html:\t"
+
resultItems
.
get
(
"html"
));
printWriter
.
close
();
}
catch
(
IOException
e
)
{
logger
.
warn
(
"write file error"
,
e
);
}
}
@Override
public
void
process
(
Page
page
)
{
pageProcessor
.
process
(
page
);
}
@Override
public
Site
getSite
()
{
return
pageProcessor
.
getSite
();
}
}
}
webmagic-extension/src/test/java/us/codecraft/webmagic/downloader/FileCacheTest.java
0 → 100644
View file @
1148450f
package
us
.
codecraft
.
webmagic
.
downloader
;
import
org.junit.Test
;
import
us.codecraft.webmagic.Spider
;
/**
* @author code4crafter@gmail.com <br>
*/
public
class
FileCacheTest
{
// @Ignore("takes long")
@Test
public
void
test
()
{
FileCache
fileCache
=
new
FileCache
(
"http://my.oschina.net/flashsword/blog"
,
"http://my.oschina.net/flashsword/blog/*"
);
Spider
.
create
(
fileCache
).
downloader
(
fileCache
).
pipeline
(
fileCache
).
run
();
}
}
zh_docs/us/codecraft/webmagic/downloader/FileDownloader-cmnt.xml
View file @
1148450f
...
@@ -4,7 +4,7 @@
...
@@ -4,7 +4,7 @@
<date-generated>
Sat Aug 17 14:14:45 CST 2013
</date-generated>
<date-generated>
Sat Aug 17 14:14:45 CST 2013
</date-generated>
</meta>
</meta>
<comment>
<comment>
<key>
<![CDATA[us.codecraft.webmagic.downloader.File
Downloader
]]>
</key>
<key>
<![CDATA[us.codecraft.webmagic.downloader.File
Cache
]]>
</key>
<data>
<![CDATA[ 使用缓存到本地的文件来模拟下载,可以在Spider框架中仅进行抽取工作。<br>
<data>
<![CDATA[ 使用缓存到本地的文件来模拟下载,可以在Spider框架中仅进行抽取工作。<br>
@author code4crafer@gmail.com
@author code4crafer@gmail.com
Date: 13-6-24
Date: 13-6-24
...
...
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