{"id":1098674,"date":"2025-01-08T15:23:18","date_gmt":"2025-01-08T07:23:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1098674.html"},"modified":"2025-01-08T15:23:20","modified_gmt":"2025-01-08T07:23:20","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e4%bf%a1%e6%81%af%e4%b8%ad%e7%9a%84%e6%96%87%e6%9c%ac-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1098674.html","title":{"rendered":"Python\u5982\u4f55\u83b7\u53d6\u4fe1\u606f\u4e2d\u7684\u6587\u672c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25062746\/96e1b0ca-c918-458d-b0a3-52d691261910.webp\" alt=\"Python\u5982\u4f55\u83b7\u53d6\u4fe1\u606f\u4e2d\u7684\u6587\u672c\" \/><\/p>\n<p><p> <strong>Python\u83b7\u53d6\u4fe1\u606f\u4e2d\u7684\u6587\u672c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u6b63\u5219\u8868\u8fbe\u5f0f\u3001BeautifulSoup\u5e93\u3001Python\u7684string\u65b9\u6cd5\u3002<\/strong> \u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u7528\u4e8e\u5bf9\u6587\u672c\u8fdb\u884c\u6a21\u5f0f\u5339\u914d\u548c\u63d0\u53d6\uff0cBeautifulSoup\u5e93\u5219\u4e13\u95e8\u7528\u4e8e\u89e3\u6790\u548c\u6293\u53d6HTML\u548cXML\u6587\u4ef6\u4e2d\u7684\u6570\u636e\uff0c\u800cPython\u7684string\u65b9\u6cd5\u5219\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u529f\u80fd\u3002<strong>\u5176\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u6700\u7075\u6d3b\u548c\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u4f60\u5b9a\u4e49\u590d\u6742\u7684\u6a21\u5f0f\u6765\u5339\u914d\u548c\u63d0\u53d6\u6587\u672c\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\uff08Regular Expression, Regex\uff09\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u5339\u914d\u548c\u63d0\u53d6\u5de5\u5177\u3002Python\u4e2d\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u529f\u80fd\uff0c\u5141\u8bb8\u7528\u6237\u6839\u636e\u9884\u5b9a\u4e49\u7684\u6a21\u5f0f\u5339\u914d\u548c\u63d0\u53d6\u6587\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u9700\u8981\u5bfc\u5165<code>re<\/code>\u6a21\u5757\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>re.match(pattern, string)<\/code>\uff1a\u4ece\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/li>\n<li><code>re.search(pattern, string)<\/code>\uff1a\u626b\u63cf\u6574\u4e2a\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u7ed3\u679c\u3002<\/li>\n<li><code>re.findall(pattern, string)<\/code>\uff1a\u8fd4\u56de\u6240\u6709\u4e0e\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7684\u5b50\u4e32\u3002<\/li>\n<li><code>re.sub(pattern, repl, string)<\/code>\uff1a\u4f7f\u7528\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u66ff\u6362\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7684\u5b50\u4e32\u3002<\/li>\n<\/ul>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;Hello, my em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l is example@example.com and my phone number is 123-456-7890.&quot;<\/p>\n<h2><strong>\u63d0\u53d6\u7535\u5b50\u90ae\u4ef6<\/strong><\/h2>\n<p>email_pattern = r&#39;\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,7}\\b&#39;<\/p>\n<p>emails = re.findall(email_pattern, text)<\/p>\n<p>print(&quot;Emails:&quot;, emails)<\/p>\n<h2><strong>\u63d0\u53d6\u7535\u8bdd\u53f7\u7801<\/strong><\/h2>\n<p>phone_pattern = r&#39;\\d{3}-\\d{3}-\\d{4}&#39;<\/p>\n<p>phones = re.findall(phone_pattern, text)<\/p>\n<p>print(&quot;Phone numbers:&quot;, phones)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001BeautifulSoup\u5e93<\/p>\n<\/p>\n<p><p>BeautifulSoup\u662f\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u6587\u6863\u7684Python\u5e93\u3002\u5b83\u521b\u5efa\u4e00\u4e2a\u89e3\u6790\u6811\uff0c\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u5730\u5bfc\u822a\u3001\u641c\u7d22\u548c\u4fee\u6539\u6811\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>BeautifulSoup\u7684\u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528BeautifulSoup\u9700\u8981\u5b89\u88c5<code>beautifulsoup4<\/code>\u548c<code>lxml<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4 lxml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>html = &quot;&quot;&quot;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;&lt;title&gt;Example Page&lt;\/title&gt;&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;p class=&quot;title&quot;&gt;&lt;b&gt;The Example Page&lt;\/b&gt;&lt;\/p&gt;<\/p>\n<p>&lt;p class=&quot;content&quot;&gt;Hello, this is an example page.&lt;\/p&gt;<\/p>\n<p>&lt;a href=&quot;http:\/\/example.com&quot; class=&quot;link&quot;&gt;Example Link&lt;\/a&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<h2><strong>\u89e3\u6790HTML<\/strong><\/h2>\n<p>soup = BeautifulSoup(html, &#39;lxml&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u6807\u9898<\/strong><\/h2>\n<p>title = soup.title.string<\/p>\n<p>print(&quot;Title:&quot;, title)<\/p>\n<h2><strong>\u83b7\u53d6\u6240\u6709\u6bb5\u843d\u5185\u5bb9<\/strong><\/h2>\n<p>paragraphs = soup.find_all(&#39;p&#39;)<\/p>\n<p>for p in paragraphs:<\/p>\n<p>    print(&quot;Paragraph:&quot;, p.text)<\/p>\n<h2><strong>\u83b7\u53d6\u94fe\u63a5<\/strong><\/h2>\n<p>links = soup.find_all(&#39;a&#39;)<\/p>\n<p>for link in links:<\/p>\n<p>    print(&quot;Link:&quot;, link.get(&#39;href&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001Python\u7684string\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6e\u5b57\u7b26\u4e32\u65b9\u6cd5\u63d0\u4f9b\u4e86\u4e00\u4e9b\u57fa\u672c\u7684\u6587\u672c\u5904\u7406\u529f\u80fd\uff0c\u5982\u67e5\u627e\u3001\u66ff\u6362\u3001\u62c6\u5206\u548c\u8fde\u63a5\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u5e38\u7528\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5<\/h3>\n<\/p>\n<ul>\n<li><code>str.find(sub)<\/code>\uff1a\u8fd4\u56de\u5b50\u4e32\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u9996\u6b21\u51fa\u73b0\u4f4d\u7f6e\u3002<\/li>\n<li><code>str.replace(old, new)<\/code>\uff1a\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u5b50\u4e32\u3002<\/li>\n<li><code>str.split(sep)<\/code>\uff1a\u6839\u636e\u6307\u5b9a\u7684\u5206\u9694\u7b26\u62c6\u5206\u5b57\u7b26\u4e32\u3002<\/li>\n<li><code>str.join(iterable)<\/code>\uff1a\u4f7f\u7528\u6307\u5b9a\u7684\u5206\u9694\u7b26\u8fde\u63a5\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\u3002<\/li>\n<\/ul>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, my email is example@example.com and my phone number is 123-456-7890.&quot;<\/p>\n<h2><strong>\u67e5\u627e\u5b50\u4e32<\/strong><\/h2>\n<p>position = text.find(&#39;email&#39;)<\/p>\n<p>print(&quot;Position of &#39;email&#39;:&quot;, position)<\/p>\n<h2><strong>\u66ff\u6362\u5b50\u4e32<\/strong><\/h2>\n<p>new_text = text.replace(&#39;example@example.com&#39;, &#39;newemail@domain.com&#39;)<\/p>\n<p>print(&quot;New Text:&quot;, new_text)<\/p>\n<h2><strong>\u62c6\u5206\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>words = text.split()<\/p>\n<p>print(&quot;Words:&quot;, words)<\/p>\n<h2><strong>\u8fde\u63a5\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>joined_text = &#39; &#39;.join(words)<\/p>\n<p>print(&quot;Joined Text:&quot;, joined_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u7ed3\u5408\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5f80\u5f80\u9700\u8981\u7ed3\u5408\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u590d\u6742\u7684\u6587\u672c\u6570\u636e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\uff0c\u7136\u540e\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u7279\u5b9a\u7684\u6587\u672c\u4fe1\u606f\uff0c\u6700\u540e\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u5bf9\u7ed3\u679c\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>import re<\/p>\n<p>html = &quot;&quot;&quot;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;&lt;title&gt;Example Page&lt;\/title&gt;&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;p class=&quot;content&quot;&gt;Contact me at example@example.com or call 123-456-7890.&lt;\/p&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<h2><strong>\u89e3\u6790HTML<\/strong><\/h2>\n<p>soup = BeautifulSoup(html, &#39;lxml&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u6bb5\u843d\u5185\u5bb9<\/strong><\/h2>\n<p>content = soup.find(&#39;p&#39;, class_=&#39;content&#39;).text<\/p>\n<h2><strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u7535\u5b50\u90ae\u4ef6\u548c\u7535\u8bdd\u53f7\u7801<\/strong><\/h2>\n<p>email_pattern = r&#39;\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,7}\\b&#39;<\/p>\n<p>phone_pattern = r&#39;\\d{3}-\\d{3}-\\d{4}&#39;<\/p>\n<p>emails = re.findall(email_pattern, content)<\/p>\n<p>phones = re.findall(phone_pattern, content)<\/p>\n<p>print(&quot;Emails:&quot;, emails)<\/p>\n<p>print(&quot;Phone numbers:&quot;, phones)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\uff0c\u53ef\u4ee5\u770b\u5230\u5728Python\u4e2d\u83b7\u53d6\u4fe1\u606f\u4e2d\u7684\u6587\u672c\u6709\u591a\u79cd\u65b9\u6cd5\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u6570\u636e\u683c\u5f0f\u3002\u5728\u5904\u7406\u590d\u6742\u7684\u6587\u672c\u6570\u636e\u65f6\uff0c\u7ed3\u5408\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\u548c\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u63d0\u53d6\u6587\u672c\u4fe1\u606f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5e93\u6765\u63d0\u53d6\u6587\u672c\u4fe1\u606f\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ecBeautifulSoup\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u6587\u6863\uff0cPandas\u7528\u4e8e\u5904\u7406\u8868\u683c\u6570\u636e\uff0c\u4ee5\u53ca\u6b63\u5219\u8868\u8fbe\u5f0f\uff08re\u6a21\u5757\uff09\u7528\u4e8e\u63d0\u53d6\u7279\u5b9a\u6a21\u5f0f\u7684\u6587\u672c\u3002\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u6765\u83b7\u53d6\u6240\u9700\u7684\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406PDF\u6587\u4ef6\u4ee5\u63d0\u53d6\u6587\u672c\uff1f<\/strong><br \/>\u5904\u7406PDF\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528PyPDF2\u548cpdfplumber\u7b49\u5e93\u3002PyPDF2\u80fd\u591f\u8bfb\u53d6PDF\u6587\u6863\u5e76\u63d0\u53d6\u6587\u672c\uff0c\u800cpdfplumber\u5219\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u6587\u672c\u63d0\u53d6\u80fd\u529b\uff0c\u53ef\u4ee5\u5904\u7406\u590d\u6742\u5e03\u5c40\u7684PDF\u6587\u4ef6\u3002\u4f7f\u7528\u8fd9\u4e9b\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6PDF\u4e2d\u7684\u6587\u672c\u4fe1\u606f\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u63a8\u8350\u7684Python\u5e93\u6765\u8fdb\u884c\u6587\u672c\u4fe1\u606f\u7684\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff1f<\/strong><br \/>\u5bf9\u4e8e\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff0c\u53ef\u4ee5\u4f7f\u7528NLTK\uff08Natural Language Toolkit\uff09\u548cspaCy\u7b49\u5e93\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u5305\u62ec\u5206\u8bcd\u3001\u8bcd\u6027\u6807\u6ce8\u3001\u547d\u540d\u5b9e\u4f53\u8bc6\u522b\u7b49\uff0c\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u66f4\u6df1\u5165\u5730\u5206\u6790\u548c\u63d0\u53d6\u6587\u672c\u4fe1\u606f\u3002\u901a\u8fc7\u8fd9\u4e9b\u5de5\u5177\uff0c\u7528\u6237\u80fd\u591f\u66f4\u597d\u5730\u7406\u89e3\u548c\u5904\u7406\u6587\u672c\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u83b7\u53d6\u4fe1\u606f\u4e2d\u7684\u6587\u672c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u6b63\u5219\u8868\u8fbe\u5f0f\u3001BeautifulSoup\u5e93\u3001Python\u7684strin [&hellip;]","protected":false},"author":3,"featured_media":1098679,"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\/1098674"}],"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=1098674"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098674\/revisions"}],"predecessor-version":[{"id":1098681,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1098674\/revisions\/1098681"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1098679"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1098674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1098674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1098674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}