【问题标题】:Website notification counter in android using Jsoupandroid中使用Jsoup的网站通知计数器
【发布时间】:2020-12-18 10:43:30
【问题描述】:

我有一个包含网站的 web 视图。 我想从页面中提取一个 int 值。 我要转换的部分的html代码是:

<body>
   

     <div class="toolbar" id="main-toolbar">
            <a> <!-- something--> </a>
            <a> <!-- something--> </a>
            <a> <!-- something--> </a>
            <a href="https://thesitethatI'mtalkingabout.com/it/me/notifications" data-unread="1" class="toolbar__item cta-login ">
                    <span class="is-icon-bell toolbar__icon"></span>
                    <span class="toolbar__badge">1</span>
                    "
                                   Notifications
                             "
                    </a> 
    <!--this is the notification number, if you aren't logged in this would be empty--> 
        </div>
    </body>

现在我将从页面中提取值(在本例中为 1)并用它在我的应用程序中创建一个变量。我怎样才能做到这一点? 能够从 data-unread"1" 而不是从 span 中提取值会更好。

我要查找的数字是网站的通知计数。为了让Jsoup找到他,他必须登录。我使用的webView允许你登录。Jsoup有没有办法分析页面的HTML,因为它出现在webview中?

【问题讨论】:

    标签: android html android-studio webview jsoup


    【解决方案1】:

    你可以使用JSoup解析器:

    Document doc = Jsoup.connect("someurl").get();
            Log.i("DOC", doc.toString().toString());
            Elements elementsHtml = doc.getElementsByTag("span");  <--- here you specify the html tag where is the text is located
            String[] temp1 = new String[99];    
            int i =0;
            for(Element element: elementsHtml){
                temp1[i] = element.text();
                i++;
            }
    

    您只需按照CSS selector-syntax 查找元素。文本被解析为字符串。使用 Integer、Double 等 Number 类解析其他格式的值。

    您可以使用Connection接口中的其他方法进行登录操作,例如:

    • cookie(), cookies​() - 添加cookies
    • data​() - 获取/设置数据
    • execute() - 执行请求
    • get() - 以 GET 形式执行请求并解析结果
    • header(), headers() - 添加请求头
    • method() - 设置请求方法
    • post() - 以 POST 形式执行请求并解析结果
    • timeout() - 设置请求超时
    • url() - 设置请求URLurl
    • userAgent() - 设置用户代理

    例如,看看这个 sn-p:

    try {
        //grab login form page first
        Response loginPageResponse = 
        Jsoup.connect("someurl")
        .referrer("http://www.rediff.com/")
        .userAgent("Mozilla/5.0")
        .timeout(10 * 1000)
        .get();
    
        System.out.println("Fetched login page");
    
        //get the cookies from the response
        Map<String, String> mapLoginPageCookies = loginPageResponse.cookies();
    
        //make data map containing all the parameters and its values found in the form
        Map<String, String> mapParams = new HashMap<String, String>();
        mapParams.put("FormName", "existing");
        mapParams.put("seclogin", "on");
        mapParams.put("login", "YOUR_USER_ID");
        mapParams.put("passwd", "YOUR_PASSWORD");
        mapParams.put("remember", "1");
        mapParams.put("proceed", "Go");
    
        //URL found in form's action attribute
        String strActionURL = "https://mail.rediff.com/cgi-bin/login.cgi";
    
        Response responsePostLogin =
        Jsoup.connect("someurl/login")
         .referrer("someurl/login")//referrer will be the login page's URL
         .userAgent("Mozilla/5.0")
         .timeout(10 * 1000)
        .data(mapParams)//post parameters                    
        .cookies(mapLoginPageCookies)//cookies received from login page
        .post();
    
        System.out.println("HTTP Status Code: " + responsePostLogin.statusCode());
    
        //parse the document from response
        Document document = responsePostLogin.parse();
        System.out.println(document);
    
        //get the cookies
        Map<String, String> mapLoggedInCookies = responsePostLogin.cookies();
    } catch (IOException e) {
            // TODO Auto-generated catch block
        e.printStackTrace();
    }
    

    【讨论】:

    • 非常感谢您的回答。但是,我有一个迷雾。我要查找的数字是网站的通知计数。为了让Jsoup找到他,他必须登录。我使用的webView允许你登录。Jsoup有没有办法分析页面的HTML,因为它出现在webview中?
    猜你喜欢
    • 2016-08-29
    • 2016-02-23
    • 2016-07-14
    • 1970-01-01
    • 2019-09-24
    • 2014-06-29
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    相关资源
    最近更新 更多