python3读取本地_如何使用Python读取本地存储?

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

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

有可能这样做吗?是否有任何Python库可简化此操作?

解决方法:

是的,但是Python本身不包含JavaScript解释器.

因此,您可以如thibpat所述通过Selenium在Web浏览器实例上执行自定义脚本.

其他选项是运行无头浏览器的PhantomJS.

遍历localStorage的脚本

for (var i = 0; i < localStorage.length; i++){

key=localStorage.key(i);

console.log(key+': '+localStorage.getItem(key));

}

高级脚本

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

Array.apply(0, new Array(localStorage.length)).map(function (o, i)

{ return localStorage.key(i)+':'+localStorage.getItem(localStorage.key(i)); }

)

Python绑定

您可能希望将Python绑定与桌面开发框架一起使用.例如PyQt.

为什么JavaScript要获取本地存储

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.

标签:html5,web-scraping,local-storage,python

来源: https://codeday.me/bug/20191119/2037360.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值