无法使用JSoup对Google数字进行排序。

huangapple 未分类评论52阅读模式
英文:

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(&quot;https://www.google.fr/search?q=&quot; + &quot;cornes&quot;).get();
                Document docVerra = Jsoup.connect(&quot;https://www.google.fr/search?q=&quot; + &quot;verra&quot;).get();
                Document docPommes = Jsoup.connect(&quot;https://www.google.fr/search?q=&quot; + &quot;pommes&quot;).get();
                Document docDiable = Jsoup.connect(&quot;https://www.google.fr/search?q=&quot; + &quot;diable&quot;).get();
                Document docEponge = Jsoup.connect(&quot;https://www.google.fr/search?q=&quot; + &quot;eponge&quot;).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&lt;Element&gt;() {
                    @Override
                    public int compare(Element e1, Element e2) {
                        return e1.getElementById(&quot;result-stats&quot;).ownText().compareTo(e2.getElementById(&quot;result-stats&quot;).ownText());
                    }
                });

               
                for (Element results : nbrIdiom) {
                    stringBuilder
                            .append(&quot;\n&quot;)
                            .append(results.getElementsByClass(&quot;gLFyf gsfi&quot;).attr(&quot;value&quot;))
                            .append(&quot;\n&quot;)
                            .append(results.getElementById(&quot;result-stats&quot;).ownText())
                            .append(&quot;\n&quot;);
                }

            } catch (IOException e) {
                stringBuilder
                        .append(&quot;Error : &quot;)
                        .append(e.getMessage())
                        .append(&quot;\n&quot;);
            }
            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 ^-^

huangapple
  • 本文由 发表于 2020年4月7日 18:07:08
  • 转载请务必保留本文链接:https://java.coder-hub.com/61077524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定