|
1 | 1 | package downloader; |
2 | 2 |
|
| 3 | +import java.io.BufferedReader; |
3 | 4 | import java.io.IOException; |
| 5 | +import java.io.InputStream; |
| 6 | +import java.io.InputStreamReader; |
4 | 7 | import java.util.ArrayList; |
5 | 8 | import java.util.HashMap; |
6 | 9 | import java.util.Iterator; |
|
24 | 27 | import org.apache.http.message.BasicNameValuePair; |
25 | 28 | import org.apache.http.util.EntityUtils; |
26 | 29 |
|
| 30 | +import selector.XpathSelector; |
| 31 | + |
| 32 | + |
| 33 | + |
27 | 34 | public class DownloaderTest { |
28 | 35 | private List<String> ProblemDescriptionPath = new ArrayList<String>(); |
29 | 36 | private List<String> ProblemSubmissionPath = new ArrayList<String>(); |
@@ -60,6 +67,9 @@ public static void main(String[] args) throws ClientProtocolException, IOExcepti |
60 | 67 | HttpGet httpGet1 = new HttpGet("https://leetcode.com/problems/rotate-array/submissions/"); |
61 | 68 | response1 = httpClient.execute(httpGet1); |
62 | 69 | 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()); |
63 | 73 | entity1 = response1.getEntity(); |
64 | 74 | EntityUtils.consume(entity1); |
65 | 75 | } |
@@ -105,4 +115,29 @@ public static String getCookie(HttpResponse httpResponse) { |
105 | 115 | return null; |
106 | 116 | } |
107 | 117 |
|
| 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 | + } |
108 | 143 | } |
0 commit comments