Skip to content

Commit 5d99f27

Browse files
committed
change pattern
1 parent d9971a1 commit 5d99f27

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

src/main/java/downloader/DownloaderTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package downloader;
22

3+
import java.io.BufferedReader;
34
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.InputStreamReader;
47
import java.util.ArrayList;
58
import java.util.HashMap;
69
import java.util.Iterator;
@@ -24,6 +27,10 @@
2427
import org.apache.http.message.BasicNameValuePair;
2528
import org.apache.http.util.EntityUtils;
2629

30+
import selector.XpathSelector;
31+
32+
33+
2734
public class DownloaderTest {
2835
private List<String> ProblemDescriptionPath = new ArrayList<String>();
2936
private List<String> ProblemSubmissionPath = new ArrayList<String>();
@@ -60,6 +67,9 @@ public static void main(String[] args) throws ClientProtocolException, IOExcepti
6067
HttpGet httpGet1 = new HttpGet("https://leetcode.com/problems/rotate-array/submissions/");
6168
response1 = httpClient.execute(httpGet1);
6269
printResponse(response1);
70+
XpathSelector xpathSelector = new XpathSelector("//div[@class='row']/div/div/a/@href");
71+
List<String> listTmp = xpathSelector.selectList(getHtml(response1));
72+
System.out.println(listTmp.size());
6373
entity1 = response1.getEntity();
6474
EntityUtils.consume(entity1);
6575
}
@@ -105,4 +115,29 @@ public static String getCookie(HttpResponse httpResponse) {
105115
return null;
106116
}
107117

118+
public static String getHtml(HttpResponse response) {
119+
BufferedReader br = null;
120+
try {
121+
InputStream responseBody = response.getEntity().getContent();
122+
StringBuilder stringBuilder = new StringBuilder();
123+
br = new BufferedReader(new InputStreamReader(
124+
responseBody));
125+
String line = null;
126+
while ((line = br.readLine()) != null) {
127+
stringBuilder.append("\n" + line);
128+
}
129+
return stringBuilder.toString();
130+
} catch (IllegalStateException e) {
131+
System.out.println(e.getMessage());
132+
} catch (IOException e) {
133+
System.out.println(e.getMessage());
134+
} finally {
135+
try {
136+
br.close();
137+
} catch (IOException e) {
138+
System.out.println(e.getMessage());
139+
}
140+
}
141+
return null;
142+
}
108143
}

src/main/java/downloader/HttpClientDownloader.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.util.Map.Entry;
1414

1515
import org.apache.http.Header;
16-
import org.apache.http.HeaderIterator;
1716
import org.apache.http.HttpEntity;
1817
import org.apache.http.HttpResponse;
1918
import org.apache.http.NameValuePair;
@@ -30,7 +29,8 @@
3029
import org.apache.log4j.Logger;
3130
import org.apache.log4j.PropertyConfigurator;
3231

33-
import us.codecraft.webmagic.selector.XpathSelector;
32+
import selector.XpathSelector;
33+
3434

3535
public class HttpClientDownloader implements Downloader {
3636
private static final String ALGORITHMS = "https://www.leetcode.com/problemset/algorithms/";
@@ -163,9 +163,7 @@ public List<String> doDispatcher(String url, String pattern) {
163163
HttpEntity entity1 = response1.getEntity();
164164
EntityUtils.consume(entity1);
165165
return listTmp;
166-
} catch (ClientProtocolException e) {
167-
System.out.println(e.getMessage());
168-
} catch (IOException e) {
166+
} catch (Exception e) {
169167
System.out.println(e.getMessage());
170168
}
171169
return null;
@@ -199,9 +197,6 @@ public static void printResponse(HttpResponse httpResponse)
199197
// System.out.println("response content:"
200198
// + responseString.replace("\r\n", ""));
201199
// }
202-
PropertyConfigurator.configure("test.log");
203-
Logger m_log = Logger.getLogger(HttpClientDownloader.class);
204-
m_log.debug("hello world");
205200
}
206201

207202
/**

0 commit comments

Comments
 (0)