我正在尝试使用EEL及其文档将数据从python发送到Javascript,但它似乎不起作用...我的html / js页面中一直保持null。

这就是我所拥有的。基本上,我想获取BING壁纸的链接并将其在我的页面中用作背景。但是直到那时,我要首先得到结果。

BING PY脚本:

import bs4

import requests

import json

def scrape_bing():

BASE_PATH = 'http://www.bing.com'

BASE_REST = '/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US'

URL = BASE_PATH + BASE_REST

r = requests.get(url=URL)

if r.status_code == 200:

data = r.json()

wallpaper_path = BASE_PATH + data['images'][0]['url']

print(wallpaper_path)

else:

raise ValueError("[ERROR] non-200 response from Bing server for '{}'".format(URL))

def main():

scrape_bing()

if __name__ == '__main__':

main()

该脚本有效,它在Python控制台中返回我的URL。

我的具有EEL的main.py如下:

import eel

from inc.bing import scrape_bing

eel.init('web')

myDef = scrape_bing()

@eel.expose

def bingR():

return myDef

try:

eel.start('index.html', mode='chrome', host='localhost', port=8274)

except (SystemExit, MemoryError, KeyboardInterrupt):

pass

print ('Closed browser log...!')

我已经在他们的示例中使用了async命令,如下所示:

async function run() {

let n = await eel.bingR()();

console.log('Got this from Python: ' + n);

}

run();

请帮助我了解所有这些工作原理。

参考方案

不知道您是否意外地将代码格式设置为错误,但这有点差。另外,您在不需要时也导入了bs4和json。

您的scrape_bing()函数未返回任何内容。在“ myDef = scrape_bing()”中分配值时,需要将值返回到“ myDef”。

我稍微改变了一下,提出了这个示例,希望可以帮助您入门。希望这可以帮助。

main.py

import eel

import requests

eel.init('web')

@eel.expose

def bingR():

BASE_PATH = 'http://www.bing.com'

BASE_REST = '/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US'

URL = BASE_PATH + BASE_REST

r = requests.get(url=URL)

if r.status_code == 200:

data = r.json()

wallpaper_path = BASE_PATH + data['images'][0]['url']

print(wallpaper_path)

return wallpaper_path

return 'No wallpaper found'

try:

eel.start('index.html', mode='chrome', host='localhost', port=8274)

except (SystemExit, MemoryError, KeyboardInterrupt):

pass

print ('Closed browser log...!')

web \ myscript.js

async function run() {

let n = await eel.bingR()();

console.log('Got this from Python: ' + n);

document.getElementById('output').value = n;

}

run();

web \ index.html

Test

也感谢您向我介绍鳗鱼。第一次使用它,真的很喜欢它:)

执行onclick时获得意外令牌 - javascript

我正在使用onclick事件从PHP调用JS函数。这是我的代码:我在一个函数中,因此我需要通过PHP来完成它,因为然后我会返回:$html = '

我想在JavaScript中使用模板变量:我的问题是在javascript代码中使用for循环,for循环之间的所有事情都会重复..但我不想要....下面粘贴了我的代码..有人可以告诉我更好的方法吗这..因为这看起来很丑..这是我的代码: {% block extra_javascript %}

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐