{"id":798,"date":"2024-11-07T15:19:12","date_gmt":"2024-11-07T07:19:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/798\/"},"modified":"2024-11-07T15:19:12","modified_gmt":"2024-11-07T07:19:12","slug":"python%e7%88%ac%e8%99%ab%e6%80%8e%e4%b9%88%e8%a7%a3%e6%9e%90%e7%bd%91%e9%a1%b5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/798\/","title":{"rendered":"python\u722c\u866b\u600e\u4e48\u89e3\u6790\u7f51\u9875"},"content":{"rendered":"<blockquote><p>\n  \u7f51\u9875\u89e3\u6790\u662f\u6307\u63d0\u53d6\u7f51\u9875\u4e2d\u7ed3\u6784\u5316\u6570\u636e\u7684\u8fc7\u7a0b\u3002python\u722c\u866b\u89e3\u6790\u7f51\u9875\u7684\u6b65\u9aa4\u5305\u62ec\uff1a1. \u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff1b2. \u521b\u5efa\u89e3\u6790\u5668\uff1b3. \u63d0\u53d6\u6570\u636e\uff1b4. \u6e05\u7406\u548c\u5904\u7406\u6570\u636e\u3002\u5e38\u7528\u7684\u89e3\u6790\u5e93\u6709beautifulsoup\u3001lxml\u548chtml5lib\u3002\n<\/p><\/blockquote>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1106\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024101817154668277.jpg\" width=\"800\" height=\"320\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024101817154668277.jpg 800w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024101817154668277-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024101817154668277-768x307.jpg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024101817154668277-670x268.jpg 670w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" title=\"python\u722c\u866b\u600e\u4e48\u89e3\u6790\u7f51\u9875\u63d2\u56fe\" alt=\"python\u722c\u866b\u600e\u4e48\u89e3\u6790\u7f51\u9875\u63d2\u56fe\" \/><\/p>\n<p><strong>Python\u722c\u866b\u5982\u4f55\u89e3\u6790\u7f51\u9875<\/strong><\/p>\n<p><strong>\u4ec0\u4e48\u662f\u7f51\u9875\u89e3\u6790\uff1f<\/strong><\/p>\n<p>\u7f51\u9875\u89e3\u6790\u662f\u6307\u63d0\u53d6\u7f51\u9875\u4e2d\u7ed3\u6784\u5316\u6570\u636e\u7684\u8fc7\u7a0b\uff0c\u4ee5\u4fbf\u5bf9\u5176\u8fdb\u884c\u5206\u6790\u3001\u5b58\u50a8\u6216\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n<p><strong>Python\u722c\u866b\u89e3\u6790\u7f51\u9875\u7684\u6b65\u9aa4<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201cPython\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09\u201d\uff1b<\/p>\n<p>\u4f7f\u7528Python\u722c\u866b\u89e3\u6790\u7f51\u9875\u901a\u5e38\u6d89\u53ca\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li> <strong>\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff1a<\/strong>\u4f7f\u7528requests\u6216BeautifulSoup\u7b49\u5e93\u4ece\u76ee\u6807\u7f51\u9875\u83b7\u53d6HTML\u5185\u5bb9\u3002<\/li>\n<li> <strong>\u521b\u5efa\u89e3\u6790\u5668\uff1a<\/strong>\u4f7f\u7528BeautifulSoup\u6216\u5176\u4ed6\u5e93\u521b\u5efa\u4e00\u4e2a\u89e3\u6790\u5668\uff0c\u5b83\u5c06HTML\u5185\u5bb9\u89e3\u6790\u4e3a\u53ef\u904d\u5386\u7684\u6587\u6863\u6811\u3002<\/li>\n<li> <strong>\u63d0\u53d6\u6570\u636e\uff1a<\/strong>\u4f7f\u7528\u89e3\u6790\u5668\u7684\u67e5\u627e\u65b9\u6cd5\uff08\u5982find_all\u548cfind\uff09\u4ece\u6587\u6863\u6811\u4e2d\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u3002<\/li>\n<li> <strong>\u6e05\u7406\u548c\u5904\u7406\u6570\u636e\uff1a<\/strong>\u5bf9\u63d0\u53d6\u7684\u6570\u636e\u8fdb\u884c\u6e05\u7406\u548c\u5904\u7406\uff0c\u4ee5\u4f7f\u5176\u5904\u4e8e\u6240\u9700\u683c\u5f0f\uff0c\u4f8b\u5982\u5220\u9664\u4e0d\u9700\u8981\u7684\u6807\u7b7e\u6216\u8f6c\u6362\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<\/ol>\n<p><strong>\u5e38\u7528\u7684\u89e3\u6790\u5e93<\/strong><\/p>\n<ul>\n<li> <strong>BeautifulSoup\uff1a<\/strong>\u4e00\u4e2a\u6d41\u884c\u7684HTML\u548cXML\u89e3\u6790\u5e93\uff0c\u63d0\u4f9b\u76f4\u89c2\u4e14\u5f3a\u5927\u7684\u67e5\u627e\u65b9\u6cd5\u3002<\/li>\n<li> <strong>lxml\uff1a<\/strong>\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684XML\u548cHTML\u89e3\u6790\u5e93\uff0c\u652f\u6301XPath\u8868\u8fbe\u5f0f\u3002<\/li>\n<li> <strong>html5lib\uff1a<\/strong>\u4e00\u4e2a\u57fa\u4e8e\u6807\u51c6\u7684HTML5\u89e3\u6790\u5668\uff0c\u53ef\u4ee5\u5904\u7406\u4e0d\u7b26\u5408\u6807\u51c6\u7684HTML\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\u6f14\u793a\u4e86\u4f7f\u7528BeautifulSoup\u89e3\u6790\u7f51\u9875\u5e76\u63d0\u53d6\u6570\u636e\uff1a<\/p>\n<pre>import requests\nfrom bs4 import BeautifulSoup\n\n# \u83b7\u53d6\u7f51\u9875\u5185\u5bb9\nurl = \"https:\/\/example.com\"\nresponse = requests.get(url)\nhtml = response.content\n\n# \u521b\u5efa\u89e3\u6790\u5668\nsoup = BeautifulSoup(html, 'html.parser')\n\n# \u63d0\u53d6\u6570\u636e\ntitle = soup.find('title').string\nlinks = [a['href'] for a in soup.find_all('a')]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5176\u4ed6\u63d0\u793a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528XPath\u6216CSS\u9009\u62e9\u5668\u53ef\u4ee5\u66f4\u7cbe\u786e\u5730\u63d0\u53d6\u6570\u636e\u3002<\/li>\n<li>\u5bf9\u4e8e\u590d\u6742\u7684\u7f51\u9875\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u591a\u4e2a\u89e3\u6790\u5668\u6216\u7ec4\u5408\u4e0d\u540c\u7684\u89e3\u6790\u6280\u672f\u3002<\/li>\n<li>\u6ce8\u610f\u7f51\u9875\u4e2d\u7684\u52a8\u6001\u5185\u5bb9\uff0c\u5b83\u53ef\u80fd\u9700\u8981\u989d\u5916\u7684\u5904\u7406\u624d\u80fd\u6b63\u786e\u89e3\u6790\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fpython\u722c\u866b\u600e\u4e48\u89e3\u6790\u7f51\u9875\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7f51\u9875\u89e3\u6790\u662f\u6307\u63d0\u53d6\u7f51\u9875\u4e2d\u7ed3\u6784\u5316\u6570\u636e\u7684\u8fc7\u7a0b\u3002python\u722c\u866b\u89e3\u6790\u7f51\u9875\u7684\u6b65\u9aa4\u5305\u62ec\uff1a1. \u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff1b2. \u521b\u5efa\u89e3\u6790\u5668\uff1b3. \u63d0\u53d6\u6570\u636e\uff1b4. \u6e05\u7406\u548c\u5904\u7406\u6570\u636e\u3002\u5e38\u7528\u7684\u89e3\u6790\u5e93\u6709beautifulsoup\u3001lxml\u548chtml5lib\u3002 Python\u722c\u866b\u5982\u4f55\u89e3\u6790\u7f51\u9875 \u4ec0\u4e48\u662f\u7f51\u9875\u89e3\u6790\uff1f \u7f51\u9875\u89e3\u6790\u662f\u6307\u63d0\u53d6\u7f51\u9875\u4e2d\u7ed3\u6784\u5316\u6570\u636e\u7684\u8fc7\u7a0b\uff0c\u4ee5\u4fbf\u5bf9\u5176\u8fdb\u884c\u5206\u6790\u3001\u5b58\u50a8\u6216\u8fdb\u4e00\u6b65\u5904\u7406\u3002 Python\u722c\u866b\u89e3\u6790\u7f51\u9875\u7684\u6b65\u9aa4 \u7acb\u5373\u5b66\u4e60\u201cPython\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09\u201d\uff1b \u4f7f\u7528Python\u722c\u866b\u89e3\u6790\u7f51\u9875\u901a\u5e38\u6d89\u53ca\u4ee5\u4e0b\u6b65\u9aa4\uff1a \u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff1a\u4f7f\u7528requests\u6216BeautifulSoup\u7b49\u5e93\u4ece\u76ee\u6807\u7f51\u9875\u83b7\u53d6HTML\u5185\u5bb9\u3002 \u521b\u5efa\u89e3\u6790\u5668\uff1a\u4f7f\u7528BeautifulSoup\u6216\u5176\u4ed6\u5e93\u521b\u5efa\u4e00\u4e2a\u89e3\u6790\u5668\uff0c\u5b83\u5c06HTML\u5185\u5bb9\u89e3\u6790\u4e3a\u53ef\u904d\u5386\u7684\u6587\u6863\u6811\u3002 \u63d0\u53d6\u6570\u636e\uff1a\u4f7f\u7528\u89e3\u6790\u5668\u7684\u67e5\u627e\u65b9\u6cd5\uff08\u5982find_all\u548cfind\uff09\u4ece\u6587\u6863\u6811\u4e2d\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u3002 \u6e05\u7406\u548c\u5904\u7406\u6570\u636e\uff1a\u5bf9\u63d0\u53d6\u7684\u6570\u636e\u8fdb\u884c\u6e05\u7406\u548c\u5904\u7406\uff0c\u4ee5\u4f7f\u5176\u5904\u4e8e\u6240\u9700\u683c\u5f0f\uff0c\u4f8b\u5982\u5220\u9664\u4e0d\u9700\u8981\u7684\u6807\u7b7e\u6216\u8f6c\u6362\u6570\u636e\u7c7b\u578b\u3002 \u5e38\u7528\u7684\u89e3\u6790\u5e93 BeautifulSoup\uff1a\u4e00\u4e2a\u6d41\u884c\u7684HTML\u548cXML\u89e3\u6790\u5e93\uff0c\u63d0\u4f9b\u76f4\u89c2\u4e14\u5f3a\u5927\u7684\u67e5\u627e\u65b9\u6cd5\u3002 lxml\uff1a\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684XML\u548cHTML\u89e3\u6790\u5e93\uff0c\u652f\u6301XPath\u8868\u8fbe\u5f0f\u3002 html5lib\uff1a\u4e00\u4e2a\u57fa\u4e8e\u6807\u51c6\u7684HTML5\u89e3\u6790\u5668\uff0c\u53ef\u4ee5\u5904\u7406\u4e0d\u7b26\u5408\u6807\u51c6\u7684HTML\u3002 \u793a\u4f8b\u4ee3\u7801 \u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\u6f14\u793a\u4e86\u4f7f\u7528BeautifulSoup\u89e3\u6790\u7f51\u9875\u5e76\u63d0\u53d6\u6570\u636e\uff1a import requests from bs4 import BeautifulSoup # \u83b7\u53d6\u7f51\u9875\u5185\u5bb9 url = &#8220;https:\/\/example.com&#8221; response = requests.get(url) html = response.content # \u521b\u5efa\u89e3\u6790\u5668 soup = BeautifulSoup(html, &#8216;html.parser&#8217;) # \u63d0\u53d6\u6570\u636e title = soup.find(&#8216;title&#8217;).string links = [a[&#8216;href&#8217;] for a in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-798","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/798","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=798"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/798\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}