gpt4 book ai didi

javascript - 如何从 Jain 数据 js 脚本中的 python 访问变量?

转载 作者:行者123 更新时间:2023-12-02 22:06:30 29 4
gpt4 key购买 nike

我正在尝试从上面的 json 数据的 python 代码中访问某些变量。我刚开始使用 python 和 json ,所以我不知道如何解决这个问题。这是我的代码:

 global proxy_addressed
ordered_proxies = []
for numb in range(0, num_tasks):
if numb % num_proxies == 0:
random.shuffle(add_proxy)
proxy_addressed = add_proxy[numb % num_proxies]

ordered_proxies.append(proxy_addressed)

global pvar
for lines in ordered_proxies:
pvar = lines.split(':') # rotating proxy or host

PROXY_HOST = pvar[0]
PROXY_PORT = pvar[1]
PROXY_USER = pvar[2]
PROXY_PASS = pvar[3]
print(PROXY_HOST)
manifest_json = """
{
"version": "1.0.0",
"manifest_version": 2,
"name": "Chrome Proxy",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"<all_urls>",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
},
"minimum_chrome_version":"22.0.0"
}
"""

background_js = """
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "http","""
""" host: "{}",
port: "{}""""".format(PROXY_HOST, PROXY_PORT)
"""
},
bypassList: ["foobar.com"]
}
};
"""

"""
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});

function callbackFn(details) {
return {
authCredentials: {"""
""" username: "{}",
password: "{}""""".format(PROXY_USER, PROXY_PASS)
"""
}
};
}
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});

function callbackFn(details) {
return {
authCredentials: {
username: PROXY_USER,
password: PROXY_PASS
}
};
}

chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking']
);
"""

pluginfile = 'proxy_auth_plugin.zip'

with zipfile.ZipFile(pluginfile, 'w') as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)

co = Options()
co.add_argument("--start-maximized")
co.add_extension(pluginfile)
co.add_argument(win_size)
driver = webdriver.Chrome("chromedriver", chrome_options=co)
driver.execute_script("window.open('{}')".format(page_address))

我尝试在 list /background_json 中使用变量 PROXY_HOST、PROXY_PORT、PROXY_USER 和 PROXY_PASS,但不成功。如何将代码中的变量调用到 json 中?

编辑:我应该更具体一点,我需要将代码输入到我编写的js脚本中,然后由json数据使用。所以真的就像我需要能够访问 js 脚本中的 python 变量一样。

编辑2:我现在在控制台中收到一个以前没有收到的错误:

[22456:27868:0111/201424.041:ERROR:ssl_client_socket_impl.cc(935)] handshake failed; returned -1, SSL error code 1, net_error -201

编辑3:上面的错误是因为我不小心将PROXY_PASS而不是PROXY_PORT放入需要端口的函数中。

最佳答案

如果您有字符串,则使用字符串格式

"""username: "{}",
password: "{}" """.format(PROXY_USER, PROXY_PASS)

f-string

f"""username: "{PROXY_USER}",
password: "{PROXY_PASS}" """
<小时/>

如果字符串中有其他 { } ,那么您将必须使用 {{}}

PROXY_USER = "Hello"
PROXY_PASS = "World"

result = """{{
username: "{}",
password: "{}"
}}""".format(PROXY_USER, PROXY_PASS)

print(result)

PROXY_USER = "Hello"
PROXY_PASS = "World"

result = f"""{{
username: "{PROXY_USER}",
password: "{PROXY_PASS}"
}}"""

print(result)

结果

{
username: "Hello",
password: "World"
}

关于javascript - 如何从 Jain 数据 js 脚本中的 python 访问变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59699108/

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