js获取返回的json数据_python中input返回的类型

js获取返回的json数据_python中input返回的类型在 Python 中获取 JavaScript 返回值通常有以下几种方法 使用 Selenium 的 execute script 方法 确保在 JavaScript 代码中使用 return 关键字来返回值 示例代码 pythonfrom selenium import webdriverjs return document getElementBy input 286 value

在Python中获取JavaScript返回值通常有以下几种方法:

使用Selenium的`execute_script`方法

确保在JavaScript代码中使用`return`关键字来返回值。

示例代码:

```python

from selenium import webdriver

js = "return document.getElementById('input-286').value;"

a = driver.execute_script(js)

print(a) 输出:获取到的值

使用execjs库

安装execjs库:`pip install execjs`示例代码:```python

import execjs

js = """

function generateRandomString(length) {

var result = '';

var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0';

var charactersLength = characters.length;

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

result += characters.charAt(Math.floor(Math.random() * charactersLength));

}

return result;

}

"""

result = execjs.eval(js)

print(result) 输出:随机字符串

使用Node.js命令行

确保电脑上安装了Node.js。

示例代码:

```python

import subprocess

command = subprocess.run(["node", "add.js", "init", "3", "5"], capture_output=True, text=True)

print(command.stdout.strip()) 输出:Node.js执行结果

使用Python中的BeautifulSoup

从HTML源代码中提取JavaScript变量值。示例代码:```python

from bs4 import BeautifulSoup

import re

data = """

var my = 'hello';

var name = 'hi';

var is = 'halo';

"""

soup = BeautifulSoup(data, 'html.parser')

pattern = re.compile(r'var my')

script = soup.find('script', text=pattern)

print(pattern.search(script.text).group(1)) 输出:hello

请根据您的具体需求选择合适的方法。

编程小号
上一篇 2025-04-09 12:28
下一篇 2025-05-31 15:18

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://sigusoft.com/bj/78011.html