gpt4 book ai didi

java - 使用 Jsoup 提取 HTML

转载 作者:行者123 更新时间:2023-12-01 11:57:56 34 4
gpt4 key购买 nike

我一直在研究Jsoup用于数据提取的示例,并提取了此链接的示例

Jsoup

但我尝试操作示例以从 Div 而不是 Meta 属性中提取数据,但失败了。

我想加载人们在特定博客上发布的帖子并将其加载到应用程序页面中。

谁能帮我修改这段代码来获取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();
}
}
}

这是网站结构:

<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();
}
}

关于java - 使用 Jsoup 提取 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28282904/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com