android - 如何在android中通过Jsoup从instagram个人资料页面获取数据

标签 android jsoup instagram

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

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

最佳答案

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

|列 1 |列2 | | ------ | ------ | | 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

关于android - 如何在android中通过Jsoup从instagram个人资料页面获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38801545/

相关文章:

java - 寻找朝拜方向的通用逻辑

android - 在不同设备上布局自动化测试

java - 如何使用 Jsoup 抓取 Youtube 视频的观看次数?

facebook-graph-api - Instagram 基本 API : Is it possible to get the media_url from "CAROUSEL_ALBUM" in one query?

java - 使用android intent将带有图像的文本共享到instagram

java - 在android项目中放置并运行apk文件

android - 应用程序 :showAsAction ="always|withText" not showing text

java - 使用 jsoup 从其他 h3/class 类中的类中提取 href

java - Jsoup 与高级网站

api - 为什么 Instagram/用户/媒体/最近的 API 调用中缺少某些用户字段?