{"id":1054164,"date":"2024-12-31T14:39:59","date_gmt":"2024-12-31T06:39:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1054164.html"},"modified":"2024-12-31T14:40:01","modified_gmt":"2024-12-31T06:40:01","slug":"python%e7%88%ac%e8%99%ab%e5%a6%82%e4%bd%95%e6%9e%84%e9%80%a0%e5%ad%97%e5%85%b8%e5%ad%98%e5%82%a8%e5%8f%82%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1054164.html","title":{"rendered":"python\u722c\u866b\u5982\u4f55\u6784\u9020\u5b57\u5178\u5b58\u50a8\u53c2\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/40a096fd-2818-4f41-ba23-7974d05bd7f7.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u722c\u866b\u5982\u4f55\u6784\u9020\u5b57\u5178\u5b58\u50a8\u53c2\u6570\" \/><\/p>\n<p><p> <strong>Python\u722c\u866b\u6784\u9020\u5b57\u5178\u5b58\u50a8\u53c2\u6570\u7684\u6838\u5fc3\u8981\u70b9\u5305\u62ec\uff1a\u4f7f\u7528requests\u5e93\u53d1\u9001\u8bf7\u6c42\u3001\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u3001\u6784\u9020\u5b57\u5178\u5b58\u50a8\u722c\u53d6\u7684\u6570\u636e\u3001\u5229\u7528\u5b57\u5178\u7684\u952e\u503c\u5bf9\u6765\u7ec4\u7ec7\u6570\u636e\u3002\u901a\u8fc7\u6784\u9020\u5b57\u5178\u5b58\u50a8\u53c2\u6570\uff0c\u53ef\u4ee5\u4f7f\u6570\u636e\u7ed3\u6784\u66f4\u52a0\u6e05\u6670\u3001\u65b9\u4fbf\u540e\u7eed\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528requests\u5e93\u53d1\u9001\u8bf7\u6c42<\/p>\n<\/p>\n<p><p>\u5728Python\u722c\u866b\u4e2d\uff0crequests\u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684HTTP\u8bf7\u6c42\u5e93\u3002\u5b83\u7b80\u5355\u6613\u7528\uff0c\u529f\u80fd\u5f3a\u5927\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8f7b\u677e\u5730\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u5e76\u83b7\u53d6\u670d\u52a1\u5668\u7684\u54cd\u5e94\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &#39;http:\/\/example.com&#39;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>if response.status_code == 200:<\/p>\n<p>    print(&quot;Request was successful&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Request f<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86requests\u5e93\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e2a\u76ee\u6807URL\uff0c\u5e76\u4f7f\u7528requests.get()\u65b9\u6cd5\u53d1\u9001GET\u8bf7\u6c42\u3002\u5982\u679c\u8bf7\u6c42\u6210\u529f\uff0c\u6211\u4eec\u4f1a\u6536\u5230\u72b6\u6001\u7801200\uff0c\u5e76\u53ef\u4ee5\u901a\u8fc7response.text\u83b7\u53d6\u54cd\u5e94\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML<\/p>\n<\/p>\n<p><p>BeautifulSoup\u662f\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u6587\u6863\u7684Python\u5e93\u3002\u901a\u8fc7BeautifulSoup\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece\u7f51\u9875\u4e2d\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u5b57\u5178\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>html_content = response.text<\/p>\n<p>soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u4f8b\u5982\uff0c\u63d0\u53d6\u6240\u6709\u7684\u6807\u9898\u548c\u94fe\u63a5<\/strong><\/h2>\n<p>titles = soup.find_all(&#39;h1&#39;)<\/p>\n<p>links = soup.find_all(&#39;a&#39;)<\/p>\n<p>for title in titles:<\/p>\n<p>    print(title.text)<\/p>\n<p>for link in links:<\/p>\n<p>    print(link.get(&#39;href&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u54cd\u5e94\u5185\u5bb9\u4f20\u9012\u7ed9BeautifulSoup\uff0c\u5e76\u6307\u5b9a\u89e3\u6790\u5668\u4e3a&#39;html.parser&#39;\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528find_all()\u65b9\u6cd5\u67e5\u627e\u6240\u6709\u7684\u6807\u9898\u548c\u94fe\u63a5\uff0c\u5e76\u904d\u5386\u5b83\u4eec\u4ee5\u63d0\u53d6\u6587\u672c\u5185\u5bb9\u548c\u94fe\u63a5\u5730\u5740\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u6784\u9020\u5b57\u5178\u5b58\u50a8\u722c\u53d6\u7684\u6570\u636e<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u6784\u9020\u5b57\u5178\u5b58\u50a8\u722c\u53d6\u7684\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u7ec4\u7ec7\u548c\u7ba1\u7406\u6570\u636e\u3002\u5b57\u5178\u662f\u4e00\u79cd\u952e\u503c\u5bf9\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5177\u6709\u5feb\u901f\u67e5\u627e\u548c\u63d2\u5165\u7684\u4f18\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {<\/p>\n<p>    &#39;titles&#39;: [],<\/p>\n<p>    &#39;links&#39;: []<\/p>\n<p>}<\/p>\n<p>for title in titles:<\/p>\n<p>    data[&#39;titles&#39;].append(title.text)<\/p>\n<p>for link in links:<\/p>\n<p>    data[&#39;links&#39;].append(link.get(&#39;href&#39;))<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178data\uff0c\u5e76\u5b9a\u4e49\u4e24\u4e2a\u952e&#39;titles&#39;\u548c&#39;links&#39;\uff0c\u5206\u522b\u7528\u4e8e\u5b58\u50a8\u6807\u9898\u548c\u94fe\u63a5\u3002\u7136\u540e\uff0c\u6211\u4eec\u904d\u5386\u6807\u9898\u548c\u94fe\u63a5\uff0c\u5c06\u63d0\u53d6\u5230\u7684\u5185\u5bb9\u6dfb\u52a0\u5230\u76f8\u5e94\u7684\u5217\u8868\u4e2d\u3002\u6700\u540e\uff0c\u6211\u4eec\u8f93\u51fa\u5b57\u5178data\uff0c\u67e5\u770b\u5b58\u50a8\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5229\u7528\u5b57\u5178\u7684\u952e\u503c\u5bf9\u6765\u7ec4\u7ec7\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u7684\u952e\u503c\u5bf9\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7ec4\u7ec7\u548c\u7ba1\u7406\u6570\u636e\u3002\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5b9a\u4e49\u591a\u4e2a\u952e\uff0c\u5e76\u5c06\u76f8\u5173\u6570\u636e\u5b58\u50a8\u5728\u76f8\u5e94\u7684\u952e\u4e0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {<\/p>\n<p>    &#39;titles&#39;: [],<\/p>\n<p>    &#39;links&#39;: [],<\/p>\n<p>    &#39;descriptions&#39;: []<\/p>\n<p>}<\/p>\n<p>descriptions = soup.find_all(&#39;p&#39;)<\/p>\n<p>for title in titles:<\/p>\n<p>    data[&#39;titles&#39;].append(title.text)<\/p>\n<p>for link in links:<\/p>\n<p>    data[&#39;links&#39;].append(link.get(&#39;href&#39;))<\/p>\n<p>for description in descriptions:<\/p>\n<p>    data[&#39;descriptions&#39;].append(description.text)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5728\u5b57\u5178data\u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u7684\u952e&#39;descriptions&#39;\uff0c\u7528\u4e8e\u5b58\u50a8\u63cf\u8ff0\u5185\u5bb9\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528BeautifulSoup\u67e5\u627e\u6240\u6709\u7684\u63cf\u8ff0\uff0c\u5e76\u5c06\u63d0\u53d6\u5230\u7684\u6587\u672c\u5185\u5bb9\u6dfb\u52a0\u5230&#39;descriptions&#39;\u5217\u8868\u4e2d\u3002\u6700\u7ec8\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6253\u5370\u5b57\u5178data\uff0c\u67e5\u770b\u5b58\u50a8\u7684\u6240\u6709\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u7ed3\u5408\u591a\u4e2a\u53c2\u6570\u6784\u9020\u590d\u6742\u5b57\u5178<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u722c\u866b\u53ef\u80fd\u9700\u8981\u5904\u7406\u66f4\u52a0\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u591a\u4e2a\u53c2\u6570\uff0c\u6784\u9020\u590d\u6742\u7684\u5b57\u5178\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u6570\u636e\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = []<\/p>\n<p>articles = soup.find_all(&#39;article&#39;)<\/p>\n<p>for article in articles:<\/p>\n<p>    title = article.find(&#39;h1&#39;).text<\/p>\n<p>    link = article.find(&#39;a&#39;).get(&#39;href&#39;)<\/p>\n<p>    description = article.find(&#39;p&#39;).text<\/p>\n<p>    article_data = {<\/p>\n<p>        &#39;title&#39;: title,<\/p>\n<p>        &#39;link&#39;: link,<\/p>\n<p>        &#39;description&#39;: description<\/p>\n<p>    }<\/p>\n<p>    data.append(article_data)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868data\uff0c\u7528\u4e8e\u5b58\u50a8\u591a\u4e2a\u6587\u7ae0\u7684\u6570\u636e\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528BeautifulSoup\u67e5\u627e\u6240\u6709\u7684\u6587\u7ae0\uff0c\u5e76\u904d\u5386\u6bcf\u7bc7\u6587\u7ae0\uff0c\u63d0\u53d6\u6807\u9898\u3001\u94fe\u63a5\u548c\u63cf\u8ff0\u3002\u63a5\u7740\uff0c\u6211\u4eec\u5c06\u6bcf\u7bc7\u6587\u7ae0\u7684\u6570\u636e\u5b58\u50a8\u5728\u4e00\u4e2a\u5b57\u5178article_data\u4e2d\uff0c\u5e76\u5c06\u8be5\u5b57\u5178\u6dfb\u52a0\u5230\u5217\u8868data\u4e2d\u3002\u6700\u7ec8\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6253\u5370\u5217\u8868data\uff0c\u67e5\u770b\u5b58\u50a8\u7684\u6240\u6709\u6587\u7ae0\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5904\u7406\u52a8\u6001\u7f51\u9875<\/p>\n<\/p>\n<p><p>\u6709\u4e9b\u7f51\u9875\u5185\u5bb9\u662f\u901a\u8fc7JavaScript\u52a8\u6001\u52a0\u8f7d\u7684\uff0c\u4f7f\u7528requests\u5e93\u65e0\u6cd5\u76f4\u63a5\u83b7\u53d6\u8fd9\u4e9b\u5185\u5bb9\u3002\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Selenium\u7b49\u5de5\u5177\uff0c\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\uff0c\u83b7\u53d6\u52a8\u6001\u52a0\u8f7d\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>url = &#39;http:\/\/example.com&#39;<\/p>\n<p>driver = webdriver.Chrome()<\/p>\n<p>driver.get(url)<\/p>\n<p>html_content = driver.page_source<\/p>\n<p>soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u4e4b\u540e\u53ef\u4ee5\u6309\u7167\u524d\u9762\u4ecb\u7ecd\u7684\u65b9\u6cd5\u89e3\u6790\u548c\u5b58\u50a8\u6570\u636e<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86Selenium\u7684webdriver\u6a21\u5757\uff0c\u5e76\u4f7f\u7528webdriver.Chrome()\u521b\u5efa\u4e00\u4e2aChrome\u6d4f\u89c8\u5668\u5b9e\u4f8b\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528get()\u65b9\u6cd5\u6253\u5f00\u76ee\u6807URL\uff0c\u5e76\u901a\u8fc7page_source\u83b7\u53d6\u9875\u9762\u7684HTML\u5185\u5bb9\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u6309\u7167\u524d\u9762\u4ecb\u7ecd\u7684\u65b9\u6cd5\uff0c\u4f7f\u7528BeautifulSoup\u89e3\u6790\u548c\u5b58\u50a8\u6570\u636e\u3002\u6700\u540e\uff0c\u6211\u4eec\u5173\u95ed\u6d4f\u89c8\u5668\u5b9e\u4f8b\uff0c\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u5904\u7406\u5206\u9875\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u722c\u866b\u53ef\u80fd\u9700\u8981\u5904\u7406\u5206\u9875\u6570\u636e\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6784\u9020\u5faa\u73af\uff0c\u53d1\u9001\u591a\u4e2a\u8bf7\u6c42\uff0c\u83b7\u53d6\u6240\u6709\u5206\u9875\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = []<\/p>\n<p>for page in range(1, 6):<\/p>\n<p>    url = f&#39;http:\/\/example.com\/page\/{page}&#39;<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        html_content = response.text<\/p>\n<p>        soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<\/p>\n<p>        articles = soup.find_all(&#39;article&#39;)<\/p>\n<p>        for article in articles:<\/p>\n<p>            title = article.find(&#39;h1&#39;).text<\/p>\n<p>            link = article.find(&#39;a&#39;).get(&#39;href&#39;)<\/p>\n<p>            description = article.find(&#39;p&#39;).text<\/p>\n<p>            article_data = {<\/p>\n<p>                &#39;title&#39;: title,<\/p>\n<p>                &#39;link&#39;: link,<\/p>\n<p>                &#39;description&#39;: description<\/p>\n<p>            }<\/p>\n<p>            data.append(article_data)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2afor\u5faa\u73af\uff0c\u904d\u5386\u6bcf\u4e00\u9875\u7684URL\uff0c\u5e76\u53d1\u9001GET\u8bf7\u6c42\u83b7\u53d6\u54cd\u5e94\u5185\u5bb9\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\uff0c\u5e76\u63d0\u53d6\u6bcf\u9875\u7684\u6587\u7ae0\u6570\u636e\uff0c\u5b58\u50a8\u5728\u5b57\u5178\u4e2d\uff0c\u6700\u7ec8\u5c06\u6240\u6709\u6570\u636e\u5b58\u50a8\u5728\u5217\u8868data\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u4e2a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u6784\u9020\u4e00\u4e2a\u5b8c\u6574\u7684Python\u722c\u866b\uff0c\u5229\u7528\u5b57\u5178\u5b58\u50a8\u53c2\u6570\uff0c\u8f7b\u677e\u5730\u83b7\u53d6\u548c\u7ec4\u7ec7\u7f51\u9875\u6570\u636e\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u722c\u866b\u4e2d\u6709\u6548\u6784\u9020\u5b57\u5178\u4ee5\u5b58\u50a8\u8bf7\u6c42\u53c2\u6570\uff1f<\/strong><br \/>\u5728Python\u722c\u866b\u4e2d\uff0c\u6784\u9020\u5b57\u5178\u7528\u4e8e\u5b58\u50a8\u8bf7\u6c42\u53c2\u6570\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u505a\u6cd5\u3002\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u952e\u503c\u5bf9\u6765\u5b9a\u4e49\u6240\u9700\u7684\u53c2\u6570\uff0c\u4f8b\u5982\u4f7f\u7528<code>requests<\/code>\u5e93\u65f6\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\u6765\u5b58\u50a8URL\u8bf7\u6c42\u7684\u67e5\u8be2\u53c2\u6570\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">params = {\n    &#39;key1&#39;: &#39;value1&#39;,\n    &#39;key2&#39;: &#39;value2&#39;\n}\nresponse = requests.get(&#39;https:\/\/example.com\/api&#39;, params=params)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u5b57\u5178\u4e2d\u5b9a\u4e49\u7684\u952e\u503c\u5bf9\u5c06\u81ea\u52a8\u9644\u52a0\u5230\u8bf7\u6c42\u7684URL\u4e2d\u3002<\/p>\n<p><strong>\u5728\u5b57\u5178\u4e2d\u5b58\u50a8\u590d\u6742\u6570\u636e\u7ed3\u6784\u65f6\u6709\u54ea\u4e9b\u6280\u5de7\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5728\u5b57\u5178\u4e2d\u5b58\u50a8\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u5217\u8868\u6216\u5d4c\u5957\u5b57\u5178\uff0c\u53ef\u4ee5\u76f4\u63a5\u5c06\u5b83\u4eec\u4f5c\u4e3a\u5b57\u5178\u7684\u503c\u3002\u8fd9\u6837\u53ef\u4ee5\u5e2e\u52a9\u7ec4\u7ec7\u6570\u636e\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">params = {\n    &#39;user&#39;: {\n        &#39;id&#39;: 123,\n        &#39;name&#39;: &#39;John Doe&#39;\n    },\n    &#39;tags&#39;: [&#39;python&#39;, &#39;web scraping&#39;]\n}\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u4fdd\u6301\u4e86\u6570\u636e\u7684\u7ed3\u6784\u6027\uff0c\u8fd8\u6709\u52a9\u4e8e\u5728\u5904\u7406\u8bf7\u6c42\u65f6\u8f7b\u677e\u8bbf\u95ee\u548c\u4fee\u6539\u53c2\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5b57\u5178\u4e2d\u7684\u52a8\u6001\u53c2\u6570\uff1f<\/strong><br \/>\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u722c\u866b\u9700\u8981\u6839\u636e\u7528\u6237\u8f93\u5165\u6216\u5176\u4ed6\u6761\u4ef6\u52a8\u6001\u751f\u6210\u8bf7\u6c42\u53c2\u6570\u3002\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6216\u5faa\u73af\u6765\u6784\u9020\u5b57\u5178\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">params = {}\nif user_input:\n    params[&#39;search&#39;] = user_input\nif date_filter:\n    params[&#39;date&#39;] = date_filter\nresponse = requests.get(&#39;https:\/\/example.com\/search&#39;, params=params)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u4e86\u5b57\u5178\u4e2d\u53ea\u5305\u542b\u5b9e\u9645\u9700\u8981\u7684\u53c2\u6570\uff0c\u907f\u514d\u4e86\u65e0\u6548\u7684\u8bf7\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u722c\u866b\u6784\u9020\u5b57\u5178\u5b58\u50a8\u53c2\u6570\u7684\u6838\u5fc3\u8981\u70b9\u5305\u62ec\uff1a\u4f7f\u7528requests\u5e93\u53d1\u9001\u8bf7\u6c42\u3001\u4f7f\u7528BeautifulSoup [&hellip;]","protected":false},"author":3,"featured_media":1054165,"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\/1054164"}],"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=1054164"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1054164\/revisions"}],"predecessor-version":[{"id":1054166,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1054164\/revisions\/1054166"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1054165"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1054164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1054164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1054164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}