提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔
目錄
文章目錄
前言
一、Jsoup是什么?
二、使用步驟
1.引入庫
2.讀入數(shù)據(jù)
總結(jié)
前言
vx開發(fā)小程序使用掃一掃時不同二維碼展示的東西不一樣,需要進(jìn)行解析
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、Jsoup是什么?
Jsoup是一款用于解析和操作HTML文檔的Java庫。它提供了一組簡單且強(qiáng)大的API,使得在Java中處理HTML文檔變得非常容易。
使用Jsoup,您可以執(zhí)行以下操作:
- 解析HTML文檔:使用
Jsoup.parse()
方法可以將HTML文檔解析成一個Document
對象,方便后續(xù)的操作。 -
String html = "<html><body><h1>Hello, Jsoup!</h1></body></html>"; Document doc = Jsoup.parse(html);
- 選擇器操作:Jsoup支持類似于CSS選擇器的語法,可以通過選擇器來選取具體的HTML元素。
-
Elements elements = doc.select("h1"); // 選擇所有<h1>元素 Element element = doc.selectFirst("h1"); // 選擇第一個<h1>元素
- 獲取元素內(nèi)容:可以通過
Element
對象獲取元素的文本內(nèi)容、屬性值等。 -
String text = element.text(); // 獲取元素的文本內(nèi)容 String attrValue = element.attr("src"); // 獲取元素的屬性值
- 遍歷元素:可以使用循環(huán)遍歷
Elements
對象中的多個元素。 -
for (Element element : elements) { // 處理每個元素 }
- 修改元素:可以通過
Element
對象修改元素的文本內(nèi)容、屬性值等。 -
element.text("New Text"); // 修改元素的文本內(nèi)容 element.attr("src", "new_image.jpg"); // 修改元素的屬性值
以上只是Jsoup的一些基本用法示例,Jsoup還提供了更多功能,如處理表單、處理URL、處理CSS樣式等。您可以參考Jsoup的官方文檔或其他教程來學(xué)習(xí)更多關(guān)于Jsoup的用法和功能。
二、使用步驟
1.引入庫
代碼如下(示例):
<!--爬取頁面--> <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.14.2</version> </dependency>
2.讀入數(shù)據(jù)
代碼如下(示例):
?public static void main(String[] args) throws IOException, InterruptedException { ? ? ? ? try { ? ? ? ? ? ? URL url = new URL("http://teshexxx"); ? ? ? ? ? ? // 設(shè)置連接超時時間 ? ? ? ? ? ? URLConnection connection = url.openConnection(); ? ? ? ? ? ? connection.setConnectTimeout(5000); ? ? ? ? ? ? // 設(shè)置讀取超時時間 ? ? ? ? ? ? connection.setReadTimeout(5000); ? ? ? ? ? ? InputStream inputStream = connection.getInputStream(); ? ? ? ? ? ? BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); ? ? ? ? ? ? StringBuilder content = new StringBuilder(); ? ? ? ? ? ? String line; ? ? ? ? ? ? while ((line = reader.readLine()) != null) { ? ? ? ? ? ? ? ? content.append(line); ? ? ? ? ? ? } ? ? ? ? ? ? String data = parseData(content.toString()); ? ? ? ? ? ? String gasCode = extractGasCode(data); ? ? ? ? ? ? System.out.println("鋼瓶編碼: " + data); ? ? ? ? ? ? System.out.println("氣瓶編號: " + gasCode); ? ? ? ? } catch (SocketTimeoutException e) { ? ? ? ? ? ? System.out.println("連接超時,請檢查網(wǎng)絡(luò)連接或增加超時時間"); ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? } ? ? public static String parseData(String content) { ? ? ? ? return content; ? ? } ? ? public static String extractGasCode(String htmlContent) throws InterruptedException { ? ? ? ? int maxRetries = 3; ? ? ? ? int retryCount = 0; ? ? ? ? String gasCode = null; ? ? ? ? while (retryCount < maxRetries) { ? ? ? ? ? ? Document doc = Jsoup.parse(htmlContent); ? ? ? ? ? ? Elements spans = doc.select("span"); ? ? ? ? ? ? if (spans.isEmpty()) { ? ? ? ? ? ? ? ? Elements trs = doc.select("tr"); ? ? ? ? ? ? ? ? for (Element tr : trs) { ? ? ? ? ? ? ? ? ? ? Elements tds = tr.select("td"); ? ? ? ? ? ? ? ? ? ? for (int i = 0; i < tds.size() - 1; i++) { ? ? ? ? ? ? ? ? ? ? ? ? Element td = tds.get(i); ? ? ? ? ? ? ? ? ? ? ? ? if (td.text().equals("氣瓶編號")) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? gasCode = tds.get(i + 1).text(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? if (gasCode != null) { ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? for (Element span : spans) { ? ? ? ? ? ? ? ? ? ? if (span.text().equals("氣瓶編號")) { ? ? ? ? ? ? ? ? ? ? ? ? gasCode = span.nextElementSibling().text(); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? return gasCode; ? ? }
以上是可以直接在jvav代碼中能獲取到數(shù)據(jù)的可以使用; 下面這種則需要對url發(fā)起請求方能獲取數(shù)據(jù)?try { ? ? ? ? ? ? // 創(chuàng)建URL對象 ? ? ? ? ? ? URL url = new URL("http://mai.xxxx"); ? ? ? ? ? ? // 打開連接 ? ? ? ? ? ? HttpURLConnection conn = (HttpURLConnection) url.openConnection(); ? ? ? ? ? ? // 設(shè)置請求方法為POST ? ? ? ? ? ? conn.setRequestMethod("POST"); ? ? ? ? ? ? // 啟用輸入輸出 ? ? ? ? ? ? conn.setDoInput(true); ? ? ? ? ? ? conn.setDoOutput(true); ? ? ? ? ? ? // 設(shè)置請求頭 ? ? ? ? ? ? conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); ? ? ? ? ? ? // 構(gòu)建請求參數(shù) ? ? ? ? ? ? String params = "code=ASZNL2&tenant=dlh"; ? ? ? ? ? ? // 發(fā)送請求參數(shù) ? ? ? ? ? ? byte[] postData = params.getBytes(StandardCharsets.UTF_8); ? ? ? ? ? ? conn.setRequestProperty("Content-Length", String.valueOf(postData.length)); ? ? ? ? ? ? try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) { ? ? ? ? ? ? ? ? wr.write(postData); ? ? ? ? ? ? } ? ? ? ? ? ? // 獲取響應(yīng)代碼 // ? ? ? ? ? ?int responseCode = conn.getResponseCode(); // ? ? ? ? ? ?System.out.println("Response Code: " + responseCode); ? ? ? ? ? ? // 讀取響應(yīng)內(nèi)容 ? ? ? ? ? ? BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); ? ? ? ? ? ? String line; ? ? ? ? ? ? StringBuilder response = new StringBuilder(); ? ? ? ? ? ? while ((line = reader.readLine()) != null) { ? ? ? ? ? ? ? ? response.append(line); ? ? ? ? ? ? } ? ? ? ? ? ? reader.close(); ? ? ? ? ? ? // 將StringBuilder對象轉(zhuǎn)換為字符串類型 ? ? ? ? ? ? String jsonString = response.toString(); ? ? ? ? ? ? // 解析JSON ? ? ? ? ? ? JSONObject jsonObject = new JSONObject(jsonString); ? ? ? ? ? ? // 獲取data字段的值 ? ? ? ? ? ? JSONObject data = jsonObject.getJSONObject("data"); ? ? ? ? ? ? // 獲取gpbm字段的值 ? ? ? ? ? ? String gpbm = data.getString("gpbm"); ? ? ? ? ? ? System.out.println("gpbm: " + gpbm); ? ? ? ? ? ? // 關(guān)閉連接 ? ? ? ? ? ? conn.disconnect(); ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? }
該處使用的url網(wǎng)絡(luò)請求的數(shù)據(jù)。文章來源:http://www.zghlxwxcb.cn/news/detail-661995.html
總結(jié)
以上就是我根據(jù)查閱資料和實(shí)際情況結(jié)合總結(jié)出來,希望對其他人有所幫助文章來源地址http://www.zghlxwxcb.cn/news/detail-661995.html
到了這里,關(guān)于小程序掃描二維碼獲取網(wǎng)址,通過Jsoup進(jìn)行解析的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!