Python:使用请求访问会话存储

2024-06-10 11:01:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要使用python请求模块访问sessionStorage(与javascript一样)对象,是否有办法实现我的目标;我看到了其他的答案,但似乎没有一个对我想完成的任务有足够的反应

如果没有办法,除了硒之外,我还有什么其他选择(因为有一种方法可以做到)

简单地说

我想这样做:

var x = sessionStorage; // js code

但在python 3.9中:)


Tags: 模块对象方法答案目标varjscode
1条回答
网友
1楼 · 发布于 2024-06-10 11:01:47

IIUC:编写以下代码是为了将sessionStorage属性值从网页提取到Python dict

import re
import json
from bs4 import BeautifulSoup as bs
import requests

# Setup.
site = 'http://www.some-site.com/page'
exp = '^[\n\s]+sessionStorage.setItem\(.*JSON.stringify\((?P<content>{.*})\)\)'

r = requests.get(site)
if r.status_code == 200:
    soup = bs(r.text)
    # Extract all <script> tags from the full HTML.
    scripts = soup.findAll('script')
    # Loop through all <script> tags until sessionStorage is found.
    script = [s.string for s in scripts if 'sessionStorage' in s.decode()]
    # Use regex (with a named capture group) to extract the JSON data.
    m = re.match(exp, script[0])
    if m:
        content = m['content']
        # Convert scraped JSON data to a dict.
        data = json.loads(content)

注意:regex模式可能需要修改,以适合您(用户)的特定用例

TL;博士(背景):

我在寻找上述代码更优雅的解决方案时遇到了这个问题

在我的例子中,我正在为一个站点编写单元测试,需要从一个特定的网页获取sessionStorage属性,以测试它是否包含预期的元素。由于数据是JSON格式的,因此此代码提取JSON数据并转换为Python dict以供检查

相关问题 更多 >