当前位置: 首页 > 知识库问答 >
问题:

使用jsoup从标记中获取数据

澹台昆
2023-03-14

所以我尝试从pretag获取数据,我设置doc连接到url选择pretag,结果出错了,我需要获取的数据按这里

String url="http://api.airvisual.com/v2/countries?key=9c2dd8c2-1053-43fa-9357-6d3aa876aabc";
Document doc=Jsoup.connect(url).get();
  for(Element a: doc.select("pre"))
  {
  System.out.println(a.text());
  }

共有1个答案

闾丘德宇
2023-03-14

这对我很有用:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;

class AirVisualJsonData {
    
    public static void main(String[] args) throws IOException {
        String url = "http://api.airvisual.com/v2/countries?key=9c2dd8c2-1053-43fa-9357-6d3aa876aabc";
        Document document = Jsoup.connect(url).ignoreContentType(true).get();
        for (Element node : document.select("body")) {
            System.out.println(node.text());
        }
    }
}

但是您希望能够使用Json解析器解析您返回的json。有很多选择。格森、杰克逊、杰威等。你必须选择适合你的。

输出:

{
  "status": "success",
  "data": [
    {
      "country": "Afghanistan"
    },
    {
      "country": "Algeria"
    },
    {
      "country": "Andorra"
    },
    {
      "country": "Angola"
    },
    {
      "country": "Argentina"
    },
    {
      "country": "Armenia"
    },
    {
      "country": "Australia"
    },
    {
      "country": "Austria"
    },
    {
      "country": "Bahamas"
    },
    {
      "country": "Bahrain"
    },
    {
      "country": "Bangladesh"
    },
    {
      "country": "Belgium"
    },
    {
      "country": "Bosnia Herzegovina"
    },
    {
      "country": "Brazil"
    },
    {
      "country": "Brunei"
    },
    {
      "country": "Bulgaria"
    },
    {
      "country": "Canada"
    },
    {
      "country": "Chile"
    },
    {
      "country": "China"
    },
    {
      "country": "Colombia"
    },
    {
      "country": "Croatia"
    },
    {
      "country": "Cyprus"
    },
    {
      "country": "Czech Republic"
    },
    {
      "country": "Denmark"
    },
    {
      "country": "Ecuador"
    },
    {
      "country": "Ethiopia"
    },
    {
      "country": "Finland"
    },
    {
      "country": "France"
    },
    {
      "country": "Germany"
    },
    {
      "country": "Ghana"
    },
    {
      "country": "Guatemala"
    },
    {
      "country": "Hong Kong SAR"
    },
    {
      "country": "Hungary"
    },
    {
      "country": "India"
    },
    {
      "country": "Indonesia"
    },
    {
      "country": "Iran"
    },
    {
      "country": "Iraq"
    },
    {
      "country": "Ireland"
    },
    {
      "country": "Israel"
    },
    {
      "country": "Italy"
    },
    {
      "country": "Ivory Coast"
    },
    {
      "country": "Japan"
    },
    {
      "country": "Jordan"
    },
    {
      "country": "Kazakhstan"
    },
    {
      "country": "Kosovo"
    },
    {
      "country": "Kuwait"
    },
    {
      "country": "Kyrgyzstan"
    },
    {
      "country": "Latvia"
    },
    {
      "country": "Lithuania"
    },
    {
      "country": "Luxembourg"
    },
    {
      "country": "Macao SAR"
    },
    {
      "country": "Malaysia"
    },
    {
      "country": "Malta"
    },
    {
      "country": "Mexico"
    },
    {
      "country": "Mongolia"
    },
    {
      "country": "Myanmar"
    },
    {
      "country": "Nepal"
    },
    {
      "country": "Netherlands"
    },
    {
      "country": "New Caledonia"
    },
    {
      "country": "New Zealand"
    },
    {
      "country": "Nigeria"
    },
    {
      "country": "North Macedonia"
    },
    {
      "country": "Norway"
    },
    {
      "country": "Oman"
    },
    {
      "country": "Pakistan"
    },
    {
      "country": "Peru"
    },
    {
      "country": "Philippines"
    },
    {
      "country": "Poland"
    },
    {
      "country": "Portugal"
    },
    {
      "country": "Puerto Rico"
    },
    {
      "country": "Romania"
    },
    {
      "country": "Russia"
    },
    {
      "country": "San Marino"
    },
    {
      "country": "Serbia"
    },
    {
      "country": "Singapore"
    },
    {
      "country": "Slovakia"
    },
    {
      "country": "Slovenia"
    },
    {
      "country": "South Africa"
    },
    {
      "country": "South Korea"
    },
    {
      "country": "Spain"
    },
    {
      "country": "Sri Lanka"
    },
    {
      "country": "Sweden"
    },
    {
      "country": "Switzerland"
    },
    {
      "country": "Syria"
    },
    {
      "country": "Taiwan"
    },
    {
      "country": "Thailand"
    },
    {
      "country": "Turkey"
    },
    {
      "country": "U.S. Virgin Islands"
    },
    {
      "country": "USA"
    },
    {
      "country": "Uganda"
    },
    {
      "country": "Ukraine"
    },
    {
      "country": "United Arab Emirates"
    },
    {
      "country": "United Kingdom"
    },
    {
      "country": "Uzbekistan"
    },
    {
      "country": "Vietnam"
    },
    {
      "country": "Yemen"
    }
  ]
}
Process finished with exit code 0
 类似资料:
  • 我想从http://www.futbol24.com/Live/?__igp=1 我想要时间,主队和客队的每一排的tbody表。因此,第一行的输出应该是: 我可以将这些元素的td类分别视为“status alt”、“home”和“guest”。 目前我已经尝试了下面的,但似乎没有输出任何东西。。。我做错了什么? 有人知道如何使用jSoup从表的每一行获取这些元素吗? 谢谢, 抢劫

  • 我想使用jsoup从网页中提取内容。这些值在内部标签中,如何提取这些值? 例如 我想提取锚点标签中的内容首页*将如何做到这一点?

  • 问题内容: 当我使用jsoup提取数据时遇到一个职位。数据如下: 我想要这样的数据: 我怎样才能做到这一点?谁能帮我? 问题答案: 您可以将html解析为,选择-Element并获取其文本。 例: 输出:

  • 我正在从事一个个人项目,希望解析这个html并从中检索信息。 基本上,我希望获得 标记中给出的所有信息,为此,我在java中使用JSOUP。 我使用这段代码来获取,但这是在一个段落中给出所有值。 我也试过了 但他的观点是空泛的。 有人能帮我以更好的方式获得这些数据吗?

  • 我有这个html 并且,我试图得到每个标签的href。 例如,

  • 我想从URL示例中获取一个图像博客头像:https://soundcloud.com/topsify 我试图得到: 但它还是空的。请支持获取头像url:https://i1.sndcdn.com/avatars-000132054558-5ra8gl-t500x500.jpg谢谢