[英]HTML extraction using Jsoup
I've been studying the example of Jsoup for data extraction and extracts an example of this link 我一直在研究用于数据提取的Jsoup示例,并提取了此链接的示例
But I tried to manipulate the sample to extract data from a Div instead of a Meta attribute and could not. 但是我试图操纵该样本以从Div而不是Meta属性提取数据,但是不能。
I want to load the posts that people publish on a particular blog and loads them in the App page. 我想加载人们在特定博客上发布的帖子,并将其加载到“应用程序”页面中。
Can anyone help me to modify this code to get the DIV data. 谁能帮助我修改此代码以获取DIV数据。
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();
}
}
}
This is the site structure: 这是网站结构:
<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>
I don't really get what you are trying extract from HTML content above. 我并没有真正从上面的HTML内容中提取您想要的内容。 Hence I will list a few examples below to help you.
因此,我将在下面列出一些示例以帮助您。
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.