英文:
Can't sort Google numbers with JSoup
问题
在Android Studio中,我通过JSoup提取不同的谷歌编号结果,然后尝试使用Collection.sort进行排序。
排序是有效的,但由于谷歌结果中带有逗号(对于亚洲版谷歌)和空格(对于西方版谷歌),排序效果不好。
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
public class Concept1Activity extends AppCompatActivity {
Button button;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_concept1);
textView = findViewById(R.id.textView);
button = findViewById(R.id.btnParseHTML);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getHtmlFromWeb();
}
});
}
private void getHtmlFromWeb() {
new Thread(new Runnable() {
@Override
public void run() {
final StringBuilder stringBuilder = new StringBuilder();
try {
Document docCornes = Jsoup.connect("https://www.google.fr/search?q=" + "cornes").get();
Document docVerra = Jsoup.connect("https://www.google.fr/search?q=" + "verra").get();
Document docPommes = Jsoup.connect("https://www.google.fr/search?q=" + "pommes").get();
Document docDiable = Jsoup.connect("https://www.google.fr/search?q=" + "diable").get();
Document docEponge = Jsoup.connect("https://www.google.fr/search?q=" + "eponge").get();
Elements nbrIdiom = new Elements();
nbrIdiom.add(docDiable);
nbrIdiom.add(docEponge);
nbrIdiom.add(docPommes);
nbrIdiom.add(docCornes);
nbrIdiom.add(docVerra);
Collections.sort(nbrIdiom, new Comparator<Element>() {
@Override
public int compare(Element e1, Element e2) {
return e1.getElementById("result-stats").ownText().compareTo(e2.getElementById("result-stats").ownText());
}
});
for (Element results : nbrIdiom) {
stringBuilder
.append("\n")
.append(results.getElementsByClass("gLFyf gsfi").attr("value"))
.append("\n")
.append(results.getElementById("result-stats").ownText())
.append("\n");
}
} catch (IOException e) {
stringBuilder
.append("Error : ")
.append(e.getMessage())
.append("\n");
}
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(stringBuilder.toString());
}
});
}
}).start();
}
}
我得到的结果如下,您可以看到虽然已经排序,但由于逗号或空格的影响,排序并不正确:
- About 1 500 000 results
- About 19 700 000 results
- About 3 960 000 results
- About 685 000 results
- About 773 000 results
欢迎提供任何想法 ^-^
英文:
In Android Studio, I am extracting different Google numbered results through JSoup which I am trying to sort with Collection.sort.
Sorting is working but since the Google results comes with comma (with Asian Google) and space (with Western Google), the sorting is not well made.
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
public class Concept1Activity extends AppCompatActivity {
Button button;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_concept1);
textView = findViewById(R.id.textView);
button = findViewById(R.id.btnParseHTML);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getHtmlFromWeb();
}
});
}
private void getHtmlFromWeb() {
new Thread(new Runnable() {
@Override
public void run() {
final StringBuilder stringBuilder = new StringBuilder();
try {
Document docCornes = Jsoup.connect("https://www.google.fr/search?q=" + "cornes").get();
Document docVerra = Jsoup.connect("https://www.google.fr/search?q=" + "verra").get();
Document docPommes = Jsoup.connect("https://www.google.fr/search?q=" + "pommes").get();
Document docDiable = Jsoup.connect("https://www.google.fr/search?q=" + "diable").get();
Document docEponge = Jsoup.connect("https://www.google.fr/search?q=" + "eponge").get();
Elements nbrIdiom = new Elements();
nbrIdiom.add(docDiable); nbrIdiom.add(docEponge); nbrIdiom.add(docPommes); nbrIdiom.add(docCornes); nbrIdiom.add(docVerra);
Collections.sort(nbrIdiom, new Comparator<Element>() {
@Override
public int compare(Element e1, Element e2) {
return e1.getElementById("result-stats").ownText().compareTo(e2.getElementById("result-stats").ownText());
}
});
for (Element results : nbrIdiom) {
stringBuilder
.append("\n")
.append(results.getElementsByClass("gLFyf gsfi").attr("value"))
.append("\n")
.append(results.getElementById("result-stats").ownText())
.append("\n");
}
} catch (IOException e) {
stringBuilder
.append("Error : ")
.append(e.getMessage())
.append("\n");
}
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(stringBuilder.toString());
}
});
}
}).start();
}
}
I get a result like below and you could see that it is sorted but because of the comma or the space, it is not sorted properly :
- About 1 500 000 results
- About 19 700 000 results
- About 3 960 000 results
- About 685 000 results
- About 773 000 results
Any idea would be more than welcome ^-^
专注分享java语言的经验与见解,让所有开发者获益!
评论