Dare to think and do, creativity comes from life πŸ₯΄

preface

Yesterday when I queried how much electricity was left in my room on the public account, I felt that these operations were tedious and troublesome, so I came up with the idea of using JAVA to climb down these data.

Once you’ve climbed into the data, you can do a lot of things with that data, so I’m going to share with you my ideas on how to implement them in Java. If you’re interested, please read this article.

Environment set up

JDK version: 1.8

Jsoup version: 1.12.1

Compiler: IDEA

  • Project type select Java point next
  • Click next
  • Fill in the project name and project path and click Next
  • Create the package and lib folder under SRC
  • frommavenDownload from warehousejsoupVersion for 1.12.1
  • After downloading the JAR package, copy it to the project’s lib directory and apply it

Get the public number interface

Obtaining the login interface

  • Open the homepage of the official account, choose to open it in your browser, and copy the link
  • Open the link you just obtained in your PC browser
  • Open the packet capture tool and use Charles here. After observing the URL in the address bar, we find that all the login parameters are in the URL. At this time, we only need to fill in our mobile phone number and password in the URL and press Enter to initiate the request
// loginPhone and password in the URL are the parameters we need to fill in
var url = "http://www.quanfangtong.net/phonehtml/phoneLogin/login.action?loginPhone=xxxxx&password=xxx&conpanyId=Company_201706271 13853JMD7cK&language=Chinese";
Copy the code
  • Open the packet capture tool and view the intercepted requests
Through the packet capture tool, we obtain the following information: 1. Url of the interface 2. Parameters required for invoking the interface 3. HTTP request packet, from which it is learned that the method is a GET request and the parameters and values required in its headerCopy the code

Obtain the power query interface

  • On the PC browser, the page for querying battery quantity is displayed
  • View the URL of the current browser, open the packet capture tool, and find the power query interface
Through the packet capture tool, we get the following information: 1. Cookies are returned after login, and each request is authenticated by cookies. 2. The request mode is GETCopy the code

So far, we got the power query interface and the parameters he needs to pass, then we use the code to achieve the crawl 😜

Write utility classes

  • Create the JsoupUtils class under the util package

According to the information in the interface and HTTP request we got above, we use the Jsoup plug-in package to initiate the request and construct the obtained information. Login method, which will return a cookie. Method of obtaining electric quantity information, according to the DOM structure of the page, find the required information, put it into the HashMap, and return it. Jsoup Syntax please go to :Jsoup Chinese documentation

package com.lk.util;

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

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class JsoupUtils {
    /** * get cookie *@param loginUrl
     * @param data
     * @return
     * @throws IOException
     */
    public Map<String, String> jsoupCookieLogin(String loginUrl, HashMap<String,String> data) throws IOException {
        // Initiate a login request
        Connection.Response login = Jsoup.connect(loginUrl)
                // Ignore type validation
                .ignoreContentType(true)
                // Disable redirection
                .followRedirects(false)
                .postDataCharset("utf-8")
                // Set the request header information
                .header("Upgrade-Insecure-Requests"."1")
                .header("Accept"."text/html,application/xhtml+xml,application/xml; Q = 0.9, image/webp image/apng, * / *; Q = 0.8, application/signed - exchange; v=b3; Q = 0.9")
                .header("Accept-Encoding"."gzip, deflate")
                .header("Accept-Language"."zh-CN,zh; Q = 0.9, en. Q = 0.8")
                .header("Connection"."keep-alive")
                .header("User-Agent"."Mozilla / 5.0 (Linux; The Android 8.0. Pixel 2 Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Mobile Safari/537.36")
                // The login interface requires parameters
                .data(data)
                // The request method of the login interface
                .method(Connection.Method.GET)
                / / execution
                .execute();
        // Set the character set encoding
        login.charset("UTF-8");
        // Prints cookies returned by the interface
        return login.cookies();
    };

    /** * Get power information *@param cookie
     * @param url
     * @return
     * @throws IOException
     */
    public HashMap<String,String> getBatteryInfo(Map<String,String> cookie,String url) throws IOException {
        // Power information
        HashMap<String, String> batteryInfo = new HashMap<>();
        // Adjust the electricity consumption query interface to obtain the returned Dom
        Document document = Jsoup.connect(url)
                / / set cookies
                .cookies(cookie)
                .get();
        // Parse the Dom to get the power information
        if(! document.title().equals(Error page)) {
            // Use electricity today
            String electricityToday = document.body().select("div > div > div").eq(2).text();
            // Remaining power
            String remainingBattery = document.body().select("div > div > div").eq(3).text() + "εΊ¦";
            // Accumulated electricity usage this month
            String currentMonthBatteryTotal = document.body().select("div > div > div").eq(5).text();
            // Count time
            String time = document.body().select("div > div > div").eq(6).text();
            // Assign power information
            batteryInfo.put("electricityToday", electricityToday);
            batteryInfo.put("remainingBattery", remainingBattery);
            batteryInfo.put("currentMonthBatteryTotal", currentMonthBatteryTotal);
            batteryInfo.put("time", time);
            batteryInfo.put("code"."0");
            batteryInfo.put("msg"."Get ahead");
        } else {
            batteryInfo.put("code"."1");
            batteryInfo.put("msg"."Fetch failed");
        }
        returnbatteryInfo; }}Copy the code

Test tool class

  • Create the NetWorkTest class in the httpNetWork package and test it by calling the method in the utility class in the main method
package com.lk.httpNetWork;

import com.lk.util.JsoupUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class NetWorkTest {

    public static void main(String[] args) throws IOException {
        // Receives the returned Cookie
        Map<String,String> cookie;
        // Construct login parameters
        HashMap<String,String> UserInfo = new HashMap<>();
        UserInfo.put("loginPhone"."* * * *");
        UserInfo.put("password"."* * * *");
        UserInfo.put("conpanyId"."Company_20170627113853JMD7cK");
        UserInfo.put("language"."Chinese");
        JsoupUtils jsoupUtils = new JsoupUtils();
        // Call the cookie login function to get the cookie
        cookie = jsoupUtils.jsoupCookieLogin("http://www.quanfangtong.net/phonehtml/phoneLogin/login.action",UserInfo);
        // Call the method to get the power information
        HashMap<String,String> batteryInfo = jsoupUtils.getBatteryInfo(cookie,"http://www.quanfangtong.net/phonehtml/myIntelligent/findMyelec.action");
        if(batteryInfo.get("code").equals("0")){
            System.out.println("Electricity today:" + batteryInfo.get("electricityToday"));
            System.out.println("Remaining battery:" + batteryInfo.get("remainingBattery"));
            System.out.println("Accumulated electricity consumption this month:" + batteryInfo.get("currentMonthBatteryTotal"));
            System.out.println("Statistical time:" + batteryInfo.get("time"));
        }else{
            System.out.println(batteryInfo.get("msg")); }}}Copy the code
  • The execution result

Write in the last

At this point, the Java access my end of the process of electricity meter information sharing inside the room, now we got the data, write a interface on the server, call the utility class method, electricity information returned to the caller, the caller to get these data, can be in any form to display, late I’m going to write a Mac the small tools, Display the data returned by the interface in the top bar, and welcome interested developers to keep an eye on it.

  • If there are any errors in this article, please correct them in the comments section. If this article helped you, please like it and follow 😊
  • This article was first published in nuggets. Reprint is prohibited without permission πŸ’Œ