{"id":1081143,"date":"2025-01-08T12:36:52","date_gmt":"2025-01-08T04:36:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1081143.html"},"modified":"2025-01-08T12:36:55","modified_gmt":"2025-01-08T04:36:55","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e7%99%be%e5%ba%a6%e7%88%ac%e6%95%b0%e6%8d%ae-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1081143.html","title":{"rendered":"python\u5982\u4f55\u5728\u767e\u5ea6\u722c\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183321\/e230361b-1428-41e6-8311-212e8ea49081.webp\" alt=\"python\u5982\u4f55\u5728\u767e\u5ea6\u722c\u6570\u636e\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u5728\u767e\u5ea6\u722c\u6570\u636e\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u53d1\u9001HTTP\u8bf7\u6c42\u3001\u89e3\u6790HTML\u5185\u5bb9\u3001\u63d0\u53d6\u6240\u9700\u6570\u636e\u3001\u5904\u7406\u548c\u5b58\u50a8\u6570\u636e\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7requests\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u901a\u8fc7BeautifulSoup\u5e93\u89e3\u6790HTML\u5185\u5bb9\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u722c\u866b\u64cd\u4f5c\u8981\u9075\u5b88\u76f8\u5173\u6cd5\u5f8b\u6cd5\u89c4\u548c\u7f51\u7ad9\u7684robots.txt\u6587\u4ef6\uff0c\u907f\u514d\u9891\u7e41\u8bf7\u6c42\u5bf9\u7f51\u7ad9\u9020\u6210\u8d1f\u62c5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u5c55\u5f00\u5982\u4f55\u4f7f\u7528Python\u5728\u767e\u5ea6\u722c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u53d1\u9001HTTP\u8bf7\u6c42<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u722c\u53d6\u7f51\u9875\u7684\u7b2c\u4e00\u6b65\u662f\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u83b7\u53d6\u7f51\u9875\u7684\u5185\u5bb9\u3002\u6700\u5e38\u7528\u7684\u5e93\u662frequests\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86requests\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install requests<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528requests\u5e93\u53d1\u9001HTTP\u8bf7\u6c42\u83b7\u53d6\u767e\u5ea6\u641c\u7d22\u7ed3\u679c\u9875\u9762\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>def get_html(url):<\/p>\n<p>    headers = {<\/p>\n<p>        &#39;User-Agent&#39;: &#39;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/58.0.3029.110 Safari\/537.3&#39;}<\/p>\n<p>    response = requests.get(url, headers=headers)<\/p>\n<p>    return response.text<\/p>\n<p>url = &#39;https:\/\/www.b<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>du.com\/s?wd=Python&#39;<\/p>\n<p>html_content = get_html(url)<\/p>\n<p>print(html_content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u89e3\u6790HTML\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6\u5230HTML\u5185\u5bb9\u540e\uff0c\u4e0b\u4e00\u6b65\u662f\u89e3\u6790HTML\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528BeautifulSoup\u5e93\u6765\u89e3\u6790HTML\u5185\u5bb9\u3002\u786e\u4fdd\u5b89\u88c5BeautifulSoup\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install beautifulsoup4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528BeautifulSoup\u89e3\u6790HTML\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>def parse_html(html_content):<\/p>\n<p>    soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<\/p>\n<p>    return soup<\/p>\n<p>soup = parse_html(html_content)<\/p>\n<p>print(soup.prettify())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u63d0\u53d6\u6240\u9700\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4ece\u89e3\u6790\u540e\u7684HTML\u4e2d\u63d0\u53d6\u6240\u9700\u7684\u6570\u636e\u3002\u4ee5\u767e\u5ea6\u641c\u7d22\u7ed3\u679c\u4e3a\u4f8b\uff0c\u6211\u4eec\u63d0\u53d6\u6bcf\u4e2a\u641c\u7d22\u7ed3\u679c\u7684\u6807\u9898\u548c\u94fe\u63a5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def extract_data(soup):<\/p>\n<p>    results = []<\/p>\n<p>    for item in soup.find_all(&#39;div&#39;, class_=&#39;result c-container&#39;):<\/p>\n<p>        title_tag = item.find(&#39;h3&#39;)<\/p>\n<p>        link_tag = item.find(&#39;a&#39;)<\/p>\n<p>        if title_tag and link_tag:<\/p>\n<p>            title = title_tag.get_text()<\/p>\n<p>            link = link_tag[&#39;href&#39;]<\/p>\n<p>            results.append({&#39;title&#39;: title, &#39;link&#39;: link})<\/p>\n<p>    return results<\/p>\n<p>data = extract_data(soup)<\/p>\n<p>for item in data:<\/p>\n<p>    print(f&quot;Title: {item[&#39;title&#39;]}, Link: {item[&#39;link&#39;]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u548c\u5b58\u50a8\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6\u5230\u6240\u9700\u6570\u636e\u540e\uff0c\u53ef\u4ee5\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u548c\u5b58\u50a8\u3002\u53ef\u4ee5\u5c06\u6570\u636e\u4fdd\u5b58\u5230CSV\u6587\u4ef6\u3001\u6570\u636e\u5e93\u7b49\u3002\u8fd9\u91cc\u4ee5\u4fdd\u5b58\u5230CSV\u6587\u4ef6\u4e3a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>def save_to_csv(data, filename):<\/p>\n<p>    with open(filename, mode=&#39;w&#39;, newline=&#39;&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>        writer = csv.DictWriter(file, fieldnames=[&#39;title&#39;, &#39;link&#39;])<\/p>\n<p>        writer.writeheader()<\/p>\n<p>        for item in data:<\/p>\n<p>            writer.writerow(item)<\/p>\n<p>save_to_csv(data, &#39;baidu_search_results.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u9075\u5b88\u722c\u866b\u89c4\u5219<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u722c\u866b\u64cd\u4f5c\u65f6\uff0c\u52a1\u5fc5\u9075\u5b88\u7f51\u7ad9\u7684robots.txt\u89c4\u5219\u3002\u767e\u5ea6\u7684robots.txt\u6587\u4ef6\u5730\u5740\u4e3a\uff1a<a href=\"https:\/\/www.baidu.com\/robots.txt%E3%80%82%E5%BB%BA%E8%AE%AE%E5%9C%A8%E7%88%AC%E5%8F%96%E6%97%B6%E6%B7%BB%E5%8A%A0%E5%BB%B6%E8%BF%9F%EF%BC%8C%E9%81%BF%E5%85%8D%E9%A2%91%E7%B9%81%E8%AF%B7%E6%B1%82%E5%AF%B9%E7%BD%91%E7%AB%99%E9%80%A0%E6%88%90%E8%B4%9F%E6%8B%85%EF%BC%9A\">https:\/\/www.baidu.com\/robots.txt\u3002\u5efa\u8bae\u5728\u722c\u53d6\u65f6\u6dfb\u52a0\u5ef6\u8fdf\uff0c\u907f\u514d\u9891\u7e41\u8bf7\u6c42\u5bf9\u7f51\u7ad9\u9020\u6210\u8d1f\u62c5\uff1a<\/a><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def get_html_with_delay(url, delay=1):<\/p>\n<p>    headers = {<\/p>\n<p>        &#39;User-Agent&#39;: &#39;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/58.0.3029.110 Safari\/537.3&#39;}<\/p>\n<p>    response = requests.get(url, headers=headers)<\/p>\n<p>    time.sleep(delay)<\/p>\n<p>    return response.text<\/p>\n<p>html_content = get_html_with_delay(url, delay=2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684Python\u811a\u672c\u793a\u4f8b\uff0c\u7528\u4e8e\u5728\u767e\u5ea6\u641c\u7d22\u201cPython\u201d\u5e76\u63d0\u53d6\u641c\u7d22\u7ed3\u679c\u7684\u6807\u9898\u548c\u94fe\u63a5\uff0c\u4fdd\u5b58\u5230CSV\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>import csv<\/p>\n<p>import time<\/p>\n<p>def get_html(url, delay=1):<\/p>\n<p>    headers = {<\/p>\n<p>        &#39;User-Agent&#39;: &#39;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/58.0.3029.110 Safari\/537.3&#39;}<\/p>\n<p>    response = requests.get(url, headers=headers)<\/p>\n<p>    time.sleep(delay)<\/p>\n<p>    return response.text<\/p>\n<p>def parse_html(html_content):<\/p>\n<p>    soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<\/p>\n<p>    return soup<\/p>\n<p>def extract_data(soup):<\/p>\n<p>    results = []<\/p>\n<p>    for item in soup.find_all(&#39;div&#39;, class_=&#39;result c-container&#39;):<\/p>\n<p>        title_tag = item.find(&#39;h3&#39;)<\/p>\n<p>        link_tag = item.find(&#39;a&#39;)<\/p>\n<p>        if title_tag and link_tag:<\/p>\n<p>            title = title_tag.get_text()<\/p>\n<p>            link = link_tag[&#39;href&#39;]<\/p>\n<p>            results.append({&#39;title&#39;: title, &#39;link&#39;: link})<\/p>\n<p>    return results<\/p>\n<p>def save_to_csv(data, filename):<\/p>\n<p>    with open(filename, mode=&#39;w&#39;, newline=&#39;&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>        writer = csv.DictWriter(file, fieldnames=[&#39;title&#39;, &#39;link&#39;])<\/p>\n<p>        writer.writeheader()<\/p>\n<p>        for item in data:<\/p>\n<p>            writer.writerow(item)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    url = &#39;https:\/\/www.baidu.com\/s?wd=Python&#39;<\/p>\n<p>    html_content = get_html(url, delay=2)<\/p>\n<p>    soup = parse_html(html_content)<\/p>\n<p>    data = extract_data(soup)<\/p>\n<p>    save_to_csv(data, &#39;baidu_search_results.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5408\u6cd5\u6027\u548c\u9053\u5fb7\u6027<\/strong>\uff1a\u5728\u8fdb\u884c\u722c\u866b\u64cd\u4f5c\u65f6\uff0c\u5fc5\u987b\u9075\u5b88\u76f8\u5173\u6cd5\u5f8b\u6cd5\u89c4\u548c\u7f51\u7ad9\u7684robots.txt\u6587\u4ef6\uff0c\u907f\u514d\u5bf9\u7f51\u7ad9\u9020\u6210\u8d1f\u62c5\u3002<\/li>\n<li><strong>\u9891\u7387\u63a7\u5236<\/strong>\uff1a\u4f7f\u7528\u5ef6\u8fdf\uff08\u5982time.sleep\uff09\u6765\u63a7\u5236\u722c\u53d6\u9891\u7387\uff0c\u907f\u514d\u5bf9\u7f51\u7ad9\u670d\u52a1\u5668\u9020\u6210\u8fc7\u5927\u538b\u529b\u3002<\/li>\n<li><strong>IP\u5c01\u7981<\/strong>\uff1a\u9891\u7e41\u8bf7\u6c42\u53ef\u80fd\u5bfc\u81f4IP\u88ab\u5c01\u7981\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee3\u7406IP\u6765\u5206\u6563\u8bf7\u6c42\u3002<\/li>\n<li><strong>\u6570\u636e\u5b58\u50a8<\/strong>\uff1a\u5408\u7406\u8bbe\u8ba1\u6570\u636e\u5b58\u50a8\u7ed3\u6784\uff0c\u786e\u4fdd\u6570\u636e\u5b58\u50a8\u7684\u9ad8\u6548\u6027\u548c\u5b89\u5168\u6027\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\u548c\u6ce8\u610f\u4e8b\u9879\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u5728\u767e\u5ea6\u722c\u53d6\u6240\u9700\u6570\u636e\uff0c\u5e76\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u548c\u5b58\u50a8\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u722c\u866b\u6280\u672f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8fdb\u884c\u767e\u5ea6\u7f51\u9875\u6570\u636e\u722c\u53d6\uff1f<\/strong><br \/>\u8981\u4f7f\u7528Python\u722c\u53d6\u767e\u5ea6\u7f51\u9875\u6570\u636e\uff0c\u60a8\u9700\u8981\u5b89\u88c5\u4e00\u4e9b\u5e93\uff0c\u5982Requests\u548cBeautifulSoup\u3002Requests\u5e93\u53ef\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u800cBeautifulSoup\u5219\u7528\u4e8e\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u3002\u9996\u5148\uff0c\u4f7f\u7528Requests\u5e93\u53d1\u9001GET\u8bf7\u6c42\u83b7\u53d6\u7f51\u9875\u6570\u636e\uff0c\u7136\u540e\u5229\u7528BeautifulSoup\u89e3\u6790HTML\uff0c\u63d0\u53d6\u6240\u9700\u7684\u4fe1\u606f\u3002\u8bf7\u6ce8\u610f\uff0c\u9075\u5faa\u767e\u5ea6\u7684\u722c\u866b\u534f\u8bae\uff0c\u907f\u514d\u9891\u7e41\u8bf7\u6c42\u4ee5\u9632\u6b62\u88ab\u5c01\u7981\u3002<\/p>\n<p><strong>\u5728\u722c\u53d6\u767e\u5ea6\u6570\u636e\u65f6\uff0c\u5982\u4f55\u5904\u7406\u53cd\u722c\u866b\u673a\u5236\uff1f<\/strong><br \/>\u767e\u5ea6\u91c7\u53d6\u4e86\u4e00\u4e9b\u53cd\u722c\u866b\u63aa\u65bd\u6765\u4fdd\u62a4\u5176\u6570\u636e\uff0c\u56e0\u6b64\u5728\u722c\u53d6\u65f6\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u6539\u53d8\u8bf7\u6c42\u5934\u3001\u4f7f\u7528\u4ee3\u7406IP\u6216\u589e\u52a0\u8bf7\u6c42\u95f4\u9694\u6765\u964d\u4f4e\u88ab\u68c0\u6d4b\u7684\u98ce\u9669\u3002\u8bbe\u7f6eUser-Agent\u53ef\u4ee5\u4f2a\u88c5\u6210\u666e\u901a\u7528\u6237\u7684\u6d4f\u89c8\u5668\u8bf7\u6c42\uff0c\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u6210\u529f\u7387\u3002\u786e\u4fdd\u60a8\u7684\u722c\u866b\u884c\u4e3a\u7b26\u5408\u7f51\u7ad9\u7684\u4f7f\u7528\u6761\u6b3e\uff0c\u4ee5\u907f\u514d\u6cd5\u5f8b\u98ce\u9669\u3002<\/p>\n<p><strong>\u722c\u53d6\u767e\u5ea6\u6570\u636e\u540e\uff0c\u5982\u4f55\u5b58\u50a8\u548c\u5206\u6790\u8fd9\u4e9b\u6570\u636e\uff1f<\/strong><br \/>\u4e00\u65e6\u6210\u529f\u722c\u53d6\u4e86\u6570\u636e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684Pandas\u5e93\u5c06\u6570\u636e\u5b58\u50a8\u4e3aDataFrame\uff0c\u65b9\u4fbf\u8fdb\u884c\u8fdb\u4e00\u6b65\u5206\u6790\u3002\u6570\u636e\u53ef\u4ee5\u5bfc\u51fa\u4e3aCSV\u6587\u4ef6\u6216\u76f4\u63a5\u5b58\u5165\u6570\u636e\u5e93\u4e2d\u3002\u901a\u8fc7\u53ef\u89c6\u5316\u5de5\u5177\u5982Matplotlib\u6216Seaborn\uff0c\u60a8\u53ef\u4ee5\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\uff0c\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u722c\u53d6\u7684\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u5728\u767e\u5ea6\u722c\u6570\u636e\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u53d1\u9001HTTP\u8bf7\u6c42\u3001\u89e3\u6790HTML\u5185\u5bb9\u3001\u63d0\u53d6\u6240\u9700\u6570\u636e\u3001\u5904\u7406\u548c\u5b58\u50a8\u6570\u636e\u3002 \u5176 [&hellip;]","protected":false},"author":3,"featured_media":1081149,"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\/1081143"}],"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=1081143"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1081143\/revisions"}],"predecessor-version":[{"id":1081151,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1081143\/revisions\/1081151"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1081149"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1081143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1081143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1081143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}