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
6db681ba
Commit
6db681ba
authored
Mar 15, 2018
by
shenjunlin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加data5U动态代理
parent
c28453e2
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
74 additions
and
2 deletions
+74
-2
HttpClientUtils.java
...ain/java/us/codecraft/webmagic/utils/HttpClientUtils.java
+15
-0
log4j.xml
webmagic-core/src/main/resources/log4j.xml
+1
-1
Data5UProxyProvider.java
...java/us/codecraft/webmagic/proxy/Data5UProxyProvider.java
+57
-0
log4j.xml
webmagic-extension/src/main/resources/log4j.xml
+1
-1
No files found.
webmagic-core/src/main/java/us/codecraft/webmagic/utils/HttpClientUtils.java
View file @
6db681ba
package
us
.
codecraft
.
webmagic
.
utils
;
import
org.apache.http.Header
;
import
org.apache.http.HttpEntity
;
import
org.apache.http.client.methods.CloseableHttpResponse
;
import
org.apache.http.client.methods.HttpGet
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.HttpClients
;
import
org.apache.http.util.EntityUtils
;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
...
...
@@ -25,4 +32,12 @@ public abstract class HttpClientUtils {
}
return
results
;
}
public
static
String
doGet
(
String
url
)
throws
IOException
{
CloseableHttpClient
httpClient
=
HttpClients
.
createDefault
();
HttpGet
httpget
=
new
HttpGet
(
url
);
CloseableHttpResponse
response
=
httpClient
.
execute
(
httpget
);
HttpEntity
httpEntity
=
response
.
getEntity
();
return
EntityUtils
.
toString
(
httpEntity
);
}
}
webmagic-core/src/main/resources/log4j.xml
View file @
6db681ba
...
...
@@ -9,7 +9,7 @@
</appender>
<logger
name=
"org.apache"
additivity=
"false"
>
<level
value=
"
warn
"
/>
<level
value=
"
error
"
/>
<appender-ref
ref=
"stdout"
/>
</logger>
...
...
webmagic-extension/src/main/java/us/codecraft/webmagic/proxy/Data5UProxyProvider.java
0 → 100644
View file @
6db681ba
package
us
.
codecraft
.
webmagic
.
proxy
;
import
com.google.common.cache.Cache
;
import
com.google.common.cache.CacheBuilder
;
import
org.apache.commons.lang3.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
us.codecraft.webmagic.Page
;
import
us.codecraft.webmagic.Task
;
import
us.codecraft.webmagic.utils.HttpClientUtils
;
import
java.io.IOException
;
import
java.util.concurrent.TimeUnit
;
/**
* data5U的动态代理
* 官方网址:http://www.data5u.com/
* 使用时,建议选择1分钟有效期,并且不带ttl参数
*/
public
class
Data5UProxyProvider
implements
ProxyProvider
{
private
Logger
logger
=
LoggerFactory
.
getLogger
(
getClass
());
private
static
Cache
<
String
,
Proxy
>
proxyCache
=
CacheBuilder
.
newBuilder
()
.
expireAfterWrite
(
50
,
TimeUnit
.
SECONDS
)
//20秒过期,Data5u提供的代理有1分钟过期,2分钟过期,5分钟过期
.
maximumSize
(
10
).
build
();
private
String
data5UApiUrl
;
public
Data5UProxyProvider
(
String
orderId
){
this
.
data5UApiUrl
=
"http://api.ip.data5u.com/dynamic/get.html?order="
+
orderId
;
}
@Override
public
void
returnProxy
(
Proxy
proxy
,
Page
page
,
Task
task
)
{
//do nothing
}
@Override
public
Proxy
getProxy
(
Task
task
)
{
final
String
key
=
"proxy"
;
try
{
return
proxyCache
.
get
(
key
,
()
->
getProxyFromData5u
());
}
catch
(
Exception
e
)
{
logger
.
warn
(
"get proxy from data5U fail {}"
,
e
);
return
null
;
}
}
//返回结果 221.32.22.54:33084
private
Proxy
getProxyFromData5u
()
throws
IOException
{
String
result
=
HttpClientUtils
.
doGet
(
data5UApiUrl
);
String
[]
res
=
StringUtils
.
split
(
result
,
"\n"
);
String
[]
ipAndPort
=
StringUtils
.
split
(
res
[
0
],
":"
);
return
new
Proxy
(
ipAndPort
[
0
],
Integer
.
valueOf
(
ipAndPort
[
1
]));
}
}
webmagic-extension/src/main/resources/log4j.xml
View file @
6db681ba
...
...
@@ -9,7 +9,7 @@
</appender>
<logger
name=
"org.apache"
additivity=
"false"
>
<level
value=
"
warn
"
/>
<level
value=
"
error
"
/>
<appender-ref
ref=
"stdout"
/>
</logger>
...
...
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