从互联网获取数据

Obtain data from internet

我正在尝试从以下页面获取数据:Page What I'm trying to do is that if the user typed something in the EditText and pressed the button, a RecyclerView with an Array of everything that search brings up is loaded. The downside is that I can't parse the page data well. When searching, for example, the words just leveling the url becomes this: Page。如您所见,我无法调出标题或图片:

这是搜索时页面body的代码(我简化了):

<body>
    <div id="app" class="pb-5">
     <main role="main" class="container-fluid">
    <div class="row">
    <div class="col-12 col-lg-8 col-xl-9">
    <div class="row">
    <div class="element  col-6 col-sm-6 col-md-4 col-lg-3 col-xl-2 mt-2 " data-identifier="41512">
    <a href=" https://lectortmo.com/library/manhwa/41512/solo-leveling"> <div class="thumbnail book book-thumbnail-41512">
    <style>
                    .book-thumbnail-41512::before{
                        background-image: url('https://otakuteca.com/images/books/cover/5c2efcd42cd5e.jpg');
                    }
                </style>
    <div class="thumbnail-title">
    <h4 class="text-truncate" title="Solo Leveling">Solo Leveling</h4>
    </div>
    </div>
    </a> </div> <div class="element  col-6 col-sm-6 col-md-4 col-lg-3 col-xl-2 mt-2 " data-identifier="48448">
    <a href=" https://lectortmo.com/library/manhwa/48448/love-leveling"> <div class="thumbnail book book-thumbnail-48448">
    <style>
                    .book-thumbnail-48448::before{
                        background-image: url('https://otakuteca.com/images/books/cover/5ed182d1c909d.jpg');
                    }
                </style>
    <div class="thumbnail-title">
    <h4 class="text-truncate" title="Love Leveling ">Love Leveling </h4>
    </div>
    </div>
    </a> </div> </div>
    </div>
    </div>
    </main>
    </div>
    </body>

这就是我解析数据的方式:

@Override
            protected ArrayList<ParseItem> doInBackground(Void... voids) {
                String texto = textoBusqueda.getText().toString();
                String arreglo = texto.replace(' ', '+');
                String url = "https://lectortmo.com/library?_page=1&title=" + arreglo;
                try {
                    Document doc = Jsoup.connect(url).get();
    
                    Elements data = doc.select("div.row");
                    int size = data.size();
                    Log.d("doc", "doc: "+doc);
                    Log.d("data", "data: "+data);
                    Log.d("size", ""+size);
                    for (int i = 0; i < size; i++) {
                        String title = data.select("div.thumbnail-title")
                                .select("h4")
                                .eq(i)
                                .attr("tittle"); //nombre del manhwa
                        String imgUrl = data.select("style")
                                .select("background-image")
                                .eq(i)
                                .attr("url"); //imagen del manga
                        String detailUrl = data.select("div.row")
                                .select("a")
                                .eq(i)
                                .attr("href");
                        String urlManga = data.select("div.row")
                                .select("a")
                                .eq(i)
                                .attr("href");
    
                        parseItems.add(new ParseItem(imgUrl, title, detailUrl, urlManga));
                    }
                }  catch (IOException e) {
                    e.printStackTrace();
                }
                return parseItems;
            }

有人能告诉我如何正确地做到这一点吗?谢谢

  • 您的第一个 select returns 7 个元素,但应该只有两个,所以让我们从调整选择器开始,使用:
    Elements data = doc.select("div.row>.element");
    现在 size2.
  • 另一件事是您不必使用:for (int i = 0; i < size; i++) 但让我们使用 for (Element e : data) 这样您就不必再使用 .eq(i),只需使用 e.
  • 你打错了,应该是"title"而不是"tittle"
  • 现在您可以使用更简单的选择器获取详细信息 URL:String detailUrl = e.select("a").attr("href");
  • 我不明白你想得到什么 urlManga 因为他们每个人只有一个 link.
  • 获取 imgUrl 很困难,因为 url 在 <style> 中,而 Jsoup 无法解析其中的内容,因此您必须使用正则表达式来获取 URL 或者只是做一些有创意的字符串拆分。

在所有建议之后,您的代码应如下所示:

Document doc = Jsoup.connect(url).get();

Elements data = doc.select("div.row>.element");
int size = data.size();
Log.d("doc", "doc: "+doc);
Log.d("data", "data: "+data);
Log.d("size", ""+size);
for (Element e : data) {
    String title = e.select("h4").attr("title"); // nombre del manhwa
    String imgUrl = e.select("style").first().html().split("url\('")[1].split("'\)")[0]; // imagen del manga
    String detailUrl = e.select("a").attr("href").trim();
    String urlManga = "???";

    parseItems.add(new ParseItem(imgUrl, title, detailUrl, urlManga));
}