我可以使用 AsyncHttpResponseHandler 或 AsyncHttpClient 类 找到 HTML 标签吗?
Can I find HTML tags using AsyncHttpResponseHandler or AsyncHttpClient classes?
我正在Android中写一个webcrawler
。我的密码是
public void parseHttp() {
AsyncHttpClient client = new AsyncHttpClient();
String url = "";
client.get(url, new AsyncHttpResponseHandler(Looper.getMainLooper()) {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
String body = new String(responseBody);
System.out.println(body);
Pattern p = Pattern.compile("<h1(.*)<\/h1>");
Matcher m = p.matcher(body);
Log.d("tag", "success");
if ( m.find() ) {
String match = m.group(1);
Log.d("tag", match);
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Log.d("tag", "failure");
}
});
}
它正在字符串中找到 h1
标记,该字符串是使用 regex
的网络文档的响应。我可以像通常那样使用 Jsoup
库找到 tag
as
try {
Document doc;
URL = requestString;
doc = Jsoup.connect(URL).timeout(20 * 1000).userAgent("Chrome").get();
Elements links = doc.select("h1");
responseMessage = links.text();
} catch (IOException e) {
responseMessage = e.getMessage();
}
我可以使用 AsynsHTTPResponceHandler
class 找到 Jsoup
中的标签吗?因为第 4 行是 Elements links = doc.select("h1"); responseMessage = links.text();
任何帮助或指导将不胜感激。
Jsoup 允许从字符串解析文档,而不是直接通过 HTTP(S) 加载它。
Document doc = Jsoup.parseBodyFragment(body);
我正在Android中写一个webcrawler
。我的密码是
public void parseHttp() {
AsyncHttpClient client = new AsyncHttpClient();
String url = "";
client.get(url, new AsyncHttpResponseHandler(Looper.getMainLooper()) {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
String body = new String(responseBody);
System.out.println(body);
Pattern p = Pattern.compile("<h1(.*)<\/h1>");
Matcher m = p.matcher(body);
Log.d("tag", "success");
if ( m.find() ) {
String match = m.group(1);
Log.d("tag", match);
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Log.d("tag", "failure");
}
});
}
它正在字符串中找到 h1
标记,该字符串是使用 regex
的网络文档的响应。我可以像通常那样使用 Jsoup
库找到 tag
as
try {
Document doc;
URL = requestString;
doc = Jsoup.connect(URL).timeout(20 * 1000).userAgent("Chrome").get();
Elements links = doc.select("h1");
responseMessage = links.text();
} catch (IOException e) {
responseMessage = e.getMessage();
}
我可以使用 AsynsHTTPResponceHandler
class 找到 Jsoup
中的标签吗?因为第 4 行是 Elements links = doc.select("h1"); responseMessage = links.text();
任何帮助或指导将不胜感激。
Jsoup 允许从字符串解析文档,而不是直接通过 HTTP(S) 加载它。
Document doc = Jsoup.parseBodyFragment(body);