{"id":257274,"date":"2024-05-15T12:08:03","date_gmt":"2024-05-15T04:08:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/257274.html"},"modified":"2024-05-15T12:08:08","modified_gmt":"2024-05-15T04:08:08","slug":"%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87-python-%e6%89%a7%e8%a1%8c%e7%89%b9%e5%ae%9a-js-%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/257274.html","title":{"rendered":"\u5982\u4f55\u901a\u8fc7 python \u6267\u884c\u7279\u5b9a js \u4ee3\u7801"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27103606\/f780bb68-00a1-475a-a05e-c5cf9b5e2dbd.webp\" alt=\"\u5982\u4f55\u901a\u8fc7 python \u6267\u884c\u7279\u5b9a js \u4ee3\u7801\" \/><\/p>\n<p><p><strong>\u901a\u8fc7Python\u6267\u884c\u7279\u5b9aJS\u4ee3\u7801<\/strong> \u4e3b\u8981\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<strong>\u4f7f\u7528PyV8\u6a21\u5757\u3001\u5229\u7528Selenium Webdriver\u3001\u501f\u52a9PyQt\/PySide\u6846\u67b6\u3001\u5229\u7528\u67d0\u4e9bAPI\u670d\u52a1<\/strong>\u3002\u5176\u4e2d\uff0c<strong>Selenium Webdriver<\/strong> \u662f\u4e00\u79cd\u975e\u5e38\u6d41\u884c\u7684\u65b9\u6cd5\uff0c\u5141\u8bb8\u6211\u4eec\u6a21\u62df\u6d4f\u89c8\u5668\u73af\u5883\u6765\u6267\u884cJS\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e00\u3001\u4f7f\u7528PyV8\u6a21\u5757<\/strong><\/h3>\n<\/p>\n<p><p>PyV8\u662f\u4e00\u4e2aPython\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2aJavaScript\u8fd0\u884c\u65f6\u73af\u5883\uff0c\u5e76\u5229\u7528Google\u7684V8\u5f15\u64ce\u6267\u884cJavaScript\u4ee3\u7801\u3002\u8fd9\u4e2a\u5e93\u5bf9\u4e8e\u9700\u8981\u5728Python\u73af\u5883\u4e2d\u6267\u884c\u7eafJavaScript\u4ee3\u7801\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4><strong>\u5b89\u88c5PyV8<\/strong><\/h4>\n<\/p>\n<p><p>\u8981\u5728Python\u4e2d\u4f7f\u7528PyV8\uff0c\u9996\u5148\u9700\u8981\u786e\u4fddPyV8\u5e93\u5728\u60a8\u7684\u7cfb\u7edf\u4e2d\u662f\u53ef\u7528\u7684\u3002\u5b89\u88c5PyV8\u53ef\u80fd\u9700\u8981\u7f16\u8bd1\uff0c\u56e0\u6b64\u786e\u4fdd\u6240\u6709\u4f9d\u8d56\u7684\u5f00\u53d1\u5de5\u5177\u90fd\u5df2\u7ecf\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyv8-bin<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4><strong>\u4f7f\u7528PyV8\u6267\u884cJS\u4ee3\u7801<\/strong><\/h4>\n<\/p>\n<p><p>\u4f7f\u7528PyV8\u6267\u884cJS\u4ee3\u7801\u76f8\u5bf9\u76f4\u63a5\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aJS\u73af\u5883\uff0c\u5e76\u5728\u5176\u4e2d\u6ce8\u5165\u548c\u6267\u884c\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import PyV8<\/p>\n<p>with PyV8.JSContext() as ctxt:<\/p>\n<p>    ctxt.eval(&quot;function add(a, b) { return a + b; }&quot;)<\/p>\n<p>    result = ctxt.eval(&quot;add(3, 5)&quot;)<\/p>\n<p>    print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><strong>\u4e8c\u3001\u501f\u52a9Selenium Webdriver<\/strong><\/h3>\n<\/p>\n<p><p>Selenium Webdriver\u662f\u4e00\u4e2a\u7528\u4e8e\u81ea\u52a8\u5316\u7f51\u9875\u6d4f\u89c8\u5668\u4ea4\u4e92\u7684\u5de5\u5177\u3002\u5b83\u53ef\u4ee5\u901a\u8fc7\u7a0b\u5e8f\u63a7\u5236\u6d4f\u89c8\u5668\u5e76\u6267\u884c\u5176\u4e2d\u7684JavaScript\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4><strong>\u5b89\u88c5Selenium Webdriver<\/strong><\/h4>\n<\/p>\n<p><p>\u5b89\u88c5Selenium\u5f88\u7b80\u5355\uff0c\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4><strong>\u4f7f\u7528Selenium WebDriver\u6267\u884cJS\u4ee3\u7801<\/strong><\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u4e0b\u8f7d\u4e00\u4e2aWebDriver\uff0c\u6bd4\u5982ChromeDriver\u6216GeckoDriver\uff0c\u5e76\u786e\u4fdd\u5b83\u5728\u7cfb\u7edf\u7684PATH\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>driver = webdriver.Chrome()<\/p>\n<p>driver.get(&quot;http:\/\/yourwebsite.com&quot;)  # \u52a0\u8f7d\u7f51\u9875<\/p>\n<p>result = driver.execute_script(&quot;return yourJavaScriptFunction();&quot;)  # \u6267\u884cJS\u4ee3\u7801<\/p>\n<p>print(result)<\/p>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><strong>\u4e09\u3001\u5229\u7528PyQt\/PySide\u6846\u67b6<\/strong><\/h3>\n<\/p>\n<p><p>PyQt\u548cPySide\u90fd\u662fQt\u6846\u67b6\u7684Python\u7ed1\u5b9a\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684GUI\u7ec4\u4ef6\u548c\u5de5\u5177\u3002Qt\u4e2d\u7684QWebEngineView\u7ec4\u4ef6\u53ef\u4ee5\u7528\u6765\u52a0\u8f7d\u7f51\u9875\uff0c\u5e76\u4e14\u6267\u884cJavaScript\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4><strong>\u5b89\u88c5PyQt5\/PySide2<\/strong><\/h4>\n<\/p>\n<p><p>\u901a\u8fc7pip\u5b89\u88c5PyQt5\u6216PySide2\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyqt5  # \u6216\u8005 pip install PySide2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4><strong>\u4f7f\u7528PyQt\/PySide\u6267\u884cJS\u4ee3\u7801<\/strong><\/h4>\n<\/p>\n<p><p>\u5728PyQt\u6216PySide\u4e2d\u6267\u884cJavaScript\u9700\u8981\u8bbe\u7f6e\u4e00\u4e2aQWebEngineView\uff0c\u7136\u540e\u5c06JavaScript\u4ee3\u7801\u4f20\u9012\u7ed9\u9875\u9762\u7684runJavaScript\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PyQt5.QtWidgets import QApplication<\/p>\n<p>from PyQt5.QtWebEngineWidgets import QWebEngineView<\/p>\n<p>app = QApplication([])<\/p>\n<p>view = QWebEngineView()<\/p>\n<p>def js_callback(result):<\/p>\n<p>    print(result)<\/p>\n<p>    app.quit()<\/p>\n<p>view.loadFinished.connect(lambda ok: {<\/p>\n<p>    if ok: view.page().runJavaScript(&quot;return YourJSFunction();&quot;, js_callback)<\/p>\n<p>})<\/p>\n<p>view.load(QUrl(&quot;http:\/\/yourwebsite.com&quot;))<\/p>\n<p>app.exec_()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3><strong>\u56db\u3001\u5229\u7528\u67d0\u4e9bAPI\u670d\u52a1<\/strong><\/h3>\n<\/p>\n<p><p>\u6709\u4e9b\u5728\u7ebf\u670d\u52a1\u63d0\u4f9b\u4e86\u63a5\u53e3\uff0c\u901a\u8fc7\u8fd9\u4e9bAPI\u4f60\u53ef\u4ee5\u8fd0\u884cJS\u4ee3\u7801\u3002\u8fd9\u4e9b\u670d\u52a1\u901a\u5e38\u662f\u4ed8\u8d39\u7684\uff0c\u5e76\u63d0\u4f9b\u8d44\u6e90\u4f9b\u4f60\u8fdc\u7a0b\u6267\u884c\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4><strong>\u9009\u62e9API\u670d\u52a1<\/strong><\/h4>\n<\/p>\n<p><p>\u5e02\u9762\u4e0a\u6709\u8bb8\u591a\u516c\u53f8\u63d0\u4f9b\u6b64\u7c7b\u670d\u52a1\uff0c\u5982Webtask.io\u3001Google Cloud Functions\u7b49\uff0c\u9700\u8981\u6839\u636e\u4e2a\u4eba\u6216\u9879\u76ee\u7684\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e73\u53f0\u3002<\/p>\n<\/p>\n<p><h4><strong>\u5229\u7528API\u670d\u52a1\u6267\u884cJS\u4ee3\u7801<\/strong><\/h4>\n<\/p>\n<p><p>\u5927\u591a\u6570API\u670d\u52a1\u90fd\u63d0\u4f9b\u4e86\u8be6\u7ec6\u7684\u6587\u6863\u5173\u4e8e\u5982\u4f55\u901a\u8fc7\u5b83\u4eec\u7684\u63a5\u53e3\u8fd0\u884cJavaScript\u4ee3\u7801\u3002\u901a\u5e38\u662f\u4ee5HTTP\u8bf7\u6c42\u7684\u5f62\u5f0f\u53d1\u9001\u4f60\u7684JS\u4ee3\u7801\uff0c\u5e76\u63a5\u6536\u6267\u884c\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>api_endpoint = &quot;http:\/\/yourapi.com\/execute-js&quot;<\/p>\n<p>payload = {<\/p>\n<p>    &quot;code&quot;: &quot;function test() { return &#039;Hello, world!&#039;; } test();&quot;<\/p>\n<p>}<\/p>\n<p>response = requests.post(api_endpoint, json=payload)<\/p>\n<p>print(response.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u65b9\u6cd5\u4e2d\uff0c\u6bcf\u4e2a\u90fd\u6709\u81ea\u5df1\u7684\u5e94\u7528\u573a\u666f\u548c\u4f18\u52bf\uff0c\u5982PyV8\u662f\u8f7b\u91cf\u7ea7\u7684\uff0c\u9002\u5408\u5feb\u901f\u6267\u884c\u7b80\u5355\u7684JS\uff1bSelenium\u5219\u66f4\u591a\u7528\u4e8e\u6a21\u62df\u6d4f\u89c8\u5668\u73af\u5883\uff1bPyQt\/PySide\u9002\u5408\u96c6\u6210\u5230Qt\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff1b\u800cAPI\u670d\u52a1\u5219\u9002\u7528\u4e8e\u4e0d\u60f3\u5728\u672c\u5730\u6267\u884cJS\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q: \u5982\u4f55\u5728 Python \u4e2d\u6267\u884c\u7279\u5b9a\u7684 JavaScript \u4ee3\u7801\uff1f<\/strong><\/p>\n<p>A: \u901a\u8fc7\u4f7f\u7528Python\u7684JavaScript\u6267\u884c\u5f15\u64ce\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u73af\u5883\u4e2d\u6267\u884c\u7279\u5b9a\u7684JavaScript\u4ee3\u7801\u3002\u4e0b\u9762\u662f\u4e00\u79cd\u5b9e\u73b0\u65b9\u5f0f\uff1a<\/p>\n<ol>\n<li>\n<p>\u5b89\u88c5PyExecJS\u6a21\u5757\uff1a<\/p>\n<blockquote>\n<p><code>pip install PyExecJS<\/code><\/p>\n<\/blockquote>\n<\/li>\n<li>\n<p>\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757\uff1a<\/p>\n<pre><code class=\"language-python\">import execjs\nimport sys\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u5b9a\u4e49JavaScript\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">js_code = &quot;&quot;&quot;\nfunction multiply(a, b) {\n  return a * b;\n}\n&quot;&quot;&quot;\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u6267\u884cJavaScript\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">ctx = execjs.compile(js_code)\nresult = ctx.call(&quot;multiply&quot;, 5, 7)\nprint(result)  # \u8f93\u51fa\u7ed3\u679c\uff1a35\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u6267\u884c\u6307\u5b9a\u7684JavaScript\u4ee3\u7801\u5e76\u83b7\u5f97\u7ed3\u679c\u3002<\/p>\n<p><strong>Q: \u5982\u4f55\u5728Python\u4e2d\u6267\u884c\u5305\u542b\u5916\u90e8JavaScript\u6587\u4ef6\u7684\u4ee3\u7801?<\/strong><\/p>\n<p>A: \u5982\u679c\u4f60\u9700\u8981\u5728Python\u4e2d\u6267\u884c\u5305\u542b\u5916\u90e8JavaScript\u6587\u4ef6\u7684\u4ee3\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\n<p>\u5b89\u88c5PyExecJS\u6a21\u5757\uff1a<\/p>\n<blockquote>\n<p><code>pip install PyExecJS<\/code><\/p>\n<\/blockquote>\n<\/li>\n<li>\n<p>\u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757\uff1a<\/p>\n<pre><code class=\"language-python\">import execjs\nimport sys\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u8bfb\u53d6JavaScript\u6587\u4ef6\u7684\u5185\u5bb9\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#039;script.js&#039;, &#039;r&#039;) as file:\n    js_code = file.read()\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u6267\u884cJavaScript\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">ctx = execjs.compile(js_code)\nresult = ctx.call(&quot;multiply&quot;, 5, 7)\nprint(result)  # \u8f93\u51fa\u7ed3\u679c\uff1a35\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u6267\u884c\u5305\u542b\u5916\u90e8JavaScript\u6587\u4ef6\u7684\u4ee3\u7801\u3002<\/p>\n<p><strong>Q: \u5982\u4f55\u5728Python\u4e2d\u6267\u884c\u9700\u8981\u6d4f\u89c8\u5668\u73af\u5883\u652f\u6301\u7684JavaScript\u4ee3\u7801\uff1f<\/strong><\/p>\n<p>A: \u5982\u679c\u4f60\u9700\u8981\u5728Python\u4e2d\u6267\u884c\u9700\u8981\u6d4f\u89c8\u5668\u73af\u5883\u652f\u6301\u7684JavaScript\u4ee3\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528selenium\u5e93\u4e0e\u6d4f\u89c8\u5668\u8fdb\u884c\u4ea4\u4e92\uff0c\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\n<p>\u5b89\u88c5selenium\u5e93\uff1a<\/p>\n<blockquote>\n<p><code>pip install selenium<\/code><\/p>\n<\/blockquote>\n<\/li>\n<li>\n<p>\u4e0b\u8f7d\u5bf9\u5e94\u6d4f\u89c8\u5668\u7684WebDriver\u5e76\u5c06\u5176\u8def\u5f84\u6dfb\u52a0\u5230\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/li>\n<li>\n<p>\u5bfc\u5165selenium\u6a21\u5757\uff1a<\/p>\n<pre><code class=\"language-python\">from selenium import webdriver\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u521b\u5efa\u6d4f\u89c8\u5668\u9a71\u52a8\u5bf9\u8c61\uff1a<\/p>\n<pre><code class=\"language-python\">driver = webdriver.Chrome()  # \u4f7f\u7528Chrome\u6d4f\u89c8\u5668\u9a71\u52a8\uff0c\u5982\u679c\u4f7f\u7528\u5176\u4ed6\u6d4f\u89c8\u5668\u5219\u521b\u5efa\u76f8\u5e94\u7684\u9a71\u52a8\u5bf9\u8c61\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u52a0\u8f7d\u7f51\u9875\uff1a<\/p>\n<pre><code class=\"language-python\">driver.get(&quot;http:\/\/www.example.com&quot;)  # \u66ff\u6362\u4e3a\u4f60\u8981\u52a0\u8f7d\u7684\u7f51\u9875URL\n<\/code><\/pre>\n<\/li>\n<li>\n<p>\u6267\u884cJavaScript\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">result = driver.execute_script(&quot;return document.title;&quot;)\nprint(result)  # \u8f93\u51fa\u7ed3\u679c\uff1aExample Dom<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u6267\u884c\u9700\u8981\u6d4f\u89c8\u5668\u73af\u5883\u652f\u6301\u7684JavaScript\u4ee3\u7801\uff0c\u5e76\u4e0e\u7f51\u9875\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u901a\u8fc7Python\u6267\u884c\u7279\u5b9aJS\u4ee3\u7801 \u4e3b\u8981\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528PyV8\u6a21\u5757\u3001\u5229\u7528Selenium Webdr [&hellip;]","protected":false},"author":3,"featured_media":257278,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/257274"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=257274"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/257274\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/257278"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=257274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=257274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=257274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}