{"id":799,"date":"2024-11-07T10:49:19","date_gmt":"2024-11-07T02:49:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/799\/"},"modified":"2024-11-07T10:49:19","modified_gmt":"2024-11-07T02:49:19","slug":"python%e7%88%ac%e8%99%ab%e6%8a%93%e7%99%be%e5%ba%a6%e6%80%8e%e4%b9%88%e6%8a%93","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/799\/","title":{"rendered":"Python\u722c\u866b\u6293\u767e\u5ea6\u600e\u4e48\u6293"},"content":{"rendered":"<blockquote><p>\n  \u8981\u4f7f\u7528python\u722c\u866b\u6293\u53d6\u767e\u5ea6\uff0c\u9700\u8981\u5b89\u88c5beautifulsoup\u3001requests\u548clxml\u5e93\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\u3002\u4f7f\u7528beautifulsoup\u89e3\u6790html\u3002\u4f7f\u7528requests\u53d1\u9001http\u8bf7\u6c42\u3002\u9010\u6b65\u722c\u53d6\u767e\u5ea6\u7f51\u9875\uff08\u63d0\u53d6\u6807\u9898\u548c\u9ed8\u8ba4\u641c\u7d22\u8bcd\uff09\u3002\u5904\u7406\u767e\u5ea6\u53cd\u722c\u866b\u63aa\u65bd\uff08\u5982\u4f7f\u7528\u4ee3\u7406\u6216\u9a8c\u8bc1\u7801\u7834\u89e3\u6280\u672f\uff09\u3002\n<\/p><\/blockquote>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1105\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024101822332421282.jpg\" width=\"800\" height=\"320\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024101822332421282.jpg 800w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024101822332421282-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024101822332421282-768x307.jpg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024101822332421282-670x268.jpg 670w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" title=\"Python\u722c\u866b\u6293\u767e\u5ea6\u600e\u4e48\u6293\u63d2\u56fe\" alt=\"Python\u722c\u866b\u6293\u767e\u5ea6\u600e\u4e48\u6293\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u722c\u866b\u6293\u53d6\u767e\u5ea6<\/strong><\/p>\n<p><strong>\u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/strong><\/p>\n<p>\u8981\u4f7f\u7528Python\u722c\u866b\u6293\u53d6\u767e\u5ea6\uff0c\u4f60\u9700\u8981\u5b89\u88c5\u4ee5\u4e0b\u5e93\uff1a<\/p>\n<ul>\n<li>BeautifulSoup<\/li>\n<li>requests<\/li>\n<li>lxml<\/li>\n<\/ul>\n<p>\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201cPython\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09\u201d\uff1b<\/p>\n<pre>pip install beautifulsoup4 requests lxml<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4e8c\u3001\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML<\/strong><\/p>\n<p>BeautifulSoup\u662f\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790HTML\u7684\u5e93\u3002\u5b83\u53ef\u4ee5\u5c06HTML\u6587\u6863\u8f6c\u6362\u4e3a\u53ef\u7528\u7684\u6811\u5f62\u7ed3\u6784\uff0c\u4ee5\u4fbf\u4e8e\u63d0\u53d6\u548c\u64cd\u4f5c\u6570\u636e\u3002<\/p>\n<p><strong>\u4e09\u3001\u4f7f\u7528requests\u53d1\u9001HTTP\u8bf7\u6c42<\/strong><\/p>\n<p>requests\u662f\u4e00\u4e2a\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\u7684\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5411\u767e\u5ea6\u7f51\u7ad9\u53d1\u9001\u8bf7\u6c42\uff0c\u83b7\u53d6\u8981\u6293\u53d6\u7684\u7f51\u9875\u5185\u5bb9\u3002<\/p>\n<p><strong>\u56db\u3001\u9010\u6b65\u722c\u53d6\u767e\u5ea6<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u9010\u6b65\u6293\u53d6\u767e\u5ea6\u7f51\u9875\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>import requests\nfrom bs4 import BeautifulSoup\n\n# \u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u83b7\u53d6\u767e\u5ea6\u9996\u9875\u5185\u5bb9\nresponse = requests.get(\"https:\/\/www.baidu.com\")\n\n# \u89e3\u6790HTML\u6587\u6863\nsoup = BeautifulSoup(response.text, \"lxml\")\n\n# \u63d0\u53d6\u6807\u9898\ntitle = soup.find(\"title\").text\nprint(f\"\u6807\u9898\uff1a{title}\")\n\n# \u63d0\u53d6\u641c\u7d22\u6846\u4e2d\u7684\u9ed8\u8ba4\u641c\u7d22\u8bcd\nsearch_input = soup.find(\"input\", {\"id\": \"kw\"})\ndefault_search_term = search_input[\"value\"]\nprint(f\"\u641c\u7d22\u6846\u4e2d\u7684\u9ed8\u8ba4\u641c\u7d22\u8bcd\uff1a{default_search_term}\")<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4e94\u3001\u5904\u7406\u53cd\u722c\u866b\u63aa\u65bd<\/strong><\/p>\n<p>\u767e\u5ea6\u91c7\u53d6\u4e86\u53cd\u722c\u866b\u63aa\u65bd\uff0c\u56e0\u6b64\u5728\u6293\u53d6\u65f6\u53ef\u80fd\u4f1a\u9047\u5230\u6311\u6218\u3002\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528\u4ee3\u7406\u3001\u6d4f\u89c8\u5668\u5934\u4fe1\u606f\u6216\u9a8c\u8bc1\u7801\u7834\u89e3\u6280\u672f\u6765\u7ed5\u8fc7\u8fd9\u4e9b\u63aa\u65bd\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fPython\u722c\u866b\u6293\u767e\u5ea6\u600e\u4e48\u6293\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>\u8981\u4f7f\u7528python\u722c\u866b\u6293\u53d6\u767e\u5ea6\uff0c\u9700\u8981\u5b89\u88c5beautifulsoup\u3001requests\u548clxml\u5e93\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\u3002\u4f7f\u7528beautifulsoup\u89e3\u6790html\u3002\u4f7f\u7528requests\u53d1\u9001http\u8bf7\u6c42\u3002\u9010\u6b65\u722c\u53d6\u767e\u5ea6\u7f51\u9875\uff08\u63d0\u53d6\u6807\u9898\u548c\u9ed8\u8ba4\u641c\u7d22\u8bcd\uff09\u3002\u5904\u7406\u767e\u5ea6\u53cd\u722c\u866b\u63aa\u65bd\uff08\u5982\u4f7f\u7528\u4ee3\u7406\u6216\u9a8c\u8bc1\u7801\u7834\u89e3\u6280\u672f\uff09\u3002 \u5982\u4f55\u4f7f\u7528Python\u722c\u866b\u6293\u53d6\u767e\u5ea6 \u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93 \u8981\u4f7f\u7528Python\u722c\u866b\u6293\u53d6\u767e\u5ea6\uff0c\u4f60\u9700\u8981\u5b89\u88c5\u4ee5\u4e0b\u5e93\uff1a BeautifulSoup requests lxml \u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff1a \u7acb\u5373\u5b66\u4e60\u201cPython\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09\u201d\uff1b pip install beautifulsoup4 requests lxml \u767b\u5f55\u540e\u590d\u5236 \u4e8c\u3001\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML BeautifulSoup\u662f\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790HTML\u7684\u5e93\u3002\u5b83\u53ef\u4ee5\u5c06HTML\u6587\u6863\u8f6c\u6362\u4e3a\u53ef\u7528\u7684\u6811\u5f62\u7ed3\u6784\uff0c\u4ee5\u4fbf\u4e8e\u63d0\u53d6\u548c\u64cd\u4f5c\u6570\u636e\u3002 \u4e09\u3001\u4f7f\u7528requests\u53d1\u9001HTTP\u8bf7\u6c42 requests\u662f\u4e00\u4e2a\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\u7684\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5411\u767e\u5ea6\u7f51\u7ad9\u53d1\u9001\u8bf7\u6c42\uff0c\u83b7\u53d6\u8981\u6293\u53d6\u7684\u7f51\u9875\u5185\u5bb9\u3002 \u56db\u3001\u9010\u6b65\u722c\u53d6\u767e\u5ea6 \u4ee5\u4e0b\u662f\u4e00\u4e2a\u9010\u6b65\u6293\u53d6\u767e\u5ea6\u7f51\u9875\u7684\u793a\u4f8b\u4ee3\u7801\uff1a import requests from bs4 import BeautifulSoup # \u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u83b7\u53d6\u767e\u5ea6\u9996\u9875\u5185\u5bb9 response = requests.get(&#8220;https:\/\/www.baidu.com&#8221;) # \u89e3\u6790HTML\u6587\u6863 soup = BeautifulSoup(response.text, &#8220;lxml&#8221;) # \u63d0\u53d6\u6807\u9898 title = soup.find(&#8220;title&#8221;).text print(f&#8221;\u6807\u9898\uff1a{title}&#8221;) # \u63d0\u53d6\u641c\u7d22\u6846\u4e2d\u7684\u9ed8\u8ba4\u641c\u7d22\u8bcd search_input = soup.find(&#8220;input&#8221;, {&#8220;id&#8221;: &#8220;kw&#8221;}) default_search_term = search_input[&#8220;value&#8221;] print(f&#8221;\u641c\u7d22\u6846\u4e2d\u7684\u9ed8\u8ba4\u641c\u7d22\u8bcd\uff1a{default_search_term}&#8221;) [&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-799","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/799","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=799"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/799\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}