{"id":1119921,"date":"2025-01-08T18:49:18","date_gmt":"2025-01-08T10:49:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1119921.html"},"modified":"2025-01-08T18:49:26","modified_gmt":"2025-01-08T10:49:26","slug":"%e5%a6%82%e4%bd%95%e7%88%ac%e5%8f%96%e4%ba%8c%e7%ba%a7%e7%bd%91%e7%ab%99%e6%95%b0%e6%8d%aepython","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1119921.html","title":{"rendered":"\u5982\u4f55\u722c\u53d6\u4e8c\u7ea7\u7f51\u7ad9\u6570\u636epython"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082823\/ee4239a7-0a99-4340-b0e6-3fbc557c3dc3.webp\" alt=\"\u5982\u4f55\u722c\u53d6\u4e8c\u7ea7\u7f51\u7ad9\u6570\u636epython\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u722c\u53d6\u4e8c\u7ea7\u7f51\u7ad9\u6570\u636ePython<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u4f7f\u7528Python\u722c\u53d6\u4e8c\u7ea7\u7f51\u7ad9\u7684\u6570\u636e\uff0c\u5173\u952e\u6b65\u9aa4\u5305\u62ec<strong>\u9009\u62e9\u5408\u9002\u7684\u5e93\u3001\u8bbe\u7f6e\u8bf7\u6c42\u5934\u9632\u6b62\u88ab\u5c01\u7981\u3001\u89e3\u6790HTML\u83b7\u53d6\u6570\u636e\u3001\u5904\u7406\u5206\u9875\u548c\u52a8\u6001\u5185\u5bb9<\/strong>\u3002\u5176\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u81f3\u5173\u91cd\u8981\uff0c\u6bd4\u5982<code>requests<\/code>\u5e93\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\uff0c<code>BeautifulSoup<\/code>\u6216<code>lxml<\/code>\u5e93\u7528\u4e8e\u89e3\u6790HTML\u5185\u5bb9\u3002<strong>\u9009\u62e9\u5408\u9002\u7684\u5e93<\/strong>\u8fd9\u4e00\u6b65\u5c24\u4e3a\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u51b3\u5b9a\u4e86\u722c\u866b\u7684\u6548\u7387\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><p><strong>\u9009\u62e9\u5408\u9002\u7684\u5e93<\/strong>\uff1a\u5728Python\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u5e93\u5305\u62ec<code>requests<\/code>\u3001<code>BeautifulSoup<\/code>\u3001<code>Scrapy<\/code>\u7b49\u3002<code>requests<\/code>\u5e93\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u521d\u5b66\u8005\uff1b<code>BeautifulSoup<\/code>\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u6587\u6863\uff1b<code>Scrapy<\/code>\u5219\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u722c\u866b\u6846\u67b6\uff0c\u9002\u5408\u590d\u6742\u7684\u722c\u53d6\u4efb\u52a1\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\u51e0\u4e2a\u5e38\u7528\u7684\u5e93\u53ef\u4ee5\u7528\u6765\u722c\u53d6\u7f51\u9875\u6570\u636e\u3002\u5bf9\u4e8e\u521d\u5b66\u8005\u6765\u8bf4\uff0c<code>requests<\/code>\u548c<code>BeautifulSoup<\/code>\u662f\u6700\u5e38\u89c1\u7684\u7ec4\u5408\u3002<code>requests<\/code>\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u800c<code>BeautifulSoup<\/code>\u7528\u4e8e\u89e3\u6790HTML\u6587\u6863\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u4efb\u52a1\uff0c<code>Scrapy<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u722c\u866b\u6846\u67b6\uff0c\u53ef\u4ee5\u5904\u7406\u66f4\u590d\u6742\u7684\u722c\u53d6\u903b\u8f91\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>Requests\u5e93<\/strong>\uff1a\u8fd9\u4e2a\u5e93\u975e\u5e38\u9002\u5408\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u65e0\u8bba\u662fGET\u8fd8\u662fPOST\u8bf7\u6c42\uff0c\u90fd\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u3002\u5b83\u7684\u8bed\u6cd5\u7b80\u5355\u6613\u5b66\uff0c\u975e\u5e38\u9002\u5408\u521d\u5b66\u8005\u3002<\/li>\n<li><strong>BeautifulSoup\u5e93<\/strong>\uff1a\u8fd9\u4e2a\u5e93\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u6587\u6863\uff0c\u80fd\u591f\u8f7b\u677e\u63d0\u53d6\u7f51\u9875\u4e2d\u7684\u6570\u636e\u3002\u5b83\u652f\u6301\u591a\u79cd\u89e3\u6790\u5668\uff0c\u5982<code>html.parser<\/code>\u3001<code>lxml<\/code>\u7b49\u3002<\/li>\n<li><strong>Scrapy\u6846\u67b6<\/strong>\uff1a\u8fd9\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u722c\u866b\u6846\u67b6\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u722c\u53d6\u4efb\u52a1\u3002\u5b83\u652f\u6301\u5f02\u6b65\u8bf7\u6c42\uff0c\u80fd\u591f\u6709\u6548\u63d0\u9ad8\u722c\u53d6\u901f\u5ea6\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u8bbe\u7f6e\u8bf7\u6c42\u5934\u9632\u6b62\u88ab\u5c01\u7981<\/h3>\n<\/p>\n<p><p>\u5728\u722c\u53d6\u7f51\u9875\u65f6\uff0c\u8bbe\u7f6e\u5408\u9002\u7684\u8bf7\u6c42\u5934\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u8fd9\u4e0d\u4ec5\u53ef\u4ee5\u6a21\u4eff\u6d4f\u89c8\u5668\u7684\u884c\u4e3a\uff0c\u8fd8\u80fd\u9632\u6b62\u88ab\u7f51\u7ad9\u7684\u53cd\u722c\u673a\u5236\u5c01\u7981\u3002\u901a\u5e38\u9700\u8981\u8bbe\u7f6e<code>User-Agent<\/code>\u3001<code>Referer<\/code>\u3001<code>Cookies<\/code>\u7b49\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>User-Agent<\/strong>\uff1a\u8fd9\u4e2a\u5934\u90e8\u7528\u4e8e\u544a\u8bc9\u670d\u52a1\u5668\u5ba2\u6237\u7aef\u7684\u7c7b\u578b\u3002\u4e0d\u540c\u7684\u6d4f\u89c8\u5668\u548c\u8bbe\u5907\u6709\u4e0d\u540c\u7684User-Agent\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u5934\u90e8\u4f2a\u88c5\u6210\u4e0d\u540c\u7684\u5ba2\u6237\u7aef\u3002<\/li>\n<li><strong>Referer<\/strong>\uff1a\u8fd9\u4e2a\u5934\u90e8\u7528\u4e8e\u544a\u8bc9\u670d\u52a1\u5668\u8bf7\u6c42\u7684\u6765\u6e90\u9875\u9762\u3002\u8bbe\u7f6e\u5408\u9002\u7684Referer\u53ef\u4ee5\u589e\u52a0\u8bf7\u6c42\u7684\u53ef\u4fe1\u5ea6\u3002<\/li>\n<li><strong>Cookies<\/strong>\uff1a\u6709\u4e9b\u7f51\u7ad9\u9700\u8981\u7528\u6237\u767b\u5f55\u540e\u624d\u80fd\u8bbf\u95ee\u7279\u5b9a\u7684\u5185\u5bb9\uff0c\u8fd9\u65f6\u9700\u8981\u8bbe\u7f6eCookies\u6765\u4fdd\u6301\u4f1a\u8bdd\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u89e3\u6790HTML\u83b7\u53d6\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6\u5230\u7f51\u9875\u6570\u636e\u540e\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u89e3\u6790HTML\u6587\u6863\uff0c\u4ece\u4e2d\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u3002\u8fd9\u91cc\u53ef\u4ee5\u4f7f\u7528<code>BeautifulSoup<\/code>\u6216<code>lxml<\/code>\u5e93\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>BeautifulSoup<\/strong>\uff1a\u8fd9\u4e2a\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684API\u6765\u904d\u5386\u548c\u641c\u7d22HTML\u6587\u6863\u3002\u53ef\u4ee5\u901a\u8fc7\u6807\u7b7e\u540d\u3001\u5c5e\u6027\u3001\u6587\u672c\u5185\u5bb9\u7b49\u6765\u67e5\u627e\u5143\u7d20\u3002<\/li>\n<li><strong>lxml<\/strong>\uff1a\u8fd9\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u89e3\u6790\u5e93\uff0c\u652f\u6301XPath\u67e5\u8be2\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5904\u7406\u5927\u578b\u6587\u6863\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u5904\u7406\u5206\u9875\u548c\u52a8\u6001\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u722c\u53d6\u8fc7\u7a0b\u4e2d\uff0c\u5f88\u591a\u7f51\u7ad9\u7684\u6570\u636e\u662f\u5206\u9875\u5c55\u793a\u7684\uff0c\u6216\u8005\u901a\u8fc7JavaScript\u52a8\u6001\u52a0\u8f7d\u7684\u3002\u8fd9\u65f6\u9700\u8981\u5904\u7406\u5206\u9875\u548c\u52a8\u6001\u5185\u5bb9\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5206\u9875\u5904\u7406<\/strong>\uff1a\u5927\u591a\u6570\u7f51\u7ad9\u7684\u5206\u9875\u662f\u901a\u8fc7URL\u53c2\u6570\u5b9e\u73b0\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u5206\u6790URL\u6a21\u5f0f\u6765\u6784\u9020\u4e0d\u540c\u9875\u7801\u7684\u8bf7\u6c42\u3002<\/li>\n<li><strong>\u52a8\u6001\u5185\u5bb9<\/strong>\uff1a\u5bf9\u4e8e\u901a\u8fc7JavaScript\u52a8\u6001\u52a0\u8f7d\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Selenium<\/code>\u5e93\u6765\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\uff0c\u6216\u8005\u901a\u8fc7\u5206\u6790\u7f51\u7edc\u8bf7\u6c42\u6765\u76f4\u63a5\u83b7\u53d6\u6570\u636e\u3002<\/li>\n<\/ul>\n<p><h3>\u5b9e\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528<code>requests<\/code>\u548c<code>BeautifulSoup<\/code>\u5e93\u6765\u722c\u53d6\u4e8c\u7ea7\u7f51\u7ad9\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<h2><strong>\u8bbe\u7f6e\u8bf7\u6c42\u5934<\/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>\u53d1\u9001HTTP\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(&#39;https:\/\/example.com\/secondary-page&#39;, headers=headers)<\/p>\n<h2><strong>\u89e3\u6790HTML\u6587\u6863<\/strong><\/h2>\n<p>soup = BeautifulSoup(response.text, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u67e5\u627e\u5e76\u63d0\u53d6\u6570\u636e<\/strong><\/h2>\n<p>data = soup.find_all(&#39;div&#39;, class_=&#39;data-cont<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ner&#39;)<\/p>\n<p>for item in data:<\/p>\n<p>    print(item.text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u4f7f\u7528Python\u722c\u53d6\u4e8c\u7ea7\u7f51\u7ad9\u7684\u6570\u636e\u3002<strong>\u9009\u62e9\u5408\u9002\u7684\u5e93<\/strong>\u3001<strong>\u8bbe\u7f6e\u8bf7\u6c42\u5934\u9632\u6b62\u88ab\u5c01\u7981<\/strong>\u3001<strong>\u89e3\u6790HTML\u83b7\u53d6\u6570\u636e<\/strong>\u3001<strong>\u5904\u7406\u5206\u9875\u548c\u52a8\u6001\u5185\u5bb9<\/strong>\u662f\u5173\u952e\u6b65\u9aa4\u3002\u6bcf\u4e00\u6b65\u90fd\u6709\u5176\u91cd\u8981\u6027\u548c\u6280\u672f\u7ec6\u8282\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u8c03\u6574\u548c\u4f18\u5316\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5b9e\u73b0\u7f51\u9875\u6570\u636e\u722c\u53d6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u9002\u5408\u7684Python\u5e93\u8fdb\u884c\u4e8c\u7ea7\u7f51\u7ad9\u6570\u636e\u722c\u53d6\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u4e8c\u7ea7\u7f51\u7ad9\u6570\u636e\u722c\u53d6\u65f6\uff0c\u6709\u51e0\u4e2a\u6d41\u884c\u7684Python\u5e93\u53ef\u4ee5\u4f7f\u7528\u3002BeautifulSoup\u662f\u4e00\u4e2a\u89e3\u6790HTML\u548cXML\u6587\u6863\u7684\u5e93\uff0c\u9002\u5408\u5904\u7406\u9759\u6001\u7f51\u9875\u3002Scrapy\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u6846\u67b6\uff0c\u9002\u5408\u5927\u89c4\u6a21\u722c\u53d6\u548c\u590d\u6742\u7f51\u7ad9\u7684\u9700\u6c42\u3002Requests\u5e93\u5219\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u3002\u6839\u636e\u4f60\u7684\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u722c\u53d6\u7684\u6548\u7387\u3002<\/p>\n<p><strong>\u5728\u722c\u53d6\u4e8c\u7ea7\u7f51\u7ad9\u6570\u636e\u65f6\u5982\u4f55\u5904\u7406\u53cd\u722c\u866b\u673a\u5236\uff1f<\/strong><br \/>\u8bb8\u591a\u4e8c\u7ea7\u7f51\u7ad9\u4f1a\u5b9e\u65bd\u53cd\u722c\u866b\u673a\u5236\u4ee5\u4fdd\u62a4\u5176\u6570\u636e\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u8bf7\u6c42\u5934\u6a21\u62df\u6b63\u5e38\u7528\u6237\u7684\u6d4f\u89c8\u5668\u884c\u4e3a\uff0c\u4f7f\u7528\u4ee3\u7406IP\u6765\u9690\u85cf\u771f\u5b9eIP\u5730\u5740\uff0c\u5e76\u8bbe\u7f6e\u5408\u7406\u7684\u8bf7\u6c42\u95f4\u9694\u6765\u51cf\u5c11\u88ab\u68c0\u6d4b\u7684\u98ce\u9669\u3002\u6b64\u5916\uff0c\u91c7\u7528\u968f\u673a\u5ef6\u8fdf\u548c\u7528\u6237\u4ee3\u7406\u6c60\u4e5f\u53ef\u4ee5\u589e\u52a0\u6210\u529f\u722c\u53d6\u7684\u673a\u4f1a\u3002<\/p>\n<p><strong>\u6570\u636e\u722c\u53d6\u540e\uff0c\u5982\u4f55\u5b58\u50a8\u548c\u5904\u7406\u6293\u53d6\u5230\u7684\u6570\u636e\uff1f<\/strong><br \/>\u6293\u53d6\u5230\u7684\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b58\u50a8\u3002\u5e38\u89c1\u7684\u5b58\u50a8\u683c\u5f0f\u5305\u62ecCSV\u3001JSON\u548c\u6570\u636e\u5e93\uff08\u5982SQLite\u3001MySQL\u7b49\uff09\u3002\u9009\u62e9\u5408\u9002\u7684\u5b58\u50a8\u65b9\u5f0f\u53d6\u51b3\u4e8e\u6570\u636e\u7684\u89c4\u6a21\u548c\u540e\u7eed\u5904\u7406\u9700\u6c42\u3002\u6570\u636e\u5b58\u50a8\u540e\uff0c\u53ef\u4ee5\u5229\u7528Pandas\u7b49\u5e93\u8fdb\u884c\u6e05\u6d17\u548c\u5206\u6790\uff0c\u786e\u4fdd\u6570\u636e\u7684\u51c6\u786e\u6027\u548c\u53ef\u7528\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u722c\u53d6\u4e8c\u7ea7\u7f51\u7ad9\u6570\u636ePython \u8981\u4f7f\u7528Python\u722c\u53d6\u4e8c\u7ea7\u7f51\u7ad9\u7684\u6570\u636e\uff0c\u5173\u952e\u6b65\u9aa4\u5305\u62ec\u9009\u62e9\u5408\u9002\u7684\u5e93\u3001\u8bbe\u7f6e\u8bf7\u6c42\u5934\u9632 [&hellip;]","protected":false},"author":3,"featured_media":1119941,"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\/1119921"}],"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=1119921"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1119921\/revisions"}],"predecessor-version":[{"id":1119945,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1119921\/revisions\/1119945"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1119941"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1119921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1119921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1119921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}