{"id":1144551,"date":"2025-01-08T23:00:45","date_gmt":"2025-01-08T15:00:45","guid":{"rendered":""},"modified":"2025-01-08T23:00:48","modified_gmt":"2025-01-08T15:00:48","slug":"%e5%9c%a8python%e4%b8%ad-%e5%a6%82%e4%bd%95%e7%88%ac%e5%8f%96%e4%ba%8c%e7%ba%a7%e7%bd%91%e9%a1%b5%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1144551.html","title":{"rendered":"\u5728python\u4e2d \u5982\u4f55\u722c\u53d6\u4e8c\u7ea7\u7f51\u9875\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181608\/06f33b05-f6d5-457d-bb54-baa1351326e2.webp\" alt=\"\u5728python\u4e2d \u5982\u4f55\u722c\u53d6\u4e8c\u7ea7\u7f51\u9875\u6570\u636e\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8981\u722c\u53d6\u4e8c\u7ea7\u7f51\u9875\u6570\u636e\uff0c\u901a\u5e38\u9700\u8981\u7528\u5230\u4e00\u4e9b\u722c\u866b\u5e93\u548c\u89e3\u6790\u5e93\uff0c\u5982Scrapy\u3001BeautifulSoup\u548cRequests\u7b49\u3002\u9996\u5148\uff0c\u901a\u8fc7Requests\u5e93\u83b7\u53d6\u4e00\u7ea7\u7f51\u9875\u7684HTML\u5185\u5bb9\uff0c\u7136\u540e\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u5e76\u63d0\u53d6\u4e8c\u7ea7\u7f51\u9875\u7684\u94fe\u63a5\uff0c\u6700\u540e\u518d\u6b21\u4f7f\u7528Requests\u5e93\u83b7\u53d6\u4e8c\u7ea7\u7f51\u9875\u7684\u6570\u636e\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u9009\u62e9\u5408\u9002\u7684\u5e93<\/strong>\u662f\u5173\u952e\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u6765\u722c\u53d6\u4e8c\u7ea7\u7f51\u9875\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u6240\u9700\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86\u6240\u9700\u7684Python\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528pip\u6765\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p>pip install beautifulsoup4<\/p>\n<p>pip install lxml<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u8fd9\u4e9b\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u83b7\u53d6\u4e00\u7ea7\u7f51\u9875\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u83b7\u53d6\u4e00\u7ea7\u7f51\u9875\u7684\u5185\u5bb9\u3002\u4f7f\u7528Requests\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\u5e76\u83b7\u53d6\u7f51\u9875\u7684HTML\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">url = &#39;http:\/\/example.com&#39;<\/p>\n<p>response = requests.get(url)<\/p>\n<p>if response.status_code == 200:<\/p>\n<p>    print(&quot;Successfully fetched the page&quot;)<\/p>\n<p>    html_content = response.text<\/p>\n<p>else:<\/p>\n<p>    print(&quot;F<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led to fetch the page&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u89e3\u6790\u4e00\u7ea7\u7f51\u9875\u5e76\u63d0\u53d6\u4e8c\u7ea7\u7f51\u9875\u94fe\u63a5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u5185\u5bb9\uff0c\u5e76\u63d0\u53d6\u4e8c\u7ea7\u7f51\u9875\u7684\u94fe\u63a5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">soup = BeautifulSoup(html_content, &#39;lxml&#39;)<\/p>\n<p>links = soup.find_all(&#39;a&#39;, href=True)<\/p>\n<h2><strong>\u63d0\u53d6\u6240\u6709\u4e8c\u7ea7\u7f51\u9875\u94fe\u63a5<\/strong><\/h2>\n<p>secondary_urls = []<\/p>\n<p>for link in links:<\/p>\n<p>    href = link[&#39;href&#39;]<\/p>\n<p>    if &#39;secondary-page&#39; in href:  # \u81ea\u5b9a\u4e49\u7b5b\u9009\u6761\u4ef6<\/p>\n<p>        secondary_urls.append(href)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u83b7\u53d6\u548c\u89e3\u6790\u4e8c\u7ea7\u7f51\u9875\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u904d\u5386\u6240\u6709\u4e8c\u7ea7\u7f51\u9875\u94fe\u63a5\uff0c\u83b7\u53d6\u5e76\u89e3\u6790\u5b83\u4eec\u7684\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for secondary_url in secondary_urls:<\/p>\n<p>    full_url = url + secondary_url  # \u5b8c\u6574\u7684\u4e8c\u7ea7\u7f51\u9875URL<\/p>\n<p>    response = requests.get(full_url)<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        secondary_html_content = response.text<\/p>\n<p>        secondary_soup = BeautifulSoup(secondary_html_content, &#39;lxml&#39;)<\/p>\n<p>        # \u89e3\u6790\u4e8c\u7ea7\u7f51\u9875\u5185\u5bb9<\/p>\n<p>        # \u4f8b\u5982\uff0c\u63d0\u53d6\u6807\u9898\u548c\u6b63\u6587<\/p>\n<p>        title = secondary_soup.find(&#39;h1&#39;).text<\/p>\n<p>        content = secondary_soup.find(&#39;div&#39;, class_=&#39;content&#39;).text<\/p>\n<p>        print(f&quot;Title: {title}&quot;)<\/p>\n<p>        print(f&quot;Content: {content}&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Failed to fetch {full_url}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u548c\u5b58\u50a8\u722c\u53d6\u7684\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u722c\u53d6\u5e76\u89e3\u6790\u4e8c\u7ea7\u7f51\u9875\u6570\u636e\u540e\uff0c\u901a\u5e38\u9700\u8981\u5c06\u6570\u636e\u5b58\u50a8\u5230\u672c\u5730\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5c06\u6570\u636e\u5b58\u50a8\u5230CSV\u6587\u4ef6\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>\u6253\u5f00\u4e00\u4e2aCSV\u6587\u4ef6\uff0c\u51c6\u5907\u5199\u5165<\/strong><\/h2>\n<p>with open(&#39;data.csv&#39;, &#39;w&#39;, newline=&#39;&#39;, encoding=&#39;utf-8&#39;) as csvfile:<\/p>\n<p>    fieldnames = [&#39;Title&#39;, &#39;Content&#39;]<\/p>\n<p>    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)<\/p>\n<p>    writer.writeheader()<\/p>\n<p>    for secondary_url in secondary_urls:<\/p>\n<p>        full_url = url + secondary_url<\/p>\n<p>        response = requests.get(full_url)<\/p>\n<p>        if response.status_code == 200:<\/p>\n<p>            secondary_html_content = response.text<\/p>\n<p>            secondary_soup = BeautifulSoup(secondary_html_content, &#39;lxml&#39;)<\/p>\n<p>            title = secondary_soup.find(&#39;h1&#39;).text<\/p>\n<p>            content = secondary_soup.find(&#39;div&#39;, class_=&#39;content&#39;).text<\/p>\n<p>            writer.writerow({&#39;Title&#39;: title, &#39;Content&#39;: content})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u9519\u8bef\u5904\u7406\u548c\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u95ee\u9898\uff0c\u5982\u7f51\u7edc\u8bf7\u6c42\u5931\u8d25\u3001\u9875\u9762\u5185\u5bb9\u53d8\u5316\u7b49\u3002\u4e3a\u4e86\u63d0\u5347\u722c\u866b\u7684\u5065\u58ee\u6027\u548c\u6548\u7387\uff0c\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6dfb\u52a0\u8bf7\u6c42\u91cd\u8bd5\u673a\u5236<\/strong>\uff1a\u4f7f\u7528<code>requests<\/code>\u5e93\u7684\u91cd\u8bd5\u673a\u5236\uff0c\u786e\u4fdd\u5728\u7f51\u7edc\u6ce2\u52a8\u65f6\u4ecd\u80fd\u6210\u529f\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u4ee3\u7406<\/strong>\uff1a\u5728\u5927\u91cf\u8bf7\u6c42\u65f6\uff0c\u53ef\u80fd\u4f1a\u88ab\u76ee\u6807\u7f51\u7ad9\u5c01\u7981IP\uff0c\u4f7f\u7528\u4ee3\u7406\u53ef\u4ee5\u7ed5\u8fc7\u8fd9\u79cd\u9650\u5236\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bbe\u7f6e\u8bf7\u6c42\u5934<\/strong>\uff1a\u4f2a\u88c5\u6210\u6d4f\u89c8\u5668\uff0c\u907f\u514d\u88ab\u7f51\u7ad9\u8bc6\u522b\u4e3a\u722c\u866b\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e76\u53d1\u8bf7\u6c42<\/strong>\uff1a\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u63d0\u9ad8\u722c\u53d6\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u6dfb\u52a0\u8bf7\u6c42\u91cd\u8bd5\u673a\u5236\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from requests.adapters import HTTPAdapter<\/p>\n<p>from requests.packages.urllib3.util.retry import Retry<\/p>\n<h2><strong>\u914d\u7f6e\u8bf7\u6c42\u91cd\u8bd5\u673a\u5236<\/strong><\/h2>\n<p>session = requests.Session()<\/p>\n<p>retry = Retry(total=5, backoff_factor=1, status_forcelist=[500, 502, 503, 504])<\/p>\n<p>adapter = HTTPAdapter(max_retries=retry)<\/p>\n<p>session.mount(&#39;http:\/\/&#39;, adapter)<\/p>\n<p>session.mount(&#39;https:\/\/&#39;, adapter)<\/p>\n<p>response = session.get(url)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u4f60\u5e94\u8be5\u5df2\u7ecf\u4e86\u89e3\u4e86<strong>\u5728Python\u4e2d\u5982\u4f55\u722c\u53d6\u4e8c\u7ea7\u7f51\u9875\u6570\u636e<\/strong>\u7684\u57fa\u672c\u6d41\u7a0b\u3002\u6838\u5fc3\u6b65\u9aa4\u5305\u62ec\uff1a<strong>\u9009\u62e9\u5408\u9002\u7684\u5e93\u3001\u83b7\u53d6\u4e00\u7ea7\u7f51\u9875\u5185\u5bb9\u3001\u89e3\u6790\u4e00\u7ea7\u7f51\u9875\u5e76\u63d0\u53d6\u4e8c\u7ea7\u7f51\u9875\u94fe\u63a5\u3001\u83b7\u53d6\u548c\u89e3\u6790\u4e8c\u7ea7\u7f51\u9875\u5185\u5bb9\u3001\u5904\u7406\u548c\u5b58\u50a8\u722c\u53d6\u7684\u6570\u636e<\/strong>\u3002\u6b64\u5916\uff0c\u6dfb\u52a0\u9519\u8bef\u5904\u7406\u548c\u4f18\u5316\u63aa\u65bd\u53ef\u4ee5\u63d0\u9ad8\u722c\u866b\u7684\u5065\u58ee\u6027\u548c\u6548\u7387\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5b9e\u73b0\u7f51\u9875\u6570\u636e\u722c\u53d6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728\u722c\u53d6\u4e8c\u7ea7\u7f51\u9875\u6570\u636e\u65f6\uff0c\u6211\u9700\u8981\u4e86\u89e3\u54ea\u4e9b\u57fa\u7840\u77e5\u8bc6\uff1f<\/strong><br \/>\u5728\u5f00\u59cb\u722c\u53d6\u4e8c\u7ea7\u7f51\u9875\u6570\u636e\u4e4b\u524d\uff0c\u4e86\u89e3\u4e00\u4e9b\u57fa\u7840\u77e5\u8bc6\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u719f\u6089Python\u7f16\u7a0b\u8bed\u8a00\u53ca\u5176\u5e93\uff0c\u5982Requests\u548cBeautifulSoup\uff0c\u8fd9\u4e24\u8005\u662f\u7f51\u9875\u722c\u866b\u7684\u6838\u5fc3\u5de5\u5177\u3002\u5176\u6b21\uff0c\u4e86\u89e3HTML\u548cCSS\u7684\u57fa\u672c\u7ed3\u6784\u5c06\u5e2e\u52a9\u60a8\u66f4\u6709\u6548\u5730\u89e3\u6790\u7f51\u9875\u6570\u636e\u3002\u6b64\u5916\uff0c\u5b66\u4e60\u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u7279\u5b9a\u6570\u636e\u4e5f\u662f\u975e\u5e38\u6709\u7528\u7684\u3002\u6700\u540e\uff0c\u638c\u63e1\u57fa\u672c\u7684\u7f51\u7edc\u8bf7\u6c42\u6982\u5ff5\u548c\u722c\u866b\u7684\u9053\u5fb7\u89c4\u8303\uff0c\u786e\u4fdd\u60a8\u7684\u722c\u866b\u884c\u4e3a\u4e0d\u4f1a\u5bf9\u7f51\u7ad9\u9020\u6210\u8d1f\u62c5\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u7f51\u9875\u4e2d\u7684\u53cd\u722c\u866b\u673a\u5236\uff1f<\/strong><br \/>\u8bb8\u591a\u7f51\u7ad9\u4e3a\u4e86\u4fdd\u62a4\u5176\u6570\u636e\uff0c\u5b9e\u65bd\u4e86\u53cd\u722c\u866b\u673a\u5236\u3002\u5728\u722c\u53d6\u4e8c\u7ea7\u7f51\u9875\u6570\u636e\u65f6\uff0c\u60a8\u53ef\u80fd\u4f1a\u9047\u5230\u8fd9\u4e9b\u963b\u788d\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528\u4f2a\u88c5\u8bf7\u6c42\u5934\uff08\u5982User-Agent\uff09\u6765\u6a21\u4eff\u6d4f\u89c8\u5668\u884c\u4e3a\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u4ee3\u7406IP\u53ef\u4ee5\u5e2e\u52a9\u9690\u85cf\u60a8\u7684\u771f\u5b9eIP\u5730\u5740\uff0c\u4ece\u800c\u51cf\u5c11\u88ab\u5c01\u7981\u7684\u98ce\u9669\u3002\u9002\u65f6\u7684\u8bf7\u6c42\u5ef6\u8fdf\u4e5f\u6709\u52a9\u4e8e\u964d\u4f4e\u5bf9\u670d\u52a1\u5668\u7684\u538b\u529b\uff0c\u907f\u514d\u89e6\u53d1\u53cd\u722c\u866b\u63aa\u65bd\u3002<\/p>\n<p><strong>\u5728\u722c\u53d6\u6570\u636e\u540e\uff0c\u5982\u4f55\u5b58\u50a8\u548c\u5904\u7406\u8fd9\u4e9b\u6570\u636e\uff1f<\/strong><br \/>\u4e00\u65e6\u6210\u529f\u722c\u53d6\u5230\u4e8c\u7ea7\u7f51\u9875\u7684\u6570\u636e\uff0c\u5b58\u50a8\u548c\u5904\u7406\u8fd9\u4e9b\u6570\u636e\u53d8\u5f97\u81f3\u5173\u91cd\u8981\u3002\u60a8\u53ef\u4ee5\u9009\u62e9\u5c06\u6570\u636e\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\uff0c\u65b9\u4fbf\u540e\u7eed\u5206\u6790\u548c\u5904\u7406\u3002\u53e6\u4e00\u79cd\u9009\u62e9\u662f\u5c06\u6570\u636e\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\uff0c\u5982SQLite\u6216MySQL\uff0c\u8fd9\u6837\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u7ba1\u7406\u548c\u67e5\u8be2\u6570\u636e\u3002\u5904\u7406\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u548c\u5206\u6790\uff0c\u4f7f\u5176\u66f4\u6613\u4e8e\u7406\u89e3\u548c\u4f7f\u7528\uff0c\u9002\u5408\u540e\u7eed\u7684\u7edf\u8ba1\u6216\u53ef\u89c6\u5316\u5de5\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8981\u722c\u53d6\u4e8c\u7ea7\u7f51\u9875\u6570\u636e\uff0c\u901a\u5e38\u9700\u8981\u7528\u5230\u4e00\u4e9b\u722c\u866b\u5e93\u548c\u89e3\u6790\u5e93\uff0c\u5982Scrapy\u3001BeautifulSou [&hellip;]","protected":false},"author":3,"featured_media":1144560,"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\/1144551"}],"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=1144551"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1144551\/revisions"}],"predecessor-version":[{"id":1144562,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1144551\/revisions\/1144562"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1144560"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1144551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1144551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1144551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}