我只想在“article”元素中获取内容。我在下面的代码中使用了Jsoup。声明“eles = doc.select(”article“);”不归还任何东西。但是,当我用“title”/“head”/“body”替换“article”时,它可以正常工作。
这里发生了什么?抱歉,因为我的英语不好!
编辑:我已经上传了我的完整代码。请给我一个成功的例子!main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="XXXX" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<WebView
android:id="@+id/wView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</ScrollView>
</LinearLayout>
MainActivity.java
package tvt.jsouptest;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.TextView;
public class MainActivity extends Activity {
WebView v;
TextView tv;
String tt = "1", er = "2";
String urlw;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
v = (WebView) findViewById(R.id.wView);
tv = (TextView) findViewById(R.id.tView);
urlw = "http://economictimes.indiatimes.com/opinion/interviews/india-is-not-sailing-in-the-same-boat-as-turkey-or-indonesia-raghuram-rajan/articleshow/27797904.cms";
new NewTasks().execute(urlw);
}
class NewTasks extends AsyncTask<String, Integer, Document> {
Element eles;
@Override
protected Document doInBackground(String... params) {
Document doc = null;
try {
doc = Jsoup.connect(params[0]).get();
tt = doc.title();
eles = doc.select("div.artText").first();
} catch (Exception e) {
doc = null;
er = e.toString();
}
return doc;
}
public void onPostExecute(Document result) {
if (result != null) {
try {
((TextView) findViewById(R.id.tView))
.setText("Title: "
+ tt
+ "\nStart........................................\n"
+ eles.toString()
+ "\nEnd..........................................");
} catch (Exception e) {
((TextView) findViewById(R.id.tView))
.setText("Error-TRUE: " + e.toString());
}
} else {
((TextView) findViewById(R.id.tView)).setText("Error-FALSE: "
+ er);
}
}
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tvt.jsouptest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="tvt.jsouptest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:1)
您在select()
方法中提供的参数(例如“文章”)指定了标记名。 <title>
,<head>
和<body>
都是有效的HTML代码,会选择一些元素。另一方面,<article>
不是有效的HTML标记,因此不会选择任何内容。阅读here。
如果您查看该页面的来源,您会发现文章文本存在于具有类的div中,特别是<div class="artText"></div>
。所以选择:
Element eles = doc.select("div.artText").first();
如果您只想要该元素中的文本(删除所有HTML标记),只需在将其设置为TextView的内容时使用eles.text()
。