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
ce3f0ac2
Commit
ce3f0ac2
authored
Apr 09, 2017
by
GZhY
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
删除 fixAllRelativeHrefs 并修复 SeleniumDownloader 对 fixAllRelativeHrefs 的依赖
parent
bc6e81e0
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
1 addition
and
58 deletions
+1
-58
UrlUtils.java
...e/src/main/java/us/codecraft/webmagic/utils/UrlUtils.java
+0
-35
UrlUtilsTest.java
...c/test/java/us/codecraft/webmagic/utils/UrlUtilsTest.java
+0
-19
SeleniumDownloader.java
...raft/webmagic/downloader/selenium/SeleniumDownloader.java
+1
-4
No files found.
webmagic-core/src/main/java/us/codecraft/webmagic/utils/UrlUtils.java
View file @
ce3f0ac2
...
...
@@ -92,41 +92,6 @@ public class UrlUtils {
}
}
/**
* allow blank space in quote
*/
private
static
Pattern
patternForHrefWithQuote
=
Pattern
.
compile
(
"(<a[^<>]*href=)[\"']([^\"'<>]*)[\"']"
,
Pattern
.
CASE_INSENSITIVE
);
/**
* disallow blank space without quote
*/
private
static
Pattern
patternForHrefWithoutQuote
=
Pattern
.
compile
(
"(<a[^<>]*href=)([^\"'<>\\s]+)"
,
Pattern
.
CASE_INSENSITIVE
);
public
static
String
fixAllRelativeHrefs
(
String
html
,
String
url
)
{
html
=
replaceByPattern
(
html
,
url
,
patternForHrefWithQuote
);
html
=
replaceByPattern
(
html
,
url
,
patternForHrefWithoutQuote
);
return
html
;
}
public
static
String
replaceByPattern
(
String
html
,
String
url
,
Pattern
pattern
)
{
StringBuilder
stringBuilder
=
new
StringBuilder
();
Matcher
matcher
=
pattern
.
matcher
(
html
);
int
lastEnd
=
0
;
boolean
modified
=
false
;
while
(
matcher
.
find
())
{
modified
=
true
;
stringBuilder
.
append
(
StringUtils
.
substring
(
html
,
lastEnd
,
matcher
.
start
()));
stringBuilder
.
append
(
matcher
.
group
(
1
));
stringBuilder
.
append
(
"\""
).
append
(
canonicalizeUrl
(
matcher
.
group
(
2
),
url
)).
append
(
"\""
);
lastEnd
=
matcher
.
end
();
}
if
(!
modified
)
{
return
html
;
}
stringBuilder
.
append
(
StringUtils
.
substring
(
html
,
lastEnd
));
return
stringBuilder
.
toString
();
}
public
static
List
<
Request
>
convertToRequests
(
Collection
<
String
>
urls
)
{
List
<
Request
>
requestList
=
new
ArrayList
<
Request
>(
urls
.
size
());
for
(
String
url
:
urls
)
{
...
...
webmagic-core/src/test/java/us/codecraft/webmagic/utils/UrlUtilsTest.java
View file @
ce3f0ac2
...
...
@@ -33,25 +33,6 @@ public class UrlUtilsTest {
assertThat
(
absoluteUrl
).
isEqualTo
(
"http://www.dianping.com/aa"
);
}
@Test
public
void
testFixAllRelativeHrefs
()
{
String
originHtml
=
"<a href=\"/start\">"
;
String
replacedHtml
=
UrlUtils
.
fixAllRelativeHrefs
(
originHtml
,
"http://www.dianping.com/"
);
assertThat
(
replacedHtml
).
isEqualTo
(
"<a href=\"http://www.dianping.com/start\">"
);
originHtml
=
"<a href=\"/start a\">"
;
replacedHtml
=
UrlUtils
.
fixAllRelativeHrefs
(
originHtml
,
"http://www.dianping.com/"
);
assertThat
(
replacedHtml
).
isEqualTo
(
"<a href=\"http://www.dianping.com/start%20a\">"
);
originHtml
=
"<a href='/start a'>"
;
replacedHtml
=
UrlUtils
.
fixAllRelativeHrefs
(
originHtml
,
"http://www.dianping.com/"
);
assertThat
(
replacedHtml
).
isEqualTo
(
"<a href=\"http://www.dianping.com/start%20a\">"
);
originHtml
=
"<a href=/start tag>"
;
replacedHtml
=
UrlUtils
.
fixAllRelativeHrefs
(
originHtml
,
"http://www.dianping.com/"
);
assertThat
(
replacedHtml
).
isEqualTo
(
"<a href=\"http://www.dianping.com/start\" tag>"
);
}
@Test
public
void
testGetDomain
(){
String
url
=
"http://www.dianping.com/aa/"
;
...
...
webmagic-selenium/src/main/java/us/codecraft/webmagic/downloader/selenium/SeleniumDownloader.java
View file @
ce3f0ac2
...
...
@@ -5,7 +5,6 @@ import org.openqa.selenium.By;
import
org.openqa.selenium.Cookie
;
import
org.openqa.selenium.WebDriver
;
import
org.openqa.selenium.WebElement
;
import
us.codecraft.webmagic.Page
;
import
us.codecraft.webmagic.Request
;
import
us.codecraft.webmagic.Site
;
...
...
@@ -13,7 +12,6 @@ import us.codecraft.webmagic.Task;
import
us.codecraft.webmagic.downloader.Downloader
;
import
us.codecraft.webmagic.selector.Html
;
import
us.codecraft.webmagic.selector.PlainText
;
import
us.codecraft.webmagic.utils.UrlUtils
;
import
java.io.Closeable
;
import
java.io.IOException
;
...
...
@@ -108,8 +106,7 @@ public class SeleniumDownloader implements Downloader, Closeable {
String
content
=
webElement
.
getAttribute
(
"outerHTML"
);
Page
page
=
new
Page
();
page
.
setRawText
(
content
);
page
.
setHtml
(
new
Html
(
UrlUtils
.
fixAllRelativeHrefs
(
content
,
request
.
getUrl
())));
page
.
setHtml
(
new
Html
(
content
,
request
.
getUrl
()));
page
.
setUrl
(
new
PlainText
(
request
.
getUrl
()));
page
.
setRequest
(
request
);
webDriverPool
.
returnToPool
(
webDriver
);
...
...
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