{"id":975277,"date":"2024-12-27T06:11:58","date_gmt":"2024-12-26T22:11:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/975277.html"},"modified":"2024-12-27T06:12:00","modified_gmt":"2024-12-26T22:12:00","slug":"python%e5%a6%82%e4%bd%95%e6%8b%bf%e5%88%b0js%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/975277.html","title":{"rendered":"python\u5982\u4f55\u62ff\u5230js\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200605\/9c126fdf-c184-49e2-a7d0-2d9c0dd95205.webp\" alt=\"python\u5982\u4f55\u62ff\u5230js\u6570\u636e\" \/><\/p>\n<p><p> <strong>Python\u62ff\u5230JS\u6570\u636e\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528\u8bf7\u6c42\u5e93\u8fdb\u884c\u7f51\u7edc\u8bf7\u6c42\u3001\u5229\u7528\u6d4f\u89c8\u5668\u81ea\u52a8\u5316\u5de5\u5177\u5982Selenium\u83b7\u53d6\u52a8\u6001\u5185\u5bb9\u3001\u4f7f\u7528PyV8\u7b49\u5e93\u6267\u884cJS\u4ee3\u7801\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u5229\u7528\u8bf7\u6c42\u5e93\u83b7\u53d6API\u8fd4\u56de\u7684\u6570\u636e\u662f\u6700\u76f4\u63a5\u6709\u6548\u7684\u65b9\u5f0f\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8bf7\u6c42\u5e93\u83b7\u53d6API\u6570\u636e\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u8bb8\u591a\u7f51\u7ad9\u7684\u52a8\u6001\u5185\u5bb9\u5b9e\u9645\u662f\u901a\u8fc7API\u8bf7\u6c42\u83b7\u53d6\u7684\u3002\u9996\u5148\uff0c\u901a\u8fc7\u6d4f\u89c8\u5668\u5f00\u53d1\u8005\u5de5\u5177\u627e\u5230\u6570\u636e\u8bf7\u6c42\u7684API\u5730\u5740\uff0c\u7136\u540e\u4f7f\u7528Python\u7684requests\u5e93\u8fdb\u884c\u6a21\u62df\u8bf7\u6c42\uff0c\u83b7\u53d6JSON\u6570\u636e\u5e76\u8fdb\u884c\u89e3\u6790\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u5982\u4f55\u4eceJS\u4e2d\u83b7\u53d6\u6570\u636e\u7684\u5404\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u8bf7\u6c42\u5e93\u8fdb\u884c\u7f51\u7edc\u8bf7\u6c42<\/h2>\n<\/p>\n<p><h3>1. \u5206\u6790\u7f51\u9875\u8bf7\u6c42<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u8bf7\u6c42\u5e93\u83b7\u53d6JS\u6570\u636e\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u901a\u8fc7\u6d4f\u89c8\u5668\u7684\u5f00\u53d1\u8005\u5de5\u5177\uff08\u901a\u5e38\u662fF12\uff09\u6765\u5206\u6790\u7f51\u9875\u7684\u7f51\u7edc\u8bf7\u6c42\u3002\u901a\u8fc7\u201cNetwork\u201d\u9009\u9879\u5361\uff0c\u627e\u5230\u5b9e\u9645\u627f\u8f7d\u6570\u636e\u7684API\u8bf7\u6c42\u5730\u5740\uff0c\u901a\u5e38\u8fd9\u4e9b\u8bf7\u6c42\u8fd4\u56de\u7684\u5185\u5bb9\u662fJSON\u683c\u5f0f\u7684\u3002<\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528requests\u5e93\u8fdb\u884c\u8bf7\u6c42<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u8bbe\u7f6e\u8bf7\u6c42\u5934\uff0c\u6a21\u4eff\u6d4f\u89c8\u5668\u8bbf\u95ee<\/strong><\/h2>\n<p>headers = {<\/p>\n<p>    &#39;User-Agent&#39;: &#39;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/85.0.4183.121 Safari\/537.36&#39;<\/p>\n<p>}<\/p>\n<h2><strong>API\u5730\u5740\uff0c\u53ef\u4ee5\u4ece\u6d4f\u89c8\u5668\u5f00\u53d1\u8005\u5de5\u5177\u4e2d\u83b7\u53d6<\/strong><\/h2>\n<p>url = &#39;https:\/\/example.com\/api\/data&#39;<\/p>\n<h2><strong>\u53d1\u9001\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(url, headers=headers)<\/p>\n<h2><strong>\u68c0\u67e5\u54cd\u5e94\u72b6\u6001<\/strong><\/h2>\n<p>if response.status_code == 200:<\/p>\n<p>    data = response.json()  # \u5c06\u54cd\u5e94\u5185\u5bb9\u89e3\u6790\u4e3aJSON<\/p>\n<p>    print(data)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;F<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led to retrieve data&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u5904\u7406JSON\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4e00\u65e6\u83b7\u53d6\u5230\u6570\u636e\uff0c\u901a\u5e38\u662fJSON\u683c\u5f0f\uff0cPython\u81ea\u5e26\u7684json\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u89e3\u6790\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u5047\u8bberesponse.text\u662f\u83b7\u53d6\u5230\u7684JSON\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>json_data = response.text<\/p>\n<p>data = json.loads(json_data)<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u8bbf\u95ee\u7279\u5b9a\u7684\u5b57\u6bb5<\/strong><\/h2>\n<p>print(data[&#39;key&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u5229\u7528Selenium\u83b7\u53d6\u52a8\u6001\u5185\u5bb9<\/h2>\n<\/p>\n<p><h3>1. \u5b89\u88c5\u548c\u8bbe\u7f6eSelenium<\/h3>\n<\/p>\n<p><p>Selenium\u662f\u4e00\u4e2a\u6d4f\u89c8\u5668\u81ea\u52a8\u5316\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u83b7\u53d6\u7531JavaScript\u52a8\u6001\u751f\u6210\u7684\u5185\u5bb9\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5Selenium\u5e93\u4ee5\u53ca\u76f8\u5e94\u7684\u6d4f\u89c8\u5668\u9a71\u52a8\uff08\u4f8b\u5982ChromeDriver\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528Selenium\u8bbf\u95ee\u7f51\u9875\u5e76\u83b7\u53d6\u6570\u636e<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<h2><strong>\u4f7f\u7528Chrome\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver = webdriver.Chrome(executable_path=&#39;path\/to\/chromedriver&#39;)<\/p>\n<h2><strong>\u8bbf\u95ee\u7f51\u9875<\/strong><\/h2>\n<p>driver.get(&#39;https:\/\/example.com&#39;)<\/p>\n<h2><strong>\u7b49\u5f85\u9875\u9762\u52a0\u8f7d\u5b8c\u6210<\/strong><\/h2>\n<p>driver.implicitly_wait(10)<\/p>\n<h2><strong>\u83b7\u53d6\u52a8\u6001\u5185\u5bb9\uff08\u4f8b\u5982\u67d0\u4e2a\u5143\u7d20\u7684\u6587\u672c\uff09<\/strong><\/h2>\n<p>element = driver.find_element_by_id(&#39;element-id&#39;)<\/p>\n<p>print(element.text)<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u5904\u7406Selenium\u83b7\u53d6\u7684\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>Selenium\u83b7\u53d6\u7684\u5185\u5bb9\u53ef\u4ee5\u901a\u8fc7XPath\u3001CSS\u9009\u62e9\u5668\u7b49\u65b9\u5f0f\u8fdb\u884c\u5b9a\u4f4d\u548c\u63d0\u53d6\uff0c\u975e\u5e38\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528PyV8\u6216\u7c7b\u4f3c\u5e93\u6267\u884cJS\u4ee3\u7801<\/h2>\n<\/p>\n<p><h3>1. \u5b89\u88c5PyV8<\/h3>\n<\/p>\n<p><p>PyV8\u662f\u4e00\u4e2aPython\u7684JavaScript\u5f15\u64ce\u63a5\u53e3\uff0c\u4f46\u7531\u4e8e\u7ef4\u62a4\u548c\u517c\u5bb9\u6027\u95ee\u9898\uff0c\u5efa\u8bae\u5bfb\u627e\u5176\u4ed6\u66f4\u6d3b\u8dc3\u7684\u9879\u76ee\u5982PyMiniRacer\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyv8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u6267\u884cJavaScript\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import PyV8<\/p>\n<h2><strong>\u521b\u5efaJS\u4e0a\u4e0b\u6587<\/strong><\/h2>\n<p>with PyV8.JSContext() as ctxt:<\/p>\n<p>    # \u6267\u884cJS\u4ee3\u7801<\/p>\n<p>    result = ctxt.eval(&quot;var a = 1; var b = 2; a + b;&quot;)<\/p>\n<p>    print(result)  # \u8f93\u51fa3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML<\/h2>\n<\/p>\n<p><p>\u867d\u7136BeautifulSoup\u4e3b\u8981\u7528\u4e8e\u89e3\u6790HTML\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9875\u9762\u7684JS\u6570\u636e\u76f4\u63a5\u5d4c\u5165\u5728HTML\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528BeautifulSoup\u63d0\u53d6\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5BeautifulSoup<\/h3>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528BeautifulSoup\u89e3\u6790HTML<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u5047\u8bbehtml_content\u662f\u83b7\u53d6\u5230\u7684\u9875\u9762HTML<\/strong><\/h2>\n<p>soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u67e5\u627e\u7279\u5b9a\u7684script\u6807\u7b7e<\/strong><\/h2>\n<p>script_tag = soup.find(&#39;script&#39;, text=lambda t: &#39;var data =&#39; in t)<\/p>\n<h2><strong>\u63d0\u53d6\u5e76\u89e3\u6790JS\u6570\u636e<\/strong><\/h2>\n<p>if script_tag:<\/p>\n<p>    js_content = script_tag.string<\/p>\n<p>    # \u4f7f\u7528\u6b63\u5219\u63d0\u53d6JSON\u90e8\u5206<\/p>\n<p>    import re<\/p>\n<p>    match = re.search(r&#39;var data = (\\{.*?\\});&#39;, js_content, re.DOTALL)<\/p>\n<p>    if match:<\/p>\n<p>        json_data = match.group(1)<\/p>\n<p>        data = json.loads(json_data)<\/p>\n<p>        print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/h2>\n<\/p>\n<p><h3>1. \u53cd\u722c\u673a\u5236<\/h3>\n<\/p>\n<p><p>\u8bb8\u591a\u7f51\u7ad9\u5177\u5907\u53cd\u722c\u673a\u5236\uff0c\u5982IP\u5c01\u7981\u3001\u9a8c\u8bc1\u7801\u7b49\u3002\u5728\u8fdb\u884c\u6570\u636e\u6293\u53d6\u65f6\uff0c\u52a1\u5fc5\u9075\u5b88\u7f51\u7ad9\u7684robots\u534f\u8bae\uff0c\u907f\u514d\u8fc7\u4e8e\u9891\u7e41\u7684\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><h3>2. \u6570\u636e\u5408\u6cd5\u6027<\/h3>\n<\/p>\n<p><p>\u786e\u4fdd\u6293\u53d6\u7684\u6570\u636e\u7528\u4e8e\u5408\u6cd5\u7528\u9014\uff0c\u4e0d\u4fb5\u72af\u7f51\u7ad9\u7684\u7248\u6743\u548c\u7528\u6237\u9690\u79c1\u3002<\/p>\n<\/p>\n<p><h3>3. \u6570\u636e\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6\u6570\u636e\u540e\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u6e05\u6d17\u548c\u8f6c\u6362\uff0c\u4ee5\u4fbf\u7528\u4e8e\u540e\u7eed\u7684\u5206\u6790\u6216\u5c55\u793a\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0cPython\u53ef\u4ee5\u6709\u6548\u5730\u83b7\u53d6\u548c\u5904\u7406JS\u751f\u6210\u7684\u6570\u636e\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u7f51\u7ad9\u7279\u6027\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u83b7\u53d6\u7f51\u9875\u4e2d\u7684JavaScript\u6570\u636e\uff1f<\/strong><br \/>\u5728\u83b7\u53d6JavaScript\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5e93\uff0c\u4f8b\u5982BeautifulSoup\u548cRequests\u6765\u6293\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u7136\u540e\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6216\u5176\u4ed6\u89e3\u6790\u6280\u672f\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u3002\u5982\u679c\u7f51\u9875\u4f7f\u7528AJAX\u52a0\u8f7d\u6570\u636e\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Selenium\u6216Pyppeteer\u7b49\u5de5\u5177\u6a21\u62df\u6d4f\u89c8\u5668\u64cd\u4f5c\uff0c\u4ee5\u83b7\u53d6\u52a8\u6001\u751f\u6210\u7684\u5185\u5bb9\u3002<\/p>\n<p><strong>Python\u4e0eJavaScript\u7684\u6570\u636e\u4ea4\u4e92\u6709\u54ea\u4e9b\u5e38\u7528\u65b9\u5f0f\uff1f<\/strong><br \/>\u6570\u636e\u4ea4\u4e92\u7684\u5e38\u7528\u65b9\u5f0f\u5305\u62ec\u4f7f\u7528API\u63a5\u53e3\uff0c\u901a\u8fc7HTTP\u8bf7\u6c42\u83b7\u53d6JSON\u6570\u636e\uff1b\u4f7f\u7528WebSocket\u8fdb\u884c\u5b9e\u65f6\u6570\u636e\u4f20\u8f93\uff1b\u6216\u901a\u8fc7\u5728\u7f51\u9875\u4e2d\u5d4c\u5165Python\u540e\u7aef\u670d\u52a1\u6765\u5904\u7406JavaScript\u53d1\u9001\u7684\u6570\u636e\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u5b9e\u73b0Python\u4e0eJavaScript\u4e4b\u95f4\u7684\u6709\u6548\u901a\u4fe1\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5904\u7406JavaScript\u5bf9\u8c61\u7684\u5e38\u89c1\u5e93\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5904\u7406JavaScript\u5bf9\u8c61\u7684\u5e38\u89c1\u5e93\u5305\u62ecPyExecJS\uff0c\u5b83\u53ef\u4ee5\u6267\u884cJavaScript\u4ee3\u7801\u5e76\u8fd4\u56de\u7ed3\u679c\uff1b\u8fd8\u6709Transcrypt\uff0c\u53ef\u4ee5\u5c06Python\u4ee3\u7801\u7f16\u8bd1\u6210JavaScript\uff0c\u4ee5\u4fbf\u5728\u7f51\u9875\u4e0a\u8fd0\u884c\u3002\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u4f7f\u5f97Python\u7a0b\u5e8f\u80fd\u591f\u66f4\u597d\u5730\u4e0eJavaScript\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u62ff\u5230JS\u6570\u636e\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528\u8bf7\u6c42\u5e93\u8fdb\u884c\u7f51\u7edc\u8bf7\u6c42\u3001\u5229\u7528\u6d4f\u89c8\u5668\u81ea\u52a8\u5316\u5de5\u5177\u5982Selenium\u83b7\u53d6\u52a8\u6001\u5185 [&hellip;]","protected":false},"author":3,"featured_media":975283,"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\/975277"}],"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=975277"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/975277\/revisions"}],"predecessor-version":[{"id":975285,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/975277\/revisions\/975285"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/975283"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=975277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=975277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=975277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}