{"id":792,"date":"2024-11-07T10:58:27","date_gmt":"2024-11-07T02:58:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/792\/"},"modified":"2024-11-07T10:58:27","modified_gmt":"2024-11-07T02:58:27","slug":"python%e6%80%8e%e4%b9%88%e8%b8%a9%e7%88%ac%e8%99%ab","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/792\/","title":{"rendered":"python\u600e\u4e48\u8e29\u722c\u866b"},"content":{"rendered":"<blockquote><p>\n  \u4f7f\u7528 python \u521b\u5efa\u722c\u866b\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a\u5b89\u88c5 requests \u5e93\u521b\u5efa python \u811a\u672c\u5e76\u5bfc\u5165 requests \u5e93\u5b9a\u4e49\u76ee\u6807 url\u53d1\u9001 http get \u8bf7\u6c42\u5230\u76ee\u6807 url\u4f7f\u7528 beautifulsoup \u6216 lxml \u7b49\u5e93\u89e3\u6790\u8bf7\u6c42\u54cd\u5e94\u4ece html \u4e2d\u63d0\u53d6\u6240\u9700\u6570\u636e\u5b58\u50a8\u6216\u5904\u7406\u63d0\u53d6\u7684\u6570\u636e\n<\/p><\/blockquote>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1112\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024101814122085860.jpg\" width=\"800\" height=\"320\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024101814122085860.jpg 800w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024101814122085860-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024101814122085860-768x307.jpg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/2024101814122085860-670x268.jpg 670w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" title=\"python\u600e\u4e48\u8e29\u722c\u866b\u63d2\u56fe\" alt=\"python\u600e\u4e48\u8e29\u722c\u866b\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Python \u521b\u5efa\u722c\u866b<\/strong><\/p>\n<p>Python \u662f\u4e00\u79cd\u529f\u80fd\u5f3a\u5927\u7684\u8bed\u8a00\uff0c\u975e\u5e38\u9002\u5408\u521b\u5efa\u7f51\u7edc\u722c\u866b\u3002\u722c\u866b\u662f\u81ea\u52a8\u5316\u5de5\u5177\uff0c\u7528\u4e8e\u4ece\u7f51\u7ad9\u63d0\u53d6\u4fe1\u606f\u3002<\/p>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<p><strong>1. \u5b89\u88c5\u8bf7\u6c42\u5e93<\/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>pip install requests<\/p>\n<p><strong>2. \u521b\u5efa\u722c\u866b<\/strong><\/p>\n<p>\u521b\u5efa\u4e00\u4e2a Python \u811a\u672c\uff0c\u5bfc\u5165\u8bf7\u6c42\u5e93\u3002<\/p>\n<pre>import requests<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u5b9a\u4e49\u76ee\u6807 URL<\/strong><\/p>\n<p>\u6307\u5b9a\u60a8\u8981\u6293\u53d6\u6570\u636e\u7684\u76ee\u6807\u7f51\u7ad9\u7684 URL\u3002<\/p>\n<pre>target_url = 'https:\/\/www.example.com'<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u53d1\u9001\u8bf7\u6c42<\/strong><\/p>\n<p>\u53d1\u9001\u4e00\u4e2a HTTP GET \u8bf7\u6c42\u5230\u76ee\u6807 URL\u3002<\/p>\n<pre>response = requests.get(target_url)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u89e3\u6790\u54cd\u5e94<\/strong><\/p>\n<p>\u89e3\u6790\u8bf7\u6c42\u54cd\u5e94\u4ee5\u83b7\u53d6\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528 BeautifulSoup \u6216 lxml \u7b49\u5e93\u6765\u89e3\u6790 HTML\u3002<\/p>\n<pre>from bs4 import BeautifulSoup\nsoup = BeautifulSoup(response.text, 'html.parser')<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>6. \u63d0\u53d6\u6570\u636e<\/strong><\/p>\n<p>\u4f7f\u7528 soup \u5bf9\u8c61\u4ece HTML \u4e2d\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u3002<\/p>\n<pre>titles = soup.find_all('h1')\nfor title in titles:\n    print(title.text)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>7. \u5b58\u50a8\u6216\u5904\u7406\u6570\u636e<\/strong><\/p>\n<p>\u60a8\u53ef\u4ee5\u5c06\u63d0\u53d6\u7684\u6570\u636e\u5b58\u50a8\u5230\u6570\u636e\u5e93\u4e2d\uff0c\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u6216\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u4f7f\u7528 Python \u521b\u5efa\u722c\u866b\u7684\u793a\u4f8b\uff0c\u5b83\u4ece Stack Overflow \u9996\u9875\u6293\u53d6\u95ee\u9898\u6807\u9898\uff1a<\/p>\n<pre>import requests\nfrom bs4 import BeautifulSoup\n\ntarget_url = 'https:\/\/stackoverflow.com'\nresponse = requests.get(target_url)\nsoup = BeautifulSoup(response.text, 'html.parser')\n\ntitles = soup.find_all('h3')\nfor title in titles:\n    print(title.text)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fpython\u600e\u4e48\u8e29\u722c\u866b\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>\u4f7f\u7528 python \u521b\u5efa\u722c\u866b\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a\u5b89\u88c5 requests \u5e93\u521b\u5efa python \u811a\u672c\u5e76\u5bfc\u5165 requests \u5e93\u5b9a\u4e49\u76ee\u6807 url\u53d1\u9001 http get \u8bf7\u6c42\u5230\u76ee\u6807 url\u4f7f\u7528 beautifulsoup \u6216 lxml \u7b49\u5e93\u89e3\u6790\u8bf7\u6c42\u54cd\u5e94\u4ece html \u4e2d\u63d0\u53d6\u6240\u9700\u6570\u636e\u5b58\u50a8\u6216\u5904\u7406\u63d0\u53d6\u7684\u6570\u636e \u5982\u4f55\u4f7f\u7528 Python \u521b\u5efa\u722c\u866b Python \u662f\u4e00\u79cd\u529f\u80fd\u5f3a\u5927\u7684\u8bed\u8a00\uff0c\u975e\u5e38\u9002\u5408\u521b\u5efa\u7f51\u7edc\u722c\u866b\u3002\u722c\u866b\u662f\u81ea\u52a8\u5316\u5de5\u5177\uff0c\u7528\u4e8e\u4ece\u7f51\u7ad9\u63d0\u53d6\u4fe1\u606f\u3002 \u6b65\u9aa4\uff1a 1. \u5b89\u88c5\u8bf7\u6c42\u5e93 \u7acb\u5373\u5b66\u4e60\u201cPython\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09\u201d\uff1b pip install requests 2. \u521b\u5efa\u722c\u866b \u521b\u5efa\u4e00\u4e2a Python \u811a\u672c\uff0c\u5bfc\u5165\u8bf7\u6c42\u5e93\u3002 import requests \u767b\u5f55\u540e\u590d\u5236 3. \u5b9a\u4e49\u76ee\u6807 URL \u6307\u5b9a\u60a8\u8981\u6293\u53d6\u6570\u636e\u7684\u76ee\u6807\u7f51\u7ad9\u7684 URL\u3002 target_url = &#8216;https:\/\/www.example.com&#8217; \u767b\u5f55\u540e\u590d\u5236 4. \u53d1\u9001\u8bf7\u6c42 \u53d1\u9001\u4e00\u4e2a HTTP GET \u8bf7\u6c42\u5230\u76ee\u6807 [&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-792","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/792","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=792"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/792\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}