gpt4 book ai didi

python - 如何使用 Python 读取本地存储?

转载 作者:可可西里 更新时间:2023-11-01 13:06:01 24 4
gpt4 key购买 nike

我必须使用 Python 以自动方式访问(阅读)网页。使用 Python,我可以轻松访问网页内容(HTML 代码)以及服务器发送的 cookie。

现在,在 HTML5 中我们有了一个新概念“本地存储”。因此,我需要修改我的 Python 脚本,以便我也可以读取存储在本地存储中的数据。

可以吗?是否有任何 Python 库可以使它变得简单?

最佳答案

是的,但是,Python 本身并不包含 JavaScript 解释器。因此,您可以像 thibpat 提到的那样,通过 Selenium 在 Web 浏览器实例上执行自定义脚本。

其他选项是 PhantomJS,运行 headless 浏览器。

遍历 localStorage 的脚本

for (var i = 0; i < localStorage.length; i++){
key=localStorage.key(i);
console.log(key+': '+localStorage.getItem(key));
}

高级脚本

如前所述here HTML5 功能浏览器还应实现 Array.prototype.map。所以脚本将是:

Array.apply(0, new Array(localStorage.length)).map(function (o, i) 
{ return localStorage.key(i)+':'+localStorage.getItem(localStorage.key(i)); }
)

Python 绑定(bind)

您可能希望将 Python 与桌面开发框架结合使用。前任。 PyQt .

为什么要用 JavaScript 获取本地存储

来自definition :

Unlike cookies, which can be accessed by both the server and client side, web storage falls exclusively under the purview of client-side scripting. Web storage data is not automatically transmitted to the server in every HTTP request, and a web server can't directly write to Web storage. However, either of these effects can be achieved with explicit client-side scripts, allowing for fine-tuning of the desired interaction with the server.

所以在我看来,本地存储是由网络浏览器(例如 Opera)存储在运行浏览器的硬盘驱动器(或云计算机)上的某个位置的数据。因此,要获取它们,您需要在本地破解 Opera 的执行程序、库和/或数据文件,这很难。最简单的方法是应用客户端脚本,即 JavaScript。

关于python - 如何使用 Python 读取本地存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33096958/

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