如何通过Android中的Jsoup从Instagram个人资料页面获取数据

时间:2016-08-06 07:07:23

标签: android jsoup instagram

在Instagram个人资料页面中有一个按钮" LOAD MORE"加载更多帖子。
图像说明

我想得到" href"这个按钮的attr由android中的jsoup。当我检查查看源代码时,我找不到它的html代码,但在Browser Inspect Element中,它的代码是可见的。

1 个答案:

答案 0 :(得分:0)

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

要获取网址,您需要先呈现网页,然后将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
相关问题