/
GetYAnswersPropertiesFromQid.java
239 lines (193 loc) · 7.51 KB
/
GetYAnswersPropertiesFromQid.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package org.trec.liveqa;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
/**
* Copyright 2015 Yahoo Inc.<br>
* Licensed under the terms of the MIT license. Please see LICENSE file at the root of this project for terms.
* <p/>
*
* This class scrapes Yahoo Answers pages by a list of QIDs, obtaining certain properties.
*
* @author yuvalp@yahoo-inc.com
*
*/
public class GetYAnswersPropertiesFromQid {
public interface ElementPredicate {
/**
* Given an HTML Element, see if it conforms to a Yahoo Answers tag pattern specific to the desired property.
* @param e
* @return
*/
public boolean check(Element e);
public String getText(Element e);
}
public static class CheckTitle implements ElementPredicate {
@Override
public boolean check(Element e) {
return e.nodeName().equals("meta") && e.attr("name").equals("title");
}
@Override
public String getText(Element e) {
return e.attr("content");
}
}
public static class CheckBody implements ElementPredicate {
@Override
public boolean check(Element e) {
return e.nodeName().equals("meta") && e.attr("name").equals("description");
}
@Override
public String getText(Element e) {
return e.attr("content");
}
}
public static class CheckBestAnswer implements ElementPredicate {
@Override
public boolean check(Element e) {
// implement your own...
return false;
}
@Override
public String getText(Element e) {
// implement your own...
return null;
}
}
public static class CheckTopLevelCategory implements ElementPredicate {
@Override
public boolean check(Element e) {
return e.nodeName().equals("a") && e.className().contains("Clr-b");
}
@Override
public String getText(Element e) {
return e.text();
}
}
// ---------------------------------------------------
private static final String URL_PREFIX = "https://answers.yahoo.com/question/index?qid=";
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss", Locale.US);
private static CheckTitle ct = new CheckTitle();
private static CheckBody cb = new CheckBody();
private static CheckTopLevelCategory cc = new CheckTopLevelCategory();
private static CheckBestAnswer cba = new CheckBestAnswer(); // not implemented
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.out.println("Usage: GetYAnswersPropertiesFromQid <plaintext-list-of-qids> <out-file>");
return;
}
BufferedWriter writer = getWriter(args[1]);
String[] qids = getQids(args[0]);
for (String qid : qids) {
System.out.println("Getting data for QID " + qid);
writer.append(qid + ":");
writer.newLine();
for (Entry<String, String> kv : extractData(qid).entrySet()) {
writer.append(kv.getKey() + "\t" + kv.getValue());
writer.newLine();
}
writer.newLine();
}
writer.flush();
writer.close();
}
private static String[] getQids(String inFile) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(new File(inFile)));
List<String> qids = new ArrayList<>();
String l = reader.readLine();
while (l != null) {
qids.add(l);
l = reader.readLine();
}
reader.close();
return qids.toArray(new String[qids.size()]);
}
private static BufferedWriter getWriter(String fileLocation) throws FileNotFoundException {
return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(fileLocation))));
}
/**
*
* @param iQid question ID
* @return map of features and attributes: question title, body, category, best answer, date
* @throws Exception
*/
public static Map<String, String> extractData(String iQid) throws Exception {
Map<String, String> res = new HashMap<>();
// parse date from qid
res.put("Date", DATE_FORMAT.parse(iQid.substring(0, 14)).toString());
// get and mine html page
String url = URL_PREFIX + iQid;
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(url);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
try {
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}
InputStream responseBody = method.getResponseBodyAsStream();
// strip top levels
Document doc = Jsoup.parse(responseBody, "UTF8", url);
Element html = doc.child(0);
Element body = html.child(1);
Element head = html.child(0);
// get category
res.put("Top level Category", findElementText(body, cc));
// get title
res.put("Title", findElementText(head, ct));
// get body
res.put("Body", findElementText(head, cb));
// get best answer
// res.put("Best Answer", findElementText(body, cba));
responseBody.close();
} catch (HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Fatal transport error: " + e.getMessage());
e.printStackTrace();
} finally {
method.releaseConnection();
}
return res;
}
private static String findElementText(Element topElem, ElementPredicate f) {
Element elem = findElement(topElem, f);
return elem == null ? "" : f.getText(elem);
}
private static Element findElement(Element e, ElementPredicate f) {
if (f.check(e)) {
return e;
}
for (Element c : e.children()) {
Element elem = findElement(c, f);
if (elem != null) {
return elem;
}
}
return null;
}
}