{"id":1089836,"date":"2025-01-08T13:57:04","date_gmt":"2025-01-08T05:57:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1089836.html"},"modified":"2025-01-08T13:57:08","modified_gmt":"2025-01-08T05:57:08","slug":"python%e5%a6%82%e4%bd%95%e6%8a%93%e5%8f%96%e6%89%8b%e6%9c%ba%e5%8f%b7%e7%a0%81-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1089836.html","title":{"rendered":"python\u5982\u4f55\u6293\u53d6\u624b\u673a\u53f7\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24202549\/38c092ce-ae1b-4c15-adb4-21118d9e1119.webp\" alt=\"python\u5982\u4f55\u6293\u53d6\u624b\u673a\u53f7\u7801\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u6293\u53d6\u624b\u673a\u53f7\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001BeautifulSoup\u3001Scrapy\u7b49\u5de5\u5177\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u3001BeautifulSoup\u3001Scrapy\u7b49\u5de5\u5177\u90fd\u53ef\u4ee5\u7528\u6765\u63d0\u53d6\u7f51\u9875\u4e2d\u7684\u624b\u673a\u53f7\u7801\u3002<\/strong>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f7f\u7528\u53ca\u5176\u4f18\u7f3a\u70b9\uff0c\u5e76\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u5339\u914d\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u5339\u914d\u7279\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528re\u6a21\u5757\u6765\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5re\u6a21\u5757<\/h4>\n<\/p>\n<p><p>re\u6a21\u5757\u662fPython\u7684\u6807\u51c6\u5e93\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5\uff0c\u76f4\u63a5\u5bfc\u5165\u5373\u53ef\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u624b\u673a\u53f7\u7801<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u624b\u673a\u53f7\u7801\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u7528\u4e8e\u5339\u914d\u5e38\u89c1\u7684\u624b\u673a\u53f7\u7801\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">phone_pattern = re.compile(r&#39;\\b\\d{10}\\b&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5339\u914d10\u4f4d\u6570\u5b57\u7684\u5b57\u7b26\u4e32\u3002\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8c03\u6574\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u4f8b\u5982\u5339\u914d\u7279\u5b9a\u7684\u56fd\u5bb6\u6216\u5730\u533a\u7684\u624b\u673a\u53f7\u7801\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u4ece\u6587\u672c\u4e2d\u63d0\u53d6\u624b\u673a\u53f7\u7801<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u4ece\u6587\u672c\u4e2d\u63d0\u53d6\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u624b\u673a\u53f7\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Here are some phone numbers: 1234567890, 0987654321, 1122334455.&quot;<\/p>\n<p>phone_numbers = phone_pattern.findall(text)<\/p>\n<p>print(phone_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u5728\u6587\u672c\u4e2d\u67e5\u627e\u6240\u6709\u7b26\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u624b\u673a\u53f7\u7801\uff0c\u5e76\u5c06\u5b83\u4eec\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001BeautifulSoup<\/h3>\n<\/p>\n<p><p>BeautifulSoup\u662f\u4e00\u4e2a\u7528\u4e8e\u89e3\u6790HTML\u548cXML\u6587\u6863\u7684Python\u5e93\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u63d0\u53d6\u7f51\u9875\u4e2d\u7684\u7279\u5b9a\u5185\u5bb9\uff0c\u4f8b\u5982\u624b\u673a\u53f7\u7801\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5BeautifulSoup<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528BeautifulSoup\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528BeautifulSoup\u89e3\u6790\u7f51\u9875<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528BeautifulSoup\u4ece\u7f51\u9875\u4e2d\u63d0\u53d6\u624b\u673a\u53f7\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>import re<\/p>\n<h2><strong>\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<\/strong><\/h2>\n<p>url = &#39;https:\/\/example.com&#39;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>web_content = response.text<\/p>\n<h2><strong>\u89e3\u6790\u7f51\u9875<\/strong><\/h2>\n<p>soup = BeautifulSoup(web_content, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u624b\u673a\u53f7\u7801<\/strong><\/h2>\n<p>phone_pattern = re.compile(r&#39;\\b\\d{10}\\b&#39;)<\/p>\n<p>phone_numbers = phone_pattern.findall(soup.get_text())<\/p>\n<p>print(phone_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u9996\u5148\u4f7f\u7528requests\u5e93\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u7136\u540e\u4f7f\u7528BeautifulSoup\u89e3\u6790\u7f51\u9875\uff0c\u5e76\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u624b\u673a\u53f7\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001Scrapy<\/h3>\n<\/p>\n<p><p>Scrapy\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u722c\u866b\u6846\u67b6\uff0c\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u6293\u53d6\u4efb\u52a1\u3002\u4f7f\u7528Scrapy\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece\u591a\u4e2a\u7f51\u9875\u4e2d\u63d0\u53d6\u624b\u673a\u53f7\u7801\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Scrapy<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Scrapy\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install scrapy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528Scrapy\u6293\u53d6\u624b\u673a\u53f7\u7801<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Scrapy\u6293\u53d6\u624b\u673a\u53f7\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import scrapy<\/p>\n<p>import re<\/p>\n<p>class PhoneSpider(scrapy.Spider):<\/p>\n<p>    name = &quot;phone_spider&quot;<\/p>\n<p>    start_urls = [&#39;https:\/\/example.com&#39;]<\/p>\n<p>    def parse(self, response):<\/p>\n<p>        phone_pattern = re.compile(r&#39;\\b\\d{10}\\b&#39;)<\/p>\n<p>        phone_numbers = phone_pattern.findall(response.text)<\/p>\n<p>        for phone_number in phone_numbers:<\/p>\n<p>            yield {&#39;phone_number&#39;: phone_number}<\/p>\n<h2><strong>\u4fdd\u5b58\u722c\u53d6\u7ed3\u679c<\/strong><\/h2>\n<p>from scrapy.crawler import CrawlerProcess<\/p>\n<p>process = CrawlerProcess(settings={<\/p>\n<p>    &quot;FEEDS&quot;: {<\/p>\n<p>        &quot;phone_numbers.json&quot;: {&quot;format&quot;: &quot;json&quot;},<\/p>\n<p>    },<\/p>\n<p>})<\/p>\n<p>process.crawl(PhoneSpider)<\/p>\n<p>process.start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2aScrapy\u722c\u866b\uff0c\u4ece\u6307\u5b9a\u7684URL\u4e2d\u63d0\u53d6\u624b\u673a\u53f7\u7801\uff0c\u5e76\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230JSON\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u4e09\u79cd\u5728Python\u4e2d\u6293\u53d6\u624b\u673a\u53f7\u7801\u7684\u65b9\u6cd5\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u3001BeautifulSoup\u548cScrapy\u3002<strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u9002\u7528\u4e8e\u7b80\u5355\u7684\u6587\u672c\u5339\u914d\u4efb\u52a1<\/strong>\uff0c<strong>BeautifulSoup\u9002\u7528\u4e8e\u89e3\u6790\u548c\u63d0\u53d6\u7f51\u9875\u4e2d\u7684\u7279\u5b9a\u5185\u5bb9<\/strong>\uff0c<strong>Scrapy\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u6293\u53d6\u4efb\u52a1<\/strong>\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u9ad8\u6570\u636e\u6293\u53d6\u7684\u6548\u7387\u548c\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u7b80\u5355\u6613\u7528\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u6587\u672c\u5339\u914d\u4efb\u52a1<\/li>\n<li>\u4e0d\u4f9d\u8d56\u4e8e\u7279\u5b9a\u7684\u7f51\u9875\u7ed3\u6784<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u590d\u6742\u7684\u7f51\u9875\u7ed3\u6784\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u80fd\u4e0d\u591f\u7075\u6d3b<\/li>\n<li>\u65e0\u6cd5\u5904\u7406\u52a8\u6001\u52a0\u8f7d\u7684\u5185\u5bb9<\/li>\n<\/ul>\n<p><h4>2\u3001BeautifulSoup\u7684\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u89e3\u6790HTML\u548cXML\u6587\u6863\uff0c\u9002\u7528\u4e8e\u63d0\u53d6\u7f51\u9875\u4e2d\u7684\u7279\u5b9a\u5185\u5bb9<\/li>\n<li>\u6613\u4e8e\u4f7f\u7528\uff0c\u4ee3\u7801\u7b80\u6d01<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u4f9d\u8d56requests\u5e93\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<\/li>\n<li>\u65e0\u6cd5\u5904\u7406\u52a8\u6001\u52a0\u8f7d\u7684\u5185\u5bb9<\/li>\n<\/ul>\n<p><h4>3\u3001Scrapy\u7684\u4f18\u7f3a\u70b9<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u6293\u53d6\u4efb\u52a1<\/li>\n<li>\u652f\u6301\u5904\u7406\u52a8\u6001\u52a0\u8f7d\u7684\u5185\u5bb9<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5b66\u4e60\u66f2\u7ebf\u8f83\u9661\uff0c\u521d\u5b66\u8005\u53ef\u80fd\u9700\u8981\u4e00\u4e9b\u65f6\u95f4\u638c\u63e1<\/li>\n<li>\u4ee3\u7801\u76f8\u5bf9\u590d\u6742<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u6269\u5c55\u9605\u8bfb<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u590d\u6742\u7684\u6570\u636e\u6293\u53d6\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528Scrapy\u6293\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u7136\u540e\u4f7f\u7528BeautifulSoup\u89e3\u6790\u7f51\u9875\uff0c\u5e76\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u7279\u5b9a\u5185\u5bb9\u3002\u6b64\u5916\uff0c\u5904\u7406\u52a8\u6001\u52a0\u8f7d\u7684\u7f51\u9875\u5185\u5bb9\u65f6\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528Selenium\u7b49\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528Selenium\u5904\u7406\u52a8\u6001\u52a0\u8f7d\u7684\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>Selenium\u662f\u4e00\u4e2a\u7528\u4e8e\u81ea\u52a8\u5316\u6d4f\u89c8\u5668\u64cd\u4f5c\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5904\u7406\u52a8\u6001\u52a0\u8f7d\u7684\u5185\u5bb9\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408Selenium\u548cBeautifulSoup\u6216Scrapy\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u5b89\u88c5Selenium\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install selenium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>import re<\/p>\n<h2><strong>\u8bbe\u7f6e\u6d4f\u89c8\u5668\u9a71\u52a8<\/strong><\/h2>\n<p>driver = webdriver.Chrome(executable_path=&#39;\/path\/to\/chromedriver&#39;)<\/p>\n<h2><strong>\u6253\u5f00\u7f51\u9875<\/strong><\/h2>\n<p>driver.get(&#39;https:\/\/example.com&#39;)<\/p>\n<h2><strong>\u7b49\u5f85\u9875\u9762\u52a0\u8f7d\u5b8c\u6210<\/strong><\/h2>\n<p>driver.implicitly_w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t(10)<\/p>\n<h2><strong>\u83b7\u53d6\u7f51\u9875\u5185\u5bb9<\/strong><\/h2>\n<p>web_content = driver.page_source<\/p>\n<h2><strong>\u89e3\u6790\u7f51\u9875<\/strong><\/h2>\n<p>soup = BeautifulSoup(web_content, &#39;html.parser&#39;)<\/p>\n<h2><strong>\u63d0\u53d6\u624b\u673a\u53f7\u7801<\/strong><\/h2>\n<p>phone_pattern = re.compile(r&#39;\\b\\d{10}\\b&#39;)<\/p>\n<p>phone_numbers = phone_pattern.findall(soup.get_text())<\/p>\n<p>print(phone_numbers)<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Selenium\u5904\u7406\u52a8\u6001\u52a0\u8f7d\u7684\u5185\u5bb9\uff0c\u5e76\u7ed3\u5408BeautifulSoup\u63d0\u53d6\u624b\u673a\u53f7\u7801\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u5904\u7406\u590d\u6742\u4efb\u52a1<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u6293\u53d6\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528Scrapy\u6293\u53d6\u7f51\u9875\u5185\u5bb9\uff0c\u7136\u540e\u4f7f\u7528BeautifulSoup\u89e3\u6790\u7f51\u9875\uff0c\u5e76\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u7279\u5b9a\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import scrapy<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>import re<\/p>\n<p>class PhoneSpider(scrapy.Spider):<\/p>\n<p>    name = &quot;phone_spider&quot;<\/p>\n<p>    start_urls = [&#39;https:\/\/example.com&#39;]<\/p>\n<p>    def parse(self, response):<\/p>\n<p>        soup = BeautifulSoup(response.text, &#39;html.parser&#39;)<\/p>\n<p>        phone_pattern = re.compile(r&#39;\\b\\d{10}\\b&#39;)<\/p>\n<p>        phone_numbers = phone_pattern.findall(soup.get_text())<\/p>\n<p>        for phone_number in phone_numbers:<\/p>\n<p>            yield {&#39;phone_number&#39;: phone_number}<\/p>\n<h2><strong>\u4fdd\u5b58\u722c\u53d6\u7ed3\u679c<\/strong><\/h2>\n<p>from scrapy.crawler import CrawlerProcess<\/p>\n<p>process = CrawlerProcess(settings={<\/p>\n<p>    &quot;FEEDS&quot;: {<\/p>\n<p>        &quot;phone_numbers.json&quot;: {&quot;format&quot;: &quot;json&quot;},<\/p>\n<p>    },<\/p>\n<p>})<\/p>\n<p>process.crawl(PhoneSpider)<\/p>\n<p>process.start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u7ed3\u5408Scrapy\u548cBeautifulSoup\u63d0\u53d6\u624b\u673a\u53f7\u7801\uff0c\u5e76\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230JSON\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Python\u6293\u53d6\u624b\u673a\u53f7\u7801\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u53ca\u5176\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u65e0\u6cd5\u5339\u914d\u7279\u5b9a\u683c\u5f0f\u7684\u624b\u673a\u53f7\u7801<\/h4>\n<\/p>\n<p><p>\u89e3\u51b3\u65b9\u6cd5\uff1a\u6839\u636e\u5177\u4f53\u9700\u6c42\u8c03\u6574\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u4f8b\u5982\uff0c\u5339\u914d\u4ee5\u7279\u5b9a\u6570\u5b57\u5f00\u5934\u7684\u624b\u673a\u53f7\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">phone_pattern = re.compile(r&#39;\\b1[3456789]\\d{9}\\b&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7f51\u9875\u5185\u5bb9\u52a8\u6001\u52a0\u8f7d\uff0c\u65e0\u6cd5\u63d0\u53d6\u624b\u673a\u53f7\u7801<\/h4>\n<\/p>\n<p><p>\u89e3\u51b3\u65b9\u6cd5\uff1a\u4f7f\u7528Selenium\u5904\u7406\u52a8\u6001\u52a0\u8f7d\u7684\u5185\u5bb9\u3002\u53c2\u89c1\u672c\u6587\u4e2d\u7684Selenium\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u6293\u53d6\u901f\u5ea6\u6162\uff0c\u6548\u7387\u4f4e<\/h4>\n<\/p>\n<p><p>\u89e3\u51b3\u65b9\u6cd5\uff1a\u4f7f\u7528Scrapy\u7b49\u529f\u80fd\u5f3a\u5927\u7684\u722c\u866b\u6846\u67b6\uff0c\u652f\u6301\u5e76\u53d1\u6293\u53d6\uff0c\u63d0\u9ad8\u6293\u53d6\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u6293\u53d6\u624b\u673a\u53f7\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001BeautifulSoup\u3001Scrapy\u7b49\u5de5\u5177\u3002<strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u9002\u7528\u4e8e\u7b80\u5355\u7684\u6587\u672c\u5339\u914d\u4efb\u52a1<\/strong>\uff0c<strong>BeautifulSoup\u9002\u7528\u4e8e\u89e3\u6790\u548c\u63d0\u53d6\u7f51\u9875\u4e2d\u7684\u7279\u5b9a\u5185\u5bb9<\/strong>\uff0c<strong>Scrapy\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u6293\u53d6\u4efb\u52a1<\/strong>\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u9ad8\u6570\u636e\u6293\u53d6\u7684\u6548\u7387\u548c\u51c6\u786e\u6027\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u6293\u53d6\u4efb\u52a1\uff0c\u5e76\u6839\u636e\u5177\u4f53\u95ee\u9898\u8c03\u6574\u89e3\u51b3\u65b9\u6848\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u638c\u63e1\u5728Python\u4e2d\u6293\u53d6\u624b\u673a\u53f7\u7801\u7684\u65b9\u6cd5\u548c\u6280\u5de7\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u6293\u53d6\u7f51\u9875\u4e2d\u7684\u624b\u673a\u53f7\u7801\uff1f<\/strong><br \/>\u4f7f\u7528Python\u6293\u53d6\u7f51\u9875\u4e2d\u7684\u624b\u673a\u53f7\u7801\u901a\u5e38\u6d89\u53ca\u5230\u4f7f\u7528\u5e93\u5982Requests\u548cBeautifulSoup\u3002\u9996\u5148\uff0c\u901a\u8fc7Requests\u83b7\u53d6\u7f51\u9875\u7684HTML\u5185\u5bb9\uff0c\u7136\u540e\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u5e76\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u624b\u673a\u53f7\u7801\u3002\u8bb0\u5f97\u9075\u5faa\u7f51\u7ad9\u7684Robots.txt\u89c4\u5219\uff0c\u786e\u4fdd\u6293\u53d6\u884c\u4e3a\u662f\u5408\u6cd5\u7684\u3002<\/p>\n<p><strong>\u5728\u6293\u53d6\u624b\u673a\u53f7\u7801\u65f6\uff0c\u5982\u4f55\u5904\u7406\u53cd\u722c\u866b\u673a\u5236\uff1f<\/strong><br \/>\u5f88\u591a\u7f51\u7ad9\u4f1a\u4f7f\u7528\u53cd\u722c\u866b\u673a\u5236\u6765\u9632\u6b62\u6570\u636e\u6293\u53d6\u3002\u4e3a\u4e86\u5e94\u5bf9\u8fd9\u4e9b\u673a\u5236\uff0c\u53ef\u4ee5\u4f7f\u7528\u4f2a\u88c5User-Agent\u3001\u8bbe\u7f6e\u8bf7\u6c42\u95f4\u9694\u3001\u4f7f\u7528\u4ee3\u7406IP\u7b49\u65b9\u6cd5\u3002\u6b64\u5916\uff0c\u6a21\u62df\u7528\u6237\u884c\u4e3a\uff0c\u6bd4\u5982\u767b\u5f55\u3001\u6eda\u52a8\u7b49\uff0c\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u7ed5\u8fc7\u4e00\u4e9b\u7b80\u5355\u7684\u53cd\u722c\u866b\u63aa\u65bd\u3002<\/p>\n<p><strong>\u6293\u53d6\u624b\u673a\u53f7\u7801\u7684\u5408\u6cd5\u6027\u5982\u4f55\u5224\u65ad\uff1f<\/strong><br \/>\u5728\u6293\u53d6\u4efb\u4f55\u6570\u636e\u4e4b\u524d\uff0c\u4e86\u89e3\u76f8\u5173\u6cd5\u5f8b\u6cd5\u89c4\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u6293\u53d6\u516c\u5f00\u4fe1\u606f\u53ef\u80fd\u662f\u5141\u8bb8\u7684\uff0c\u4f46\u5982\u679c\u6d89\u53ca\u4e2a\u4eba\u9690\u79c1\u6570\u636e\uff0c\u5c24\u5176\u662f\u624b\u673a\u53f7\u7801\uff0c\u5219\u9700\u8981\u8c28\u614e\u3002\u5efa\u8bae\u4ed4\u7ec6\u9605\u8bfb\u7f51\u7ad9\u7684\u670d\u52a1\u6761\u6b3e\u548c\u9690\u79c1\u653f\u7b56\uff0c\u786e\u4fdd\u9075\u5faa\u76f8\u5173\u7684\u6cd5\u5f8b\u6cd5\u89c4\uff0c\u907f\u514d\u4fb5\u72af\u4ed6\u4eba\u9690\u79c1\u6743\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u6293\u53d6\u624b\u673a\u53f7\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001BeautifulSoup\u3001Scrapy\u7b49\u5de5\u5177\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1089846,"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\/1089836"}],"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=1089836"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1089836\/revisions"}],"predecessor-version":[{"id":1089847,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1089836\/revisions\/1089847"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1089846"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1089836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1089836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1089836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}