{"id":1104555,"date":"2025-01-08T16:21:52","date_gmt":"2025-01-08T08:21:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1104555.html"},"modified":"2025-01-08T16:21:52","modified_gmt":"2025-01-08T08:21:52","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8python%e7%88%ac%e8%99%ab%e6%90%9c%e7%b4%a2%e7%99%be%e5%ba%a6%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1104555.html","title":{"rendered":"\u5982\u4f55\u5229\u7528python\u722c\u866b\u641c\u7d22\u767e\u5ea6\u56fe\u7247"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065844\/ca3a7020-bec1-47e9-bf32-e318654fb04f.webp\" alt=\"\u5982\u4f55\u5229\u7528python\u722c\u866b\u641c\u7d22\u767e\u5ea6\u56fe\u7247\" \/><\/p>\n<p><h3>\u5982\u4f55\u5229\u7528Python\u722c\u866b\u641c\u7d22\u767e\u5ea6\u56fe\u7247<\/h3>\n<\/p>\n<p><p><strong>\u5229\u7528Python\u722c\u866b\u641c\u7d22\u767e\u5ea6\u56fe\u7247\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u6784\u5efa\u8bf7\u6c42\u5934\u3001\u53d1\u9001\u8bf7\u6c42\u3001\u89e3\u6790\u7f51\u9875\u3001\u4e0b\u8f7d\u56fe\u7247\u3001\u4fdd\u5b58\u56fe\u7247\u3002<\/strong>\u5176\u4e2d\uff0c\u6784\u5efa\u8bf7\u6c42\u5934\u81f3\u5173\u91cd\u8981\uff0c\u5b83\u80fd\u591f\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\uff0c\u4ece\u800c\u907f\u514d\u722c\u866b\u88ab\u5c01\u7981\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u5c55\u5f00\u6784\u5efa\u8bf7\u6c42\u5934\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><p><strong>\u6784\u5efa\u8bf7\u6c42\u5934\uff1a\u5728\u722c\u53d6\u767e\u5ea6\u56fe\u7247\u65f6\uff0c\u8bbe\u7f6e\u8bf7\u6c42\u5934\u6a21\u62df\u6d4f\u89c8\u5668\u7684\u884c\u4e3a\uff0c\u80fd\u6709\u6548\u907f\u5f00\u53cd\u722c\u673a\u5236\u3002\u5173\u952e\u53c2\u6570\u5305\u62ecUser-Agent\u3001Referer\u7b49\u3002User-Agent\u662f\u6807\u8bc6\u8bf7\u6c42\u6765\u6e90\u6d4f\u89c8\u5668\u7c7b\u578b\u7684\u91cd\u8981\u5b57\u6bb5\uff0c\u800cReferer\u5219\u8868\u660e\u8bf7\u6c42\u6765\u6e90\u9875\u9762\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6784\u5efa\u8bf7\u6c42\u5934<\/h3>\n<\/p>\n<p><p>\u5728\u722c\u866b\u8fc7\u7a0b\u4e2d\uff0c\u6784\u5efa\u5408\u7406\u7684\u8bf7\u6c42\u5934\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u8bf7\u6c42\u5934\u4e2d\u7684\u5173\u952e\u5b57\u6bb5\u5305\u62ecUser-Agent\u548cReferer\u3002User-Agent\u7528\u6765\u6807\u8bc6\u8bf7\u6c42\u6765\u6e90\u7684\u6d4f\u89c8\u5668\u7c7b\u578b\uff0c\u53ef\u4ee5\u907f\u514d\u4e00\u4e9b\u57fa\u672c\u7684\u53cd\u722c\u673a\u5236\u3002Referer\u5219\u7528\u6765\u8868\u660e\u8bf7\u6c42\u6765\u6e90\uff0c\u8fdb\u4e00\u6b65\u6a21\u62df\u771f\u5b9e\u7528\u6237\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">headers = {<\/p>\n<p>    &quot;User-Agent&quot;: &quot;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/58.0.3029.110 Safari\/537.3&quot;,<\/p>\n<p>    &quot;Referer&quot;: &quot;https:\/\/image.b<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>du.com\/&quot;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u53d1\u9001\u8bf7\u6c42<\/h3>\n<\/p>\n<p><p>\u6784\u5efa\u597d\u8bf7\u6c42\u5934\u4e4b\u540e\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u53d1\u9001\u8bf7\u6c42\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u3002\u53ef\u4ee5\u4f7f\u7528requests\u5e93\u6765\u5b8c\u6210\u8fd9\u4e00\u6b65\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>url = &quot;https:\/\/image.baidu.com\/search\/index?tn=baiduimage&amp;word=cat&quot;<\/p>\n<p>response = requests.get(url, headers=headers)<\/p>\n<p>html_content = response.text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u89e3\u6790\u7f51\u9875<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6\u5230\u7f51\u9875\u5185\u5bb9\u4e4b\u540e\uff0c\u9700\u8981\u89e3\u6790\u7f51\u9875\u63d0\u53d6\u56fe\u7247\u7684\u94fe\u63a5\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528BeautifulSoup\u5e93\u6765\u89e3\u6790HTML\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>soup = BeautifulSoup(html_content, &quot;html.parser&quot;)<\/p>\n<p>img_tags = soup.find_all(&quot;img&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4e0b\u8f7d\u56fe\u7247<\/h3>\n<\/p>\n<p><p>\u89e3\u6790\u51fa\u56fe\u7247\u94fe\u63a5\u540e\uff0c\u63a5\u4e0b\u6765\u9700\u8981\u4e0b\u8f7d\u56fe\u7247\u3002\u53ef\u4ee5\u904d\u5386\u6240\u6709\u7684img\u6807\u7b7e\uff0c\u83b7\u53d6\u5176src\u5c5e\u6027\uff0c\u7136\u540e\u4e0b\u8f7d\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>if not os.path.exists(&quot;images&quot;):<\/p>\n<p>    os.makedirs(&quot;images&quot;)<\/p>\n<p>for img_tag in img_tags:<\/p>\n<p>    img_url = img_tag.get(&quot;src&quot;)<\/p>\n<p>    if img_url:<\/p>\n<p>        img_response = requests.get(img_url, headers=headers)<\/p>\n<p>        with open(os.path.join(&quot;images&quot;, os.path.basename(img_url)), &quot;wb&quot;) as img_file:<\/p>\n<p>            img_file.write(img_response.content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4fdd\u5b58\u56fe\u7247<\/h3>\n<\/p>\n<p><p>\u5728\u4e0b\u8f7d\u56fe\u7247\u7684\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u5c06\u5176\u4fdd\u5b58\u5230\u672c\u5730\u3002\u53ef\u4ee5\u4f7f\u7528os\u5e93\u521b\u5efa\u4e00\u4e2a\u76ee\u5f55\uff0c\u7136\u540e\u5c06\u4e0b\u8f7d\u7684\u56fe\u7247\u4fdd\u5b58\u5230\u8be5\u76ee\u5f55\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if not os.path.exists(&quot;images&quot;):<\/p>\n<p>    os.makedirs(&quot;images&quot;)<\/p>\n<p>for img_tag in img_tags:<\/p>\n<p>    img_url = img_tag.get(&quot;src&quot;)<\/p>\n<p>    if img_url:<\/p>\n<p>        img_response = requests.get(img_url, headers=headers)<\/p>\n<p>        with open(os.path.join(&quot;images&quot;, os.path.basename(img_url)), &quot;wb&quot;) as img_file:<\/p>\n<p>            img_file.write(img_response.content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u7ffb\u9875<\/h3>\n<\/p>\n<p><p>\u5728\u767e\u5ea6\u56fe\u7247\u641c\u7d22\u9875\u9762\u4e2d\uff0c\u641c\u7d22\u7ed3\u679c\u901a\u5e38\u662f\u5206\u9875\u663e\u793a\u7684\u3002\u4e3a\u4e86\u83b7\u53d6\u6240\u6709\u641c\u7d22\u7ed3\u679c\u4e2d\u7684\u56fe\u7247\uff0c\u6211\u4eec\u9700\u8981\u5904\u7406\u7ffb\u9875\u3002\u5728\u7f51\u9875\u4e2d\uff0c\u7ffb\u9875\u7684\u94fe\u63a5\u901a\u5e38\u53ef\u4ee5\u5728\u4e0b\u4e00\u9875\u7684\u6309\u94ae\u4e2d\u627e\u5230\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u89e3\u6790\u8be5\u94fe\u63a5\u6765\u5b9e\u73b0\u7ffb\u9875\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">next_page = soup.find(&quot;a&quot;, class_=&quot;n&quot;)<\/p>\n<p>if next_page:<\/p>\n<p>    next_url = next_page.get(&quot;href&quot;)<\/p>\n<p>    response = requests.get(next_url, headers=headers)<\/p>\n<p>    html_content = response.text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u7f51\u7edc\u722c\u866b\u8fc7\u7a0b\u4e2d\uff0c\u7f51\u7edc\u95ee\u9898\u3001\u53cd\u722c\u866b\u673a\u5236\u7b49\u5404\u79cd\u56e0\u7d20\u90fd\u53ef\u80fd\u5bfc\u81f4\u8bf7\u6c42\u5931\u8d25\u3002\u56e0\u6b64\uff0c\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0\u5f02\u5e38\u5904\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    response = requests.get(url, headers=headers)<\/p>\n<p>    response.raise_for_status()<\/p>\n<p>except requests.RequestException as e:<\/p>\n<p>    print(f&quot;Request failed: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u5229\u7528Python\u722c\u866b\u641c\u7d22\u767e\u5ea6\u56fe\u7247\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u6784\u5efa\u8bf7\u6c42\u5934\u3001\u53d1\u9001\u8bf7\u6c42\u3001\u89e3\u6790\u7f51\u9875\u3001\u4e0b\u8f7d\u56fe\u7247\u3001\u4fdd\u5b58\u56fe\u7247\u3001\u5904\u7406\u7ffb\u9875\u548c\u5f02\u5e38\u5904\u7406\u3002\u6bcf\u4e00\u6b65\u90fd\u6709\u5176\u5173\u952e\u70b9\uff0c\u5c24\u5176\u662f\u6784\u5efa\u8bf7\u6c42\u5934\u548c\u5904\u7406\u7ffb\u9875\uff0c\u8fd9\u662f\u786e\u4fdd\u722c\u866b\u80fd\u591f\u7a33\u5b9a\u9ad8\u6548\u8fd0\u884c\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>def download_images_from_baidu(query, num_images=50):<\/p>\n<p>    headers = {<\/p>\n<p>        &quot;User-Agent&quot;: &quot;Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/58.0.3029.110 Safari\/537.3&quot;,<\/p>\n<p>        &quot;Referer&quot;: &quot;https:\/\/image.baidu.com\/&quot;<\/p>\n<p>    }<\/p>\n<p>    url = f&quot;https:\/\/image.baidu.com\/search\/index?tn=baiduimage&amp;word={query}&quot;<\/p>\n<p>    image_count = 0<\/p>\n<p>    if not os.path.exists(&quot;images&quot;):<\/p>\n<p>        os.makedirs(&quot;images&quot;)<\/p>\n<p>    while url and image_count &lt; num_images:<\/p>\n<p>        try:<\/p>\n<p>            response = requests.get(url, headers=headers)<\/p>\n<p>            response.raise_for_status()<\/p>\n<p>            html_content = response.text<\/p>\n<p>        except requests.RequestException as e:<\/p>\n<p>            print(f&quot;Request failed: {e}&quot;)<\/p>\n<p>            break<\/p>\n<p>        soup = BeautifulSoup(html_content, &quot;html.parser&quot;)<\/p>\n<p>        img_tags = soup.find_all(&quot;img&quot;)<\/p>\n<p>        for img_tag in img_tags:<\/p>\n<p>            img_url = img_tag.get(&quot;src&quot;)<\/p>\n<p>            if img_url and image_count &lt; num_images:<\/p>\n<p>                try:<\/p>\n<p>                    img_response = requests.get(img_url, headers=headers)<\/p>\n<p>                    img_response.raise_for_status()<\/p>\n<p>                    with open(os.path.join(&quot;images&quot;, os.path.basename(img_url)), &quot;wb&quot;) as img_file:<\/p>\n<p>                        img_file.write(img_response.content)<\/p>\n<p>                    image_count += 1<\/p>\n<p>                except requests.RequestException as e:<\/p>\n<p>                    print(f&quot;Failed to download image {img_url}: {e}&quot;)<\/p>\n<p>        next_page = soup.find(&quot;a&quot;, class_=&quot;n&quot;)<\/p>\n<p>        url = next_page.get(&quot;href&quot;) if next_page else None<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    download_images_from_baidu(&quot;cat&quot;, num_images=50)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u4f60\u5c06\u80fd\u591f\u4e0b\u8f7d\u6307\u5b9a\u6570\u91cf\u7684\u767e\u5ea6\u56fe\u7247\u641c\u7d22\u7ed3\u679c\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u5230\u672c\u5730\u3002\u8fd9\u79cd\u722c\u866b\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u4fee\u6539\u548c\u6269\u5c55\uff0c\u4f8b\u5982\u6dfb\u52a0\u66f4\u591a\u7684\u5f02\u5e38\u5904\u7406\u3001\u4f18\u5316\u4e0b\u8f7d\u901f\u5ea6\u7b49\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u722c\u866b\u83b7\u53d6\u767e\u5ea6\u56fe\u7247\u7684\u5177\u4f53\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u8981\u4f7f\u7528Python\u722c\u866b\u83b7\u53d6\u767e\u5ea6\u56fe\u7247\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\uff0c\u5982requests\u548cBeautifulSoup\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u53d1\u9001HTTP\u8bf7\u6c42\u5230\u767e\u5ea6\u56fe\u7247\u641c\u7d22\u9875\u9762\uff0c\u89e3\u6790\u8fd4\u56de\u7684HTML\u5185\u5bb9\uff0c\u63d0\u53d6\u56fe\u7247\u7684URL\u5730\u5740\u3002\u7136\u540e\uff0c\u4f7f\u7528\u8fd9\u4e9bURL\u4e0b\u8f7d\u56fe\u7247\u5e76\u4fdd\u5b58\u5230\u672c\u5730\u3002\u786e\u4fdd\u9075\u5faa\u7f51\u7ad9\u7684robots.txt\u6587\u4ef6\u548c\u76f8\u5173\u6cd5\u5f8b\u6cd5\u89c4\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528\u722c\u866b\u6293\u53d6\u767e\u5ea6\u56fe\u7247\u65f6\uff0c\u6709\u4ec0\u4e48\u9700\u8981\u6ce8\u610f\u7684\u4e8b\u9879\u5417\uff1f<\/strong><br \/>\u5728\u6293\u53d6\u767e\u5ea6\u56fe\u7247\u65f6\uff0c\u9700\u6ce8\u610f\u7f51\u7ad9\u7684\u4f7f\u7528\u6761\u6b3e\u548c\u722c\u866b\u884c\u4e3a\u7684\u5408\u6cd5\u6027\u3002\u907f\u514d\u8fc7\u4e8e\u9891\u7e41\u5730\u8bf7\u6c42\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4IP\u88ab\u5c01\u7981\u3002\u6b64\u5916\uff0c\u5904\u7406\u8bf7\u6c42\u5934\u548cUser-Agent\u53ef\u4ee5\u5e2e\u52a9\u6a21\u62df\u771f\u5b9e\u7528\u6237\u884c\u4e3a\uff0c\u4ece\u800c\u964d\u4f4e\u88ab\u68c0\u6d4b\u7684\u98ce\u9669\u3002\u5c0a\u91cd\u7248\u6743\u95ee\u9898\uff0c\u907f\u514d\u4e0b\u8f7d\u548c\u4f7f\u7528\u53d7\u7248\u6743\u4fdd\u62a4\u7684\u56fe\u7247\u3002<\/p>\n<p><strong>\u5982\u4f55\u63d0\u9ad8\u722c\u53d6\u767e\u5ea6\u56fe\u7247\u7684\u6548\u7387\u548c\u51c6\u786e\u6027\uff1f<\/strong><br \/>\u63d0\u9ad8\u722c\u53d6\u6548\u7387\u548c\u51c6\u786e\u6027\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u5f02\u6b65\u8bf7\u6c42\u6765\u52a0\u5feb\u4e0b\u8f7d\u901f\u5ea6\uff0c\u5e76\u5728\u89e3\u6790HTML\u65f6\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u9009\u62e9\u5668\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u5408\u7406\u7684\u8bf7\u6c42\u95f4\u9694\u65f6\u95f4\u4ee5\u9632\u6b62\u88ab\u5c01\uff0c\u5e76\u4f7f\u7528\u4ee3\u7406IP\u6765\u5206\u6563\u8bf7\u6c42\u6765\u6e90\uff0c\u4ece\u800c\u63d0\u5347\u722c\u866b\u7684\u7a33\u5b9a\u6027\u548c\u6301\u7eed\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u5229\u7528Python\u722c\u866b\u641c\u7d22\u767e\u5ea6\u56fe\u7247 \u5229\u7528Python\u722c\u866b\u641c\u7d22\u767e\u5ea6\u56fe\u7247\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u6784\u5efa\u8bf7\u6c42\u5934\u3001\u53d1\u9001\u8bf7\u6c42 [&hellip;]","protected":false},"author":3,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104555"}],"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=1104555"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104555\/revisions"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1104555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1104555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1104555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}