亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用EEL將數據從Python發送到Javascript

使用EEL將數據從Python發送到Javascript

慕田峪4524236 2022-08-25 15:11:16
我正在嘗試使用EEL及其文檔將數據從python發送到Javascript,但它似乎不起作用...我一直在我的html / js頁面中得到空值。這是我所擁有的。基本上,我想獲取BING壁紙的鏈接,并將其用作我的頁面作為背景。但在那之前,我想先得到結果。必應派腳本:import bs4import requestsimport jsondef 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。我 main.py 有EEL如下:import eelfrom inc.bing import scrape_bingeel.init('web')myDef = scrape_bing()@eel.exposedef bingR():   return myDeftry:   eel.start('index.html', mode='chrome', host='localhost', port=8274)except (SystemExit, MemoryError, KeyboardInterrupt):   passprint ('Closed browser log...!')我使用了一個異步命令,就像在他們的例子中一樣,就像這樣:    <script type="text/javascript" src="/eel.js"></script>    <script type="text/javascript">    async function run() {        let n = await eel.bingR()();        console.log('Got this from Python: ' + n);    }    run();    </script>請幫助我了解這一切是如何工作的。
查看完整描述

1 回答

?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

不確定您是否不小心將代碼格式錯誤,但它有點偏差。此外,您在不需要的時候導入了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();

網站\索引.html


<!doctype html>

<html>

<head>

  <meta charset="utf-8">

  <title>Test</title>

</head>

<body>

  <script type="text/javascript" src="/eel.js"></script>

  <script type="text/javascript" src="/myscript.js"></script>

  <input id="output" value="Output here" style="width: 700px;">

</body>

</html>

也感謝您向我介紹鰻魚。第一次使用它,真的很喜歡它:)


查看完整回答
反對 回復 2022-08-25
  • 1 回答
  • 0 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號