使用 JSoup 从 div 中获取第二个单词
Get the second word from the div using JSoup
我的代码我得到了div中的所有单词。我需要做什么才能只获取 div 中的第二个单词?
例如:
<div id="div01"> FIRSTWORD SECONDWORD </DIV>
我的代码:
Document doc = Jsoup.connect(url).get();
Elements statistics = doc.select(div.div01);
textDiv1 = statistics.text();
为什么不像 Java 字符串中的第二个单词那样呢?
String secondWord = line.split(" ")[1]; //add your own checks to avoid out of bounds or null exceptions.
扩展现有内容的示例:
if (textDiv1 != null) {
String[] words = textDiv1.split(" "); //split our text into individual words, which are separated by spaces
if (words.length >= 2) { //there are at least two words
String secondWord = words[1];
}
//otherwise, there is no second word, so you can handle it here
}
//there is no text found as textDiv1 is null
** 除非你的第二个词恰好在它自己的元素中,否则这不是 JSOUP 会区分的东西 **
@AllanW 最后一个代码是示例代码..这是我的真实代码:
private class Div1 extends AsyncTask<String, String, String> {
String text = null;
String text2 = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
try {
// Connect to the web site
Document doc = Jsoup.connect(url).get();
// Using Elements to get the Meta data
Elements statistics = doc.select("div.statistics__userInfo > div");
if (text !=null){
String[] words = text.split("");
if (words.length >= 2) {
String secondWord = words[1];
}
}
Elements span1 = doc.getElementsByTag("span");
doc.getElementsByTag("span").remove();
// Locate the content attribute
text = statistics.text();
text2 = span1.text();
} catch (IOException e) {
e.printStackTrace();
}
return text;
}
我的代码我得到了div中的所有单词。我需要做什么才能只获取 div 中的第二个单词?
例如:
<div id="div01"> FIRSTWORD SECONDWORD </DIV>
我的代码:
Document doc = Jsoup.connect(url).get();
Elements statistics = doc.select(div.div01);
textDiv1 = statistics.text();
为什么不像 Java 字符串中的第二个单词那样呢?
String secondWord = line.split(" ")[1]; //add your own checks to avoid out of bounds or null exceptions.
扩展现有内容的示例:
if (textDiv1 != null) {
String[] words = textDiv1.split(" "); //split our text into individual words, which are separated by spaces
if (words.length >= 2) { //there are at least two words
String secondWord = words[1];
}
//otherwise, there is no second word, so you can handle it here
}
//there is no text found as textDiv1 is null
** 除非你的第二个词恰好在它自己的元素中,否则这不是 JSOUP 会区分的东西 **
@AllanW 最后一个代码是示例代码..这是我的真实代码:
private class Div1 extends AsyncTask<String, String, String> {
String text = null;
String text2 = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
try {
// Connect to the web site
Document doc = Jsoup.connect(url).get();
// Using Elements to get the Meta data
Elements statistics = doc.select("div.statistics__userInfo > div");
if (text !=null){
String[] words = text.split("");
if (words.length >= 2) {
String secondWord = words[1];
}
}
Elements span1 = doc.getElementsByTag("span");
doc.getElementsByTag("span").remove();
// Locate the content attribute
text = statistics.text();
text2 = span1.text();
} catch (IOException e) {
e.printStackTrace();
}
return text;
}