使用Jsoup解析HTML页面
2016-06-29 19:43
477 查看
Jsoup是通过解析网站网页,结合标签,进而获取想要的内容。
通过继承Asynctask,通过Jsoup解析标签,将标签内容显示出来。
用法很简单。
<span style="font-size:18px;">class LoadHtml extends AsyncTask<String, String, String> {
Document doc;
URLImageParser imageParser = new URLImageParser();
@Override
protected String doInBackground(String... params) { //后台执行的方法
try {
doc = Jsoup.connect(params[0]).get();
Document content = Jsoup.parse(doc.toString());//返回一个Document对象,像JQuery那样解析
Element elementContent = content.getElementById("vsb_content");
if (elementContent == null) {
elementContent = content.getElementById("vsb_content_2");
}
String temp = elementContent.html();
author = content.getElementsByClass("authorstyle44518").text();
time = content.getElementsByClass("timestyle44518").text();
desc = android.text.Html.fromHtml(temp, imageParser, null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {//<span style="color: rgb(51, 51, 51); font-family: arial, 'courier new', courier, 宋体, monospace, 'Microsoft YaHei'; font-size: 14px; line-height: 24px; white-space: pre-wrap;">onPostExecute方法是在doInBackground方法执行之后执行</span>
// TODO Auto-generated method stub
super.onPostExecute(result);
loadContentBar.setVisibility(View.GONE);
noticeAuthor.setText(author);//设置作者
noticeTime.setText(time);//设置时间
noticeContent.setText(desc);//设置描述内容
noticeContent.setMovementMethod(LinkMovementMethod.getInstance());
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
loadContentBar.setVisibility(View.VISIBLE);
}
}</span>
通过继承Asynctask,通过Jsoup解析标签,将标签内容显示出来。
用法很简单。
<span style="font-size:18px;">class LoadHtml extends AsyncTask<String, String, String> {
Document doc;
URLImageParser imageParser = new URLImageParser();
@Override
protected String doInBackground(String... params) { //后台执行的方法
try {
doc = Jsoup.connect(params[0]).get();
Document content = Jsoup.parse(doc.toString());//返回一个Document对象,像JQuery那样解析
Element elementContent = content.getElementById("vsb_content");
if (elementContent == null) {
elementContent = content.getElementById("vsb_content_2");
}
String temp = elementContent.html();
author = content.getElementsByClass("authorstyle44518").text();
time = content.getElementsByClass("timestyle44518").text();
desc = android.text.Html.fromHtml(temp, imageParser, null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {//<span style="color: rgb(51, 51, 51); font-family: arial, 'courier new', courier, 宋体, monospace, 'Microsoft YaHei'; font-size: 14px; line-height: 24px; white-space: pre-wrap;">onPostExecute方法是在doInBackground方法执行之后执行</span>
// TODO Auto-generated method stub
super.onPostExecute(result);
loadContentBar.setVisibility(View.GONE);
noticeAuthor.setText(author);//设置作者
noticeTime.setText(time);//设置时间
noticeContent.setText(desc);//设置描述内容
noticeContent.setMovementMethod(LinkMovementMethod.getInstance());
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
loadContentBar.setVisibility(View.VISIBLE);
}
}</span>
相关文章推荐
- W3C api 抓取
- Android开发之利用jsoup解析HTML页面的方法
- Android使用Jsoup解析Html表格的方法
- Jsoup解析HTML实例及文档方法详解
- Java中使用开源库JSoup解析HTML文件实例
- crawler4j抓取页面使用jsoup解析html时的解决方法
- Jsoup获取全国地区数据属性值(省市县镇村)
- java使用Jsoup连接网站超时的解决方法
- Java实现爬虫给App提供数据(Jsoup 网络爬虫)
- 简单网路爬虫(JSoup) + SSH + Mysql保存...
- 如何用Java监控XX挂号平台X科的可用号
- 使用HttpClient和jsoup获取并解析html
- 多线程爬虫遇到的一些问题
- jsoup的Node类
- JSOUP简单应用
- 如何使用Java中HttpClient解析Html中的table
- HttpClient+jsoup登录+解析 163邮箱
- 百科描述jsoup
- Jsoup抓取页面内容
- Jsoup处理html空格乱码问题