我一直在研究用于数据提取的Jsoup示例,并提取了此链接的一个示例
J汤
package com.androidbegin.jsouptutorial;
import java.io.IOException;
import java.io.InputStream;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
// URL Address
String url = "http://www.androidbegin.com";
ProgressDialog mProgressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Locate the Buttons in activity_main.xml
Button titlebutton = (Button) findViewById(R.id.titlebutton);
Button descbutton = (Button) findViewById(R.id.descbutton);
Button logobutton = (Button) findViewById(R.id.logobutton);
// Capture button click
titlebutton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Execute Title AsyncTask
new Title().execute();
}
});
// Capture button click
descbutton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Execute Description AsyncTask
new Description().execute();
}
});
// Capture button click
logobutton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Execute Logo AsyncTask
new Logo().execute();
}
});
}
// Title AsyncTask
private class Title extends AsyncTask<Void, Void, Void> {
String title;
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setTitle("Android Basic JSoup Tutorial");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
// Connect to the web site
Document document = Jsoup.connect(url).get();
// Get the html document title
title = document.title();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Set title into TextView
TextView txttitle = (TextView) findViewById(R.id.titletxt);
txttitle.setText(title);
mProgressDialog.dismiss();
}
}
// Description AsyncTask
private class Description extends AsyncTask<Void, Void, Void> {
String desc;
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setTitle("Android Basic JSoup Tutorial");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
// Connect to the web site
Document document = Jsoup.connect(url).get();
// Using Elements to get the Meta data
Elements description = document
.select("meta[name=description]");
// Locate the content attribute
desc = description.attr("content");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Set description into TextView
TextView txtdesc = (TextView) findViewById(R.id.desctxt);
txtdesc.setText(desc);
mProgressDialog.dismiss();
}
}
// Logo AsyncTask
private class Logo extends AsyncTask<Void, Void, Void> {
Bitmap bitmap;
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setTitle("Android Basic JSoup Tutorial");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
// Connect to the web site
Document document = Jsoup.connect(url).get();
// Using Elements to get the class data
Elements img = document.select("a[class=brand brand-image] img[src]");
// Locate the src attribute
String imgSrc = img.attr("src");
// Download image from URL
InputStream input = new java.net.URL(imgSrc).openStream();
// Decode Bitmap
bitmap = BitmapFactory.decodeStream(input);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Set downloaded image into ImageView
ImageView logoimg = (ImageView) findViewById(R.id.logo);
logoimg.setImageBitmap(bitmap);
mProgressDialog.dismiss();
}
}
}
<div class="postWrapper" id="post162">
<div class="postTitle">
<h2> Title to post </h2>
<div class="fb-custom-share" data-url="http://url...">
</div>
<div class="date"> 26 de janeiro de 2015 </div>
</div>
<div class="postContent">
content
</div>
我没有真正理解您正在尝试从上面HTML内容中提取什么。因此,我将在下面列出几个例子来帮助您。
private static void test() {
Document doc = null;
Elements dateDivs = null;
Elements postContentDivs = null;
Elements fbCustomShareDivs = null;
Element specificIdDiv = null;
try {
doc = Jsoup.connect(url).get();
/** First part of example, I assume all elements are unique in term of classname, which means for eg there is only one DIV element with date as classname **/
/** Find all SPAN element with matched CLASS name **/
dateDivs = doc.select("div.date");
if (dateDivs.size() > 0) {
String date = dateDivs.get(0).text();
System.out.println("date: " + date);
}
else {
System.out.println("No DIV element found with class date.");
}
/** Find all SPAN element with matched CLASS name **/
postContentDivs = doc.select("div.postContent");
if (postContentDivs.size() > 0) {
String postContent = postContentDivs.get(0).text();
System.out.println("postContent: " + postContent);
}
else {
System.out.println("No DIV element found with postContentDivs date.");
}
/** Find all SPAN element with matched CLASS name **/
fbCustomShareDivs = doc.select("div.fb-custom-share");
if (fbCustomShareDivs.size() > 0) {
String dataurl = fbCustomShareDivs.get(0).attr("data-url");
System.out.println("dataurl: " + dataurl);
}
else {
System.out.println("No DIV element found with fb-custom-share date.");
}
/** Second part of example **/
/** If elements are not unique in term of class name, then use following code to retrieve DIV with ID first. **/
/** Then only continue to retrieve elements inside DIV id post162 **/
specificIdDiv = doc.getElementById("post162");
if (specificIdDiv != null) {
postContentDivs = specificIdDiv.select("div.postContent");
if (postContentDivs.size() > 0) {
String postContent = postContentDivs.get(0).text();
System.out.println("postContent: " + postContent);
}
else {
System.out.println("No DIV element found with postContentDivs date.");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
我有一个带有ID、TEXT等列的表,这里的TEXT是超文本标记语言FORMAT中包含数据的Clob列 样本数据: 当我使用Jsoup.parse(AUDIT_SCOPE_LOB.text()时;我得到的数据如下 我对java知之甚少。我可以使用jsoup获取java代码来提取数据并重新运行下面的outpu吗 实际上,这个数据是一个样本数据。我有一些带有html标记的数据,这里没有提到。
我已经使用HTTPClient连接到一个网站,我可以成功地访问所需的数据从网站使用jSoup.我有以下代码,我需要从中提取提交按钮信息。 如何访问提交按钮的值和名称?
有多个包含美国专利No.9,000,000的转让数据的div元素出现在行下面 有办法用JSOUP提取这个隐藏的html吗?
问题内容: 我想使用JSoup-framework提取此表,以将内容保存在“表”数组中。第一个tr-tag是表头。所有以下内容(不包括在内)均描述了内容。 我已经测试了这一个和其他一些,但是我没有让它们为我工作: 使用JSoup提取HTML表内容 问题答案: 这是一些示例代码,您如何仅选择标题: 你得到… 解析 文件 :(这里是和字符集,请参阅jsoup对铁道部的相关信息文件) 解析 网站 :(不
当我试图从在线URL=forexalgerie.com中的表中获取数据时,我的目标是这些值: ...似乎我的代码一切正常: 但是结果包含表中的所有内容,除了我想要的值? 怎么了?
我需要从如下节点中提取文本: 我需要建立: 仅返回div的所有内容。-不在子元素内的所有内容。两者都错了。迭代子节点会忽略文本节点。 文本节点-某些文本