【问题标题】:How to get data from instagram profile page by Jsoup in android如何通过Android中的Jsoup从instagram个人资料页面获取数据
【发布时间】:2016-12-12 14:18:07
【问题描述】:

instagram 个人资料页面中有一个“加载更多”按钮,用于加载更多帖子。
图片说明

我想通过 android 中的 jsoup 获取此按钮的“href”属性。当我检查查看源代码时,我找不到它的 html 代码,但在 Browser Inspect Element 中它的代码是可见的。

【问题讨论】:

    标签: android jsoup instagram


    【解决方案1】:

    Jsoup 只能解析从服务器检索到的源代码(右键单击 > 查看源代码)。但是,您的按钮使用 javascript 添加到 dom(右键单击 > 检查)。

    要获取url,需要先渲染页面,然后将html传给jsoup。

    这是一个如何使用HtmlUnit 的示例:

    page.html - 源代码

    <html>
    <head>
        <script src="loadData.js"></script>
    </head>
    <body onLoad="loadData()">
        <div class="container">
            <table id="data" border="1">
                <tr>
                    <th>col1</th>
                    <th>col2</th>
                </tr>
            </table>
        </div>
    </body>
    </html>
    

    loadData.js

        // append rows and cols to table.data in page.html
        function loadData() {
            data = document.getElementById("data");
            for (var row = 0; row < 2; row++) {
                var tr = document.createElement("tr");
                for (var col = 0; col < 2; col++) {
                    td = document.createElement("td");
                    td.appendChild(document.createTextNode(row + "." + col));
                    tr.appendChild(td);
                }
                data.appendChild(tr);
            }
        }
    

    加载到浏览器时的page.html

    | Col1 | Col2 | | ------ | ------ | | 0.0 | 0.1 | | 1.0 | 1.1 |

    使用jsoup解析page.html获取col数据

        // load source from file
        Document doc = Jsoup.parse(new File("page.html"), "UTF-8");
    
        // iterate over row and col
        for (Element row : doc.select("table#data > tbody > tr"))
    
            for (Element col : row.select("td"))
    
                // print results
                System.out.println(col.ownText());
    

    输出

    (空)

    发生了什么?

    Jsoup 解析从服务器交付的源代码(或者在这种情况下从文件加载)。它不调用客户端操作,例如 JavaScript 或 CSS DOM 操作。在此示例中,行和列从未附加到数据表中。

    如何在浏览器中解析我的页面?

        // load page using HTML Unit and fire scripts
        WebClient webClient = new WebClient();
        HtmlPage myPage = webClient.getPage(new File("page.html").toURI().toURL());
    
        // convert page to generated HTML and convert to document
        doc = Jsoup.parse(myPage.asXml());
    
        // iterate row and col
        for (Element row : doc.select("table#data > tbody > tr"))
    
            for (Element col : row.select("td"))
    
                // print results
                System.out.println(col.ownText());
    
        // clean up resources        
        webClient.close();
    

    输出

    0.0
    0.1
    1.0
    1.1
    

    【讨论】:

    • 但是 HtmlUnit 在 Android 上不起作用,而且 selenium webdriver 也不好,因为我必须在活动中创建 webview。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2015-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多