{"id":1077313,"date":"2025-01-08T12:01:13","date_gmt":"2025-01-08T04:01:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1077313.html"},"modified":"2025-01-08T12:01:16","modified_gmt":"2025-01-08T04:01:16","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%88%ac%e4%b8%a4%e4%b8%aa%e7%bd%91%e9%a1%b5-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1077313.html","title":{"rendered":"\u5982\u4f55\u7528python\u722c\u4e24\u4e2a\u7f51\u9875"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181429\/3f3e8601-89e9-429e-b2e5-637a4278aa6b.webp\" alt=\"\u5982\u4f55\u7528python\u722c\u4e24\u4e2a\u7f51\u9875\" \/><\/p>\n<p><p> <strong>\u7528Python\u722c\u53d6\u4e24\u4e2a\u7f51\u9875\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u9009\u62e9\u5408\u9002\u7684\u5e93\u3001\u53d1\u9001HTTP\u8bf7\u6c42\u3001\u89e3\u6790HTML\u5185\u5bb9\u3001\u63d0\u53d6\u6240\u9700\u6570\u636e\u3002<\/strong> \u5176\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u662f\u5173\u952e\uff0c\u53ef\u4ee5\u9009\u62e9requests\u5e93\u548cBeautifulSoup\u5e93\u6765\u5b8c\u6210\u722c\u53d6\u4efb\u52a1\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u722c\u53d6\u4e24\u4e2a\u7f51\u9875\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u9009\u62e9\u5408\u9002\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u5f88\u591a\u5e93\u53ef\u4ee5\u7528\u6765\u722c\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u5c31\u662frequests\u5e93\u548cBeautifulSoup\u5e93\u3002requests\u5e93\u53ef\u4ee5\u7528\u6765\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u800cBeautifulSoup\u5e93\u5219\u53ef\u4ee5\u7528\u6765\u89e3\u6790\u548c\u63d0\u53d6HTML\u5185\u5bb9\u3002\u901a\u8fc7\u8fd9\u4e24\u4e2a\u5e93\u7684\u7ed3\u5408\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u722c\u53d6\u7f51\u9875\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p><strong>1. requests\u5e93<\/strong><\/p>\n<p>requests\u5e93\u662f\u4e00\u4e2a\u7b80\u5355\u4e14\u529f\u80fd\u5f3a\u5927\u7684HTTP\u5e93\uff0c\u4f7f\u7528requests\u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u5e76\u83b7\u53d6\u670d\u52a1\u5668\u7684\u54cd\u5e94\u5185\u5bb9\u3002\u5b83\u652f\u6301GET\u3001POST\u7b49\u5404\u79cdHTTP\u65b9\u6cd5\uff0c\u5e76\u4e14\u53ef\u4ee5\u5904\u7406Cookies\u548c\u4f1a\u8bdd\u3002<\/p>\n<\/p>\n<p><p><strong>2. BeautifulSoup\u5e93<\/strong><\/p>\n<p>BeautifulSoup\u5e93\u662f\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u6587\u6863\u7684\u5e93\uff0c\u5b83\u53ef\u4ee5\u5c06\u590d\u6742\u7684HTML\u6587\u6863\u8f6c\u5316\u4e3a\u4e00\u4e2a\u53ef\u4f9b\u904d\u5386\u7684\u6811\u5f62\u7ed3\u6784\u3002\u901a\u8fc7BeautifulSoup\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u63d0\u53d6\u7f51\u9875\u4e2d\u7684\u5404\u79cd\u5143\u7d20\uff0c\u6bd4\u5982\u6807\u9898\u3001\u6bb5\u843d\u3001\u94fe\u63a5\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u53d1\u9001HTTP\u8bf7\u6c42<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u597d\u5408\u9002\u7684\u5e93\u4e4b\u540e\uff0c\u63a5\u4e0b\u6765\u5c31\u9700\u8981\u53d1\u9001HTTP\u8bf7\u6c42\u6765\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u3002\u901a\u8fc7requests\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u53d1\u9001GET\u8bf7\u6c42\uff0c\u5e76\u83b7\u53d6\u7f51\u9875\u7684HTML\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u53d1\u9001GET\u8bf7\u6c42\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<\/strong><\/h2>\n<p>url1 = &#39;https:\/\/example.com\/page1&#39;<\/p>\n<p>response1 = requests.get(url1)<\/p>\n<p>url2 = &#39;https:\/\/example.com\/page2&#39;<\/p>\n<p>response2 = requests.get(url2)<\/p>\n<h2><strong>\u68c0\u67e5\u8bf7\u6c42\u662f\u5426\u6210\u529f<\/strong><\/h2>\n<p>if response1.status_code == 200:<\/p>\n<p>    html_content1 = response1.text<\/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 page 1&quot;)<\/p>\n<p>if response2.status_code == 200:<\/p>\n<p>    html_content2 = response2.text<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Failed to retrieve page 2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7requests.get()\u51fd\u6570\u53d1\u9001GET\u8bf7\u6c42\uff0c\u5e76\u83b7\u53d6\u4e24\u4e2a\u7f51\u9875\u7684HTML\u5185\u5bb9\u3002\u901a\u8fc7\u68c0\u67e5\u54cd\u5e94\u7684\u72b6\u6001\u7801\uff0c\u53ef\u4ee5\u5224\u65ad\u8bf7\u6c42\u662f\u5426\u6210\u529f\u3002\u6210\u529f\u7684\u8bdd\uff0c\u5c31\u53ef\u4ee5\u83b7\u53d6\u7f51\u9875\u7684HTML\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u89e3\u6790HTML\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u5728\u83b7\u53d6\u5230\u7f51\u9875\u7684HTML\u5185\u5bb9\u4e4b\u540e\uff0c\u63a5\u4e0b\u6765\u5c31\u9700\u8981\u4f7f\u7528BeautifulSoup\u5e93\u6765\u89e3\u6790\u8fd9\u4e9b\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u89e3\u6790HTML\u5185\u5bb9<\/strong><\/h2>\n<p>soup1 = BeautifulSoup(html_content1, &#39;html.parser&#39;)<\/p>\n<p>soup2 = BeautifulSoup(html_content2, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u7f51\u9875\u4e2d\u7684\u5143\u7d20<\/strong><\/h2>\n<p>title1 = soup1.title.string<\/p>\n<p>title2 = soup2.title.string<\/p>\n<p>print(&quot;Page 1 Title:&quot;, title1)<\/p>\n<p>print(&quot;Page 2 Title:&quot;, title2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7BeautifulSoup\u7c7b\u521b\u5efa\u4e86\u4e24\u4e2a\u89e3\u6790\u5bf9\u8c61soup1\u548csoup2\uff0c\u7136\u540e\u901a\u8fc7soup1.title.string\u548csoup2.title.string\u63d0\u53d6\u4e86\u4e24\u4e2a\u7f51\u9875\u7684\u6807\u9898\uff0c\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u63d0\u53d6\u6240\u9700\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u89e3\u6790HTML\u5185\u5bb9\uff0c\u6211\u4eec\u53ef\u4ee5\u63d0\u53d6\u7f51\u9875\u4e2d\u7684\u5404\u79cd\u5143\u7d20\uff0c\u6bd4\u5982\u6807\u9898\u3001\u6bb5\u843d\u3001\u94fe\u63a5\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u63d0\u53d6\u7f51\u9875\u4e2d\u7684\u6240\u6709\u94fe\u63a5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u63d0\u53d6\u7f51\u9875\u4e2d\u7684\u6240\u6709\u94fe\u63a5<\/p>\n<p>links1 = soup1.find_all(&#39;a&#39;)<\/p>\n<p>links2 = soup2.find_all(&#39;a&#39;)<\/p>\n<p>print(&quot;Page 1 Links:&quot;)<\/p>\n<p>for link in links1:<\/p>\n<p>    print(link.get(&#39;href&#39;))<\/p>\n<p>print(&quot;Page 2 Links:&quot;)<\/p>\n<p>for link in links2:<\/p>\n<p>    print(link.get(&#39;href&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7soup1.find_all(&#39;a&#39;)\u548csoup2.find_all(&#39;a&#39;)\u63d0\u53d6\u4e86\u4e24\u4e2a\u7f51\u9875\u4e2d\u7684\u6240\u6709\u94fe\u63a5\uff0c\u5e76\u9010\u4e2a\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u4e2a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u722c\u53d6\u4e24\u4e2a\u7f51\u9875\u7684\u5185\u5bb9\u3002\u9996\u5148\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u6bd4\u5982requests\u5e93\u548cBeautifulSoup\u5e93\uff1b\u7136\u540e\uff0c\u53d1\u9001HTTP\u8bf7\u6c42\u83b7\u53d6\u7f51\u9875\u7684HTML\u5185\u5bb9\uff1b\u63a5\u7740\uff0c\u89e3\u6790HTML\u5185\u5bb9\uff0c\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u3002\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u722c\u53d6\u7f51\u9875\u6570\u636e\uff0c\u5e76\u8fdb\u884c\u540e\u7eed\u7684\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5b9e\u6218\uff1a\u722c\u53d6\u4e24\u4e2a\u5177\u4f53\u7f51\u9875<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u7528Python\u722c\u53d6\u4e24\u4e2a\u7f51\u9875\uff0c\u4e0b\u9762\u5c06\u4ee5\u5177\u4f53\u7684\u4f8b\u5b50\u8fdb\u884c\u6f14\u793a\u3002\u6211\u4eec\u5c06\u722c\u53d6\u4e24\u4e2a\u793a\u4f8b\u7f51\u9875\uff0c\u5e76\u63d0\u53d6\u5176\u4e2d\u7684\u6807\u9898\u548c\u6240\u6709\u7684\u94fe\u63a5\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u6240\u9700\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86requests\u548cBeautifulSoup\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p>pip install beautifulsoup4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u722c\u53d6\u7b2c\u4e00\u4e2a\u7f51\u9875<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u722c\u53d6\u7b2c\u4e00\u4e2a\u7f51\u9875\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u53d1\u9001GET\u8bf7\u6c42\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<\/strong><\/h2>\n<p>url1 = &#39;https:\/\/example.com\/page1&#39;<\/p>\n<p>response1 = requests.get(url1)<\/p>\n<h2><strong>\u68c0\u67e5\u8bf7\u6c42\u662f\u5426\u6210\u529f<\/strong><\/h2>\n<p>if response1.status_code == 200:<\/p>\n<p>    html_content1 = response1.text<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Failed to retrieve page 1&quot;)<\/p>\n<h2><strong>\u89e3\u6790HTML\u5185\u5bb9<\/strong><\/h2>\n<p>soup1 = BeautifulSoup(html_content1, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u7f51\u9875\u4e2d\u7684\u6807\u9898<\/strong><\/h2>\n<p>title1 = soup1.title.string<\/p>\n<p>print(&quot;Page 1 Title:&quot;, title1)<\/p>\n<h2><strong>\u63d0\u53d6\u7f51\u9875\u4e2d\u7684\u6240\u6709\u94fe\u63a5<\/strong><\/h2>\n<p>links1 = soup1.find_all(&#39;a&#39;)<\/p>\n<p>print(&quot;Page 1 Links:&quot;)<\/p>\n<p>for link in links1:<\/p>\n<p>    print(link.get(&#39;href&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u722c\u53d6\u7b2c\u4e8c\u4e2a\u7f51\u9875<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u722c\u53d6\u7b2c\u4e8c\u4e2a\u7f51\u9875\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u53d1\u9001GET\u8bf7\u6c42\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<\/strong><\/h2>\n<p>url2 = &#39;https:\/\/example.com\/page2&#39;<\/p>\n<p>response2 = requests.get(url2)<\/p>\n<h2><strong>\u68c0\u67e5\u8bf7\u6c42\u662f\u5426\u6210\u529f<\/strong><\/h2>\n<p>if response2.status_code == 200:<\/p>\n<p>    html_content2 = response2.text<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Failed to retrieve page 2&quot;)<\/p>\n<h2><strong>\u89e3\u6790HTML\u5185\u5bb9<\/strong><\/h2>\n<p>soup2 = BeautifulSoup(html_content2, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u7f51\u9875\u4e2d\u7684\u6807\u9898<\/strong><\/h2>\n<p>title2 = soup2.title.string<\/p>\n<p>print(&quot;Page 2 Title:&quot;, title2)<\/p>\n<h2><strong>\u63d0\u53d6\u7f51\u9875\u4e2d\u7684\u6240\u6709\u94fe\u63a5<\/strong><\/h2>\n<p>links2 = soup2.find_all(&#39;a&#39;)<\/p>\n<p>print(&quot;Page 2 Links:&quot;)<\/p>\n<p>for link in links2:<\/p>\n<p>    print(link.get(&#39;href&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u7efc\u5408\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u65b9\u4fbf\u5730\u5c55\u793a\uff0c\u53ef\u4ee5\u5c06\u4ee5\u4e0a\u4e24\u4e2a\u6b65\u9aa4\u7684\u4ee3\u7801\u7efc\u5408\u5230\u4e00\u8d77\uff0c\u5f62\u6210\u4e00\u4e2a\u5b8c\u6574\u7684Python\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u5b9a\u4e49\u8981\u722c\u53d6\u7684\u7f51\u9875URL<\/strong><\/h2>\n<p>url1 = &#39;https:\/\/example.com\/page1&#39;<\/p>\n<p>url2 = &#39;https:\/\/example.com\/page2&#39;<\/p>\n<h2><strong>\u53d1\u9001GET\u8bf7\u6c42\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<\/strong><\/h2>\n<p>response1 = requests.get(url1)<\/p>\n<p>response2 = requests.get(url2)<\/p>\n<h2><strong>\u68c0\u67e5\u8bf7\u6c42\u662f\u5426\u6210\u529f\uff0c\u5e76\u89e3\u6790HTML\u5185\u5bb9<\/strong><\/h2>\n<p>if response1.status_code == 200:<\/p>\n<p>    html_content1 = response1.text<\/p>\n<p>    soup1 = BeautifulSoup(html_content1, &#39;html.parser&#39;)<\/p>\n<p>    title1 = soup1.title.string<\/p>\n<p>    print(&quot;Page 1 Title:&quot;, title1)<\/p>\n<p>    links1 = soup1.find_all(&#39;a&#39;)<\/p>\n<p>    print(&quot;Page 1 Links:&quot;)<\/p>\n<p>    for link in links1:<\/p>\n<p>        print(link.get(&#39;href&#39;))<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Failed to retrieve page 1&quot;)<\/p>\n<p>if response2.status_code == 200:<\/p>\n<p>    html_content2 = response2.text<\/p>\n<p>    soup2 = BeautifulSoup(html_content2, &#39;html.parser&#39;)<\/p>\n<p>    title2 = soup2.title.string<\/p>\n<p>    print(&quot;Page 2 Title:&quot;, title2)<\/p>\n<p>    links2 = soup2.find_all(&#39;a&#39;)<\/p>\n<p>    print(&quot;Page 2 Links:&quot;)<\/p>\n<p>    for link in links2:<\/p>\n<p>        print(link.get(&#39;href&#39;))<\/p>\n<p>else:<\/p>\n<p>    print(&quot;Failed to retrieve page 2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u52a8\u6001\u7f51\u9875<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u7f51\u9875\u4e2d\u7684\u5185\u5bb9\u662f\u901a\u8fc7JavaScript\u52a8\u6001\u52a0\u8f7d\u7684\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0crequests\u5e93\u53ef\u80fd\u65e0\u6cd5\u83b7\u53d6\u5230\u5b8c\u6574\u7684\u7f51\u9875\u5185\u5bb9\u3002\u5904\u7406\u52a8\u6001\u7f51\u9875\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Selenium\u5e93\uff0c\u5b83\u53ef\u4ee5\u6a21\u62df\u6d4f\u89c8\u5668\u7684\u884c\u4e3a\uff0c\u6267\u884cJavaScript\u4ee3\u7801\uff0c\u5e76\u83b7\u53d6\u52a8\u6001\u52a0\u8f7d\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Selenium\u5e93<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Selenium\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4e0b\u8f7d\u6d4f\u89c8\u5668\u9a71\u52a8<\/h4>\n<\/p>\n<p><p>Selenium\u9700\u8981\u4f7f\u7528\u6d4f\u89c8\u5668\u9a71\u52a8\u6765\u63a7\u5236\u6d4f\u89c8\u5668\uff0c\u53ef\u4ee5\u6839\u636e\u4f7f\u7528\u7684\u6d4f\u89c8\u5668\u4e0b\u8f7d\u76f8\u5e94\u7684\u9a71\u52a8\u7a0b\u5e8f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Chrome\u6d4f\u89c8\u5668\u65f6\uff0c\u53ef\u4ee5\u4e0b\u8f7dChromeDriver\u3002<\/p>\n<\/p>\n<p><h4>3. \u4f7f\u7528Selenium\u722c\u53d6\u52a8\u6001\u7f51\u9875<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528Selenium\u722c\u53d6\u52a8\u6001\u7f51\u9875\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u8bbe\u7f6eChrome\u6d4f\u89c8\u5668\u9a71\u52a8\u8def\u5f84<\/strong><\/h2>\n<p>driver_path = &#39;\/path\/to\/chromedriver&#39;<\/p>\n<h2><strong>\u521b\u5efaChrome\u6d4f\u89c8\u5668\u5b9e\u4f8b<\/strong><\/h2>\n<p>driver = webdriver.Chrome(executable_path=driver_path)<\/p>\n<h2><strong>\u8bbf\u95ee\u7f51\u9875<\/strong><\/h2>\n<p>url = &#39;https:\/\/example.com\/dynamic_page&#39;<\/p>\n<p>driver.get(url)<\/p>\n<h2><strong>\u83b7\u53d6\u7f51\u9875\u7684HTML\u5185\u5bb9<\/strong><\/h2>\n<p>html_content = driver.page_source<\/p>\n<h2><strong>\u89e3\u6790HTML\u5185\u5bb9<\/strong><\/h2>\n<p>soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u7f51\u9875\u4e2d\u7684\u6807\u9898<\/strong><\/h2>\n<p>title = soup.title.string<\/p>\n<p>print(&quot;Page Title:&quot;, title)<\/p>\n<h2><strong>\u63d0\u53d6\u7f51\u9875\u4e2d\u7684\u6240\u6709\u94fe\u63a5<\/strong><\/h2>\n<p>links = soup.find_all(&#39;a&#39;)<\/p>\n<p>print(&quot;Page Links:&quot;)<\/p>\n<p>for link in links:<\/p>\n<p>    print(link.get(&#39;href&#39;))<\/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>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u8f7b\u677e\u5730\u722c\u53d6\u4e24\u4e2a\u7f51\u9875\u7684\u5185\u5bb9\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u3001\u53d1\u9001HTTP\u8bf7\u6c42\u3001\u89e3\u6790HTML\u5185\u5bb9\u3001\u63d0\u53d6\u6240\u9700\u6570\u636e\uff0c\u8fd9\u4e9b\u6b65\u9aa4\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u9ad8\u6548\u5730\u83b7\u53d6\u7f51\u9875\u6570\u636e\u3002\u5bf9\u4e8e\u52a8\u6001\u7f51\u9875\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Selenium\u5e93\u6765\u5904\u7406\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1Python\u7f51\u9875\u722c\u53d6\u7684\u6280\u5de7\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u63d0\u9ad8\u722c\u53d6\u6548\u7387\u548c\u5904\u7406\u53cd\u722c\u866b\u673a\u5236<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u722c\u53d6\u5927\u91cf\u7f51\u9875\u65f6\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u8003\u8651\u63d0\u9ad8\u722c\u53d6\u6548\u7387\u548c\u5904\u7406\u53cd\u722c\u866b\u673a\u5236\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\u548c\u6280\u5de7\uff1a<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u722c\u53d6\u6548\u7387\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u6765\u5e76\u53d1\u722c\u53d6\u591a\u4e2a\u7f51\u9875\u3002Python\u4e2d\u7684Threading\u548cMultiprocessing\u6a21\u5757\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u5e76\u53d1\u722c\u53d6\u3002<\/p>\n<\/p>\n<p><h4>2. \u8bbe\u7f6e\u8bf7\u6c42\u5934\u548c\u4ee3\u7406<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u907f\u514d\u88ab\u53cd\u722c\u866b\u673a\u5236\u68c0\u6d4b\u5230\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u8bf7\u6c42\u5934\uff08\u5982User-Agent\uff09\u548c\u4f7f\u7528\u4ee3\u7406\u3002\u8bf7\u6c42\u5934\u53ef\u4ee5\u4f2a\u88c5\u6210\u6d4f\u89c8\u5668\u8bf7\u6c42\uff0c\u800c\u4ee3\u7406\u53ef\u4ee5\u9690\u85cf\u6211\u4eec\u7684\u771f\u5b9eIP\u5730\u5740\u3002<\/p>\n<\/p>\n<p><h4>3. \u9075\u5b88\u7f51\u7ad9\u7684robots.txt\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728\u722c\u53d6\u7f51\u9875\u4e4b\u524d\uff0c\u5e94\u8be5\u68c0\u67e5\u7f51\u7ad9\u7684robots.txt\u6587\u4ef6\uff0c\u786e\u4fdd\u9075\u5b88\u7f51\u7ad9\u7684\u722c\u53d6\u89c4\u5219\u3002robots.txt\u6587\u4ef6\u89c4\u5b9a\u4e86\u54ea\u4e9b\u9875\u9762\u53ef\u4ee5\u88ab\u722c\u53d6\uff0c\u54ea\u4e9b\u9875\u9762\u4e0d\u53ef\u4ee5\u88ab\u722c\u53d6\u3002<\/p>\n<\/p>\n<p><h4>4. \u9650\u5236\u722c\u53d6\u901f\u5ea6<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u907f\u514d\u7ed9\u76ee\u6807\u7f51\u7ad9\u5e26\u6765\u8fc7\u5927\u7684\u8d1f\u62c5\uff0c\u53ef\u4ee5\u9650\u5236\u722c\u53d6\u901f\u5ea6\u3002\u53ef\u4ee5\u5728\u6bcf\u6b21\u8bf7\u6c42\u4e4b\u95f4\u8bbe\u7f6e\u9002\u5f53\u7684\u5ef6\u8fdf\uff08\u5982\u4f7f\u7528time.sleep()\u51fd\u6570\uff09\u3002<\/p>\n<\/p>\n<p><h4>5. \u5904\u7406\u5f02\u5e38\u548c\u9519\u8bef<\/h4>\n<\/p>\n<p><p>\u5728\u722c\u53d6\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\u548c\u9519\u8bef\uff08\u5982\u7f51\u7edc\u8fde\u63a5\u9519\u8bef\u3001\u8bf7\u6c42\u8d85\u65f6\u7b49\uff09\u3002\u9700\u8981\u7f16\u5199\u5065\u58ee\u7684\u4ee3\u7801\u6765\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u548c\u9519\u8bef\uff0c\u786e\u4fdd\u722c\u53d6\u4efb\u52a1\u80fd\u591f\u987a\u5229\u8fdb\u884c\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u53ef\u4ee5\u63d0\u9ad8\u722c\u53d6\u6548\u7387\uff0c\u5e76\u6709\u6548\u5730\u5904\u7406\u53cd\u722c\u866b\u673a\u5236\uff0c\u786e\u4fdd\u722c\u53d6\u4efb\u52a1\u7684\u987a\u5229\u8fdb\u884c\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u638c\u63e1Python\u7f51\u9875\u722c\u53d6\u7684\u6280\u5de7\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4f7f\u7528Python\u722c\u53d6\u7f51\u9875\u9700\u8981\u54ea\u4e9b\u57fa\u672c\u7684\u5e93\u548c\u5de5\u5177\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5e38\u7528\u7684\u5e93\u5305\u62ecRequests\u548cBeautifulSoup\u3002Requests\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u4ee5\u83b7\u53d6\u7f51\u9875\u7684HTML\u5185\u5bb9\uff0c\u800cBeautifulSoup\u5219\u7528\u4e8e\u89e3\u6790HTML\u6587\u6863\uff0c\u5e76\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u3002\u6b64\u5916\uff0cScrapy\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u6846\u67b6\uff0c\u9002\u5408\u8fdb\u884c\u590d\u6742\u7684\u7f51\u9875\u722c\u53d6\u4efb\u52a1\u3002<\/p>\n<p><strong>\u5728\u722c\u53d6\u7f51\u9875\u65f6\uff0c\u5982\u4f55\u5904\u7406\u53cd\u722c\u866b\u63aa\u65bd\uff1f<\/strong><br \/>\u53cd\u722c\u866b\u63aa\u65bd\u53ef\u80fd\u4f1a\u5bfc\u81f4\u60a8\u7684\u722c\u866b\u88ab\u5c01\u7981\u3002\u4e3a\u4e86\u5e94\u5bf9\u8fd9\u4e9b\u63aa\u65bd\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee3\u7406IP\u3001\u8bbe\u7f6e\u8bf7\u6c42\u5934\uff08\u5982User-Agent\uff09\u4ee5\u6a21\u62df\u6d4f\u89c8\u5668\u8bf7\u6c42\u3001\u9002\u5f53\u589e\u52a0\u8bf7\u6c42\u95f4\u9694\u4ee5\u964d\u4f4e\u8bbf\u95ee\u9891\u7387\uff0c\u751a\u81f3\u4f7f\u7528\u52a8\u6001\u722c\u866b\u5de5\u5177\u5982Selenium\u6765\u5904\u7406\u9700\u8981JavaScript\u52a0\u8f7d\u7684\u9875\u9762\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u722c\u53d6\u7684\u6570\u636e\u7b26\u5408\u7f51\u7ad9\u7684\u4f7f\u7528\u6761\u6b3e\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u7f51\u9875\u722c\u53d6\u4e4b\u524d\uff0c\u5efa\u8bae\u4ed4\u7ec6\u9605\u8bfb\u76ee\u6807\u7f51\u7ad9\u7684robots.txt\u6587\u4ef6\u4ee5\u53ca\u76f8\u5173\u7684\u4f7f\u7528\u6761\u6b3e\u3002robots.txt\u6587\u4ef6\u4f1a\u544a\u8bc9\u60a8\u54ea\u4e9b\u90e8\u5206\u662f\u5141\u8bb8\u722c\u53d6\u7684\uff0c\u54ea\u4e9b\u90e8\u5206\u662f\u7981\u6b62\u7684\u3002\u9075\u5faa\u8fd9\u4e9b\u89c4\u5b9a\uff0c\u4e0d\u4ec5\u80fd\u591f\u907f\u514d\u6cd5\u5f8b\u95ee\u9898\uff0c\u4e5f\u80fd\u4fdd\u62a4\u60a8\u7684\u722c\u866b\u9879\u76ee\u7684\u53ef\u6301\u7eed\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u722c\u53d6\u4e24\u4e2a\u7f51\u9875\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u9009\u62e9\u5408\u9002\u7684\u5e93\u3001\u53d1\u9001HTTP\u8bf7\u6c42\u3001\u89e3\u6790HTML\u5185\u5bb9\u3001\u63d0\u53d6\u6240\u9700\u6570\u636e [&hellip;]","protected":false},"author":3,"featured_media":1077321,"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\/1077313"}],"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=1077313"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1077313\/revisions"}],"predecessor-version":[{"id":1077322,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1077313\/revisions\/1077322"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1077321"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1077313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1077313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1077313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}