gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 06:41:57 24 4
gpt4 key购买 nike

在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com