{"id":1183038,"date":"2025-01-15T19:10:00","date_gmt":"2025-01-15T11:10:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1183038.html"},"modified":"2025-01-15T19:10:03","modified_gmt":"2025-01-15T11:10:03","slug":"python%e5%a6%82%e4%bd%95%e6%8a%93%e5%8f%96%e9%ab%98%e5%83%8f%e7%b4%a0%e5%9b%be%e7%89%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1183038.html","title":{"rendered":"python\u5982\u4f55\u6293\u53d6\u9ad8\u50cf\u7d20\u56fe\u7247"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25132929\/566e26fb-8e8f-4e6d-999c-ab7b9b569068.webp\" alt=\"python\u5982\u4f55\u6293\u53d6\u9ad8\u50cf\u7d20\u56fe\u7247\" \/><\/p>\n<p><p> <strong>\u8981\u4f7f\u7528Python\u6293\u53d6\u9ad8\u50cf\u7d20\u56fe\u7247\uff0c\u53ef\u4ee5\u901a\u8fc7\u7f51\u7edc\u722c\u866b\u6280\u672f\u3001\u4f7f\u7528\u76f8\u5173\u7684\u5e93\u5982requests\u3001BeautifulSoup\u3001Selenium\u7b49\u6765\u5b9e\u73b0\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u53d1\u9001HTTP\u8bf7\u6c42\u3001\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u3001\u63d0\u53d6\u56fe\u7247URL\u3001\u4e0b\u8f7d\u56fe\u7247\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u64cd\u4f5c\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>1. \u4f7f\u7528requests\u548cBeautifulSoup\u5e93<\/strong><\/p>\n<\/p>\n<p><p>requests\u5e93\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\uff0cBeautifulSoup\u5e93\u7528\u4e8e\u89e3\u6790HTML\u6587\u6863\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>import os<\/p>\n<p>def download_image(url, folder_path):<\/p>\n<p>    if not os.path.exists(folder_path):<\/p>\n<p>        os.makedirs(folder_path)<\/p>\n<p>    response = requests.get(url, stream=True)<\/p>\n<p>    file_name = os.path.join(folder_path, url.split(&quot;\/&quot;)[-1])<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        with open(file_name, &#39;wb&#39;) as file:<\/p>\n<p>            for chunk in response.iter_content(1024):<\/p>\n<p>                file.write(chunk)<\/p>\n<p>        print(f&quot;Image successfully downloaded: {file_name}&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;F<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>led to download image: {url}&quot;)<\/p>\n<p>def fetch_images_from_url(page_url, folder_path):<\/p>\n<p>    response = requests.get(page_url)<\/p>\n<p>    soup = BeautifulSoup(response.text, &#39;html.parser&#39;)<\/p>\n<p>    img_tags = soup.find_all(&#39;img&#39;)<\/p>\n<p>    for img in img_tags:<\/p>\n<p>        img_url = img.get(&#39;src&#39;)<\/p>\n<p>        if img_url.startswith(&#39;http&#39;):<\/p>\n<p>            download_image(img_url, folder_path)<\/p>\n<p>page_url = &#39;https:\/\/example.com&#39;<\/p>\n<p>folder_path = &#39;.\/images&#39;<\/p>\n<p>fetch_images_from_url(page_url, folder_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u51fd\u6570\uff0c\u4e00\u4e2a\u662f<code>download_image<\/code>\u7528\u4e8e\u4e0b\u8f7d\u56fe\u7247\uff0c\u53e6\u4e00\u4e2a\u662f<code>fetch_images_from_url<\/code>\u7528\u4e8e\u4ece\u7ed9\u5b9a\u7684\u7f51\u9875URL\u4e2d\u63d0\u53d6\u6240\u6709\u56fe\u7247\u94fe\u63a5\u5e76\u4e0b\u8f7d\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u4f7f\u7528Selenium<\/strong><\/p>\n<\/p>\n<p><p>Selenium\u5e93\u9002\u7528\u4e8e\u52a8\u6001\u7f51\u9875\u6293\u53d6\uff0c\u901a\u8fc7\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>from selenium.webdriver.common.by import By<\/p>\n<p>import time<\/p>\n<p>import os<\/p>\n<p>import requests<\/p>\n<p>def download_image(url, folder_path):<\/p>\n<p>    if not os.path.exists(folder_path):<\/p>\n<p>        os.makedirs(folder_path)<\/p>\n<p>    response = requests.get(url, stream=True)<\/p>\n<p>    file_name = os.path.join(folder_path, url.split(&quot;\/&quot;)[-1])<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        with open(file_name, &#39;wb&#39;) as file:<\/p>\n<p>            for chunk in response.iter_content(1024):<\/p>\n<p>                file.write(chunk)<\/p>\n<p>        print(f&quot;Image successfully downloaded: {file_name}&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Failed to download image: {url}&quot;)<\/p>\n<p>def fetch_images_from_dynamic_page(page_url, folder_path):<\/p>\n<p>    driver = webdriver.Chrome()<\/p>\n<p>    driver.get(page_url)<\/p>\n<p>    time.sleep(5)  # wait for the page to fully load<\/p>\n<p>    img_elements = driver.find_elements(By.TAG_NAME, &#39;img&#39;)<\/p>\n<p>    for img_element in img_elements:<\/p>\n<p>        img_url = img_element.get_attribute(&#39;src&#39;)<\/p>\n<p>        if img_url.startswith(&#39;http&#39;):<\/p>\n<p>            download_image(img_url, folder_path)<\/p>\n<p>    driver.quit()<\/p>\n<p>page_url = &#39;https:\/\/example.com&#39;<\/p>\n<p>folder_path = &#39;.\/images&#39;<\/p>\n<p>fetch_images_from_dynamic_page(page_url, folder_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Selenium\u5e93\u6765\u6253\u5f00\u7f51\u9875\u5e76\u7b49\u5f85\u5176\u5b8c\u5168\u52a0\u8f7d\uff0c\u7136\u540e\u63d0\u53d6\u6240\u6709\u56fe\u7247\u94fe\u63a5\u5e76\u4e0b\u8f7d\u3002<\/p>\n<\/p>\n<p><p><strong>3. \u4f7f\u7528Scrapy\u6846\u67b6<\/strong><\/p>\n<\/p>\n<p><p>Scrapy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7f51\u7edc\u722c\u866b\u6846\u67b6\uff0c\u9002\u5408\u5927\u578b\u9879\u76ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import scrapy<\/p>\n<p>from scrapy.crawler import CrawlerProcess<\/p>\n<p>class ImageSpider(scrapy.Spider):<\/p>\n<p>    name = &quot;image_spider&quot;<\/p>\n<p>    start_urls = [&#39;https:\/\/example.com&#39;]<\/p>\n<p>    def parse(self, response):<\/p>\n<p>        img_urls = response.css(&#39;img::attr(src)&#39;).getall()<\/p>\n<p>        for img_url in img_urls:<\/p>\n<p>            if img_url.startswith(&#39;http&#39;):<\/p>\n<p>                yield scrapy.Request(url=img_url, callback=self.save_image)<\/p>\n<p>    def save_image(self, response):<\/p>\n<p>        path = &#39;.\/images\/&#39;<\/p>\n<p>        if not os.path.exists(path):<\/p>\n<p>            os.makedirs(path)<\/p>\n<p>        file_name = os.path.join(path, response.url.split(&quot;\/&quot;)[-1])<\/p>\n<p>        with open(file_name, &#39;wb&#39;) as file:<\/p>\n<p>            file.write(response.body)<\/p>\n<p>        self.log(f&quot;Image successfully downloaded: {file_name}&quot;)<\/p>\n<p>process = CrawlerProcess()<\/p>\n<p>process.crawl(ImageSpider)<\/p>\n<p>process.start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2aScrapy Spider\u7c7b\u6765\u6293\u53d6\u56fe\u7247\uff0c\u5e76\u4f7f\u7528<code>CrawlerProcess<\/code>\u6765\u542f\u52a8\u722c\u866b\u3002<\/p>\n<\/p>\n<p><p><strong>4. \u5904\u7406\u56fe\u7247\u7684\u9ad8\u50cf\u7d20\u95ee\u9898<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6293\u53d6\u9ad8\u50cf\u7d20\u56fe\u7247\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u56fe\u7247\u7684\u8d28\u91cf\u548c\u5927\u5c0f\u7b26\u5408\u8981\u6c42\u3002\u53ef\u4ee5\u4f7f\u7528Pillow\u5e93\u8fdb\u884c\u56fe\u7247\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<p>import requests<\/p>\n<p>from io import BytesIO<\/p>\n<p>def download_and_save_image(url, folder_path):<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    if response.status_code == 200:<\/p>\n<p>        img = Image.open(BytesIO(response.content))<\/p>\n<p>        file_name = os.path.join(folder_path, url.split(&quot;\/&quot;)[-1])<\/p>\n<p>        img.save(file_name, quality=95)  # Save the image with high quality<\/p>\n<p>        print(f&quot;Image successfully downloaded and saved with high quality: {file_name}&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Failed to download image: {url}&quot;)<\/p>\n<p>download_and_save_image(&#39;https:\/\/example.com\/high_res_image.jpg&#39;, &#39;.\/high_res_images&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528Pillow\u5e93\u6765\u5904\u7406\u56fe\u7247\uff0c\u5e76\u4fdd\u5b58\u9ad8\u8d28\u91cf\u7684\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6293\u53d6\u9ad8\u50cf\u7d20\u56fe\u7247\u65f6\uff0c\u5173\u952e\u6b65\u9aa4\u5305\u62ec\uff1a\u53d1\u9001HTTP\u8bf7\u6c42\u3001\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u3001\u63d0\u53d6\u56fe\u7247URL\u3001\u4e0b\u8f7d\u56fe\u7247\u4ee5\u53ca\u786e\u4fdd\u56fe\u7247\u8d28\u91cf\u3002\u53ef\u4ee5\u4f7f\u7528requests\u548cBeautifulSoup\u5e93\u8fdb\u884c\u9759\u6001\u7f51\u9875\u6293\u53d6\uff0c\u4f7f\u7528Selenium\u5e93\u8fdb\u884c\u52a8\u6001\u7f51\u9875\u6293\u53d6\uff0c\u4f7f\u7528Scrapy\u6846\u67b6\u8fdb\u884c\u5927\u578b\u9879\u76ee\u7684\u6293\u53d6\uff0c\u5e76\u4f7f\u7528Pillow\u5e93\u5904\u7406\u56fe\u7247\u8d28\u91cf\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u50cf\u7d20\u56fe\u7247\u7684\u6293\u53d6\u548c\u4fdd\u5b58\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u6293\u53d6\u9ad8\u50cf\u7d20\u56fe\u7247\uff1f<\/strong><br \/>\u8981\u6293\u53d6\u9ad8\u50cf\u7d20\u56fe\u7247\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u5e93\uff0c\u5982requests\u548cBeautifulSoup\u7ed3\u5408\u4f7f\u7528\u3002\u9996\u5148\uff0c\u4f7f\u7528requests\u5e93\u83b7\u53d6\u7f51\u9875\u7684HTML\u5185\u5bb9\uff0c\u63a5\u7740\u7528BeautifulSoup\u89e3\u6790\u7f51\u9875\uff0c\u627e\u5230\u56fe\u7247\u7684URL\uff0c\u6700\u540e\u4f7f\u7528requests\u5e93\u4e0b\u8f7d\u9ad8\u50cf\u7d20\u56fe\u7247\u3002\u786e\u4fdd\u5728\u6293\u53d6\u65f6\u9075\u5b88\u7f51\u7ad9\u7684robots.txt\u6587\u4ef6\u548c\u7248\u6743\u653f\u7b56\u3002<\/p>\n<p><strong>\u5728\u6293\u53d6\u9ad8\u50cf\u7d20\u56fe\u7247\u65f6\uff0c\u6709\u4ec0\u4e48\u9700\u8981\u6ce8\u610f\u7684\u4e8b\u9879\u5417\uff1f<\/strong><br \/>\u5728\u6293\u53d6\u9ad8\u50cf\u7d20\u56fe\u7247\u65f6\uff0c\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a\u4e86\u89e3\u76ee\u6807\u7f51\u7ad9\u7684\u4f7f\u7528\u6761\u6b3e\uff0c\u786e\u4fdd\u60a8\u6709\u6743\u6293\u53d6\u548c\u4f7f\u7528\u8fd9\u4e9b\u56fe\u7247\uff1b\u63a7\u5236\u6293\u53d6\u901f\u5ea6\uff0c\u907f\u514d\u5bf9\u7f51\u7ad9\u670d\u52a1\u5668\u9020\u6210\u8d1f\u62c5\uff1b\u4f7f\u7528\u5408\u9002\u7684User-Agent\u5934\uff0c\u786e\u4fdd\u6293\u53d6\u8bf7\u6c42\u770b\u8d77\u6765\u50cf\u6b63\u5e38\u7684\u6d4f\u89c8\u5668\u8bf7\u6c42\uff1b\u540c\u65f6\uff0c\u68c0\u67e5\u548c\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684404\u9519\u8bef\u6216\u8bf7\u6c42\u9650\u5236\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9bPython\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6293\u53d6\u9ad8\u50cf\u7d20\u56fe\u7247\uff1f<\/strong><br \/>\u6709\u51e0\u4e2aPython\u5e93\u53ef\u4ee5\u5e2e\u52a9\u60a8\u6293\u53d6\u9ad8\u50cf\u7d20\u56fe\u7247\u3002requests\u5e93\u7528\u4e8e\u53d1\u9001\u7f51\u7edc\u8bf7\u6c42\uff0cBeautifulSoup\u7528\u4e8e\u89e3\u6790HTML\u6587\u6863\uff0cPillow\u53ef\u4ee5\u5904\u7406\u56fe\u7247\u683c\u5f0f\u548c\u5927\u5c0f\uff0cScrapy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u722c\u866b\u6846\u67b6\uff0c\u9002\u5408\u5927\u89c4\u6a21\u6293\u53d6\u3002\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u80fd\u591f\u63d0\u9ad8\u6293\u53d6\u6548\u7387\u548c\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u4f7f\u7528Python\u6293\u53d6\u9ad8\u50cf\u7d20\u56fe\u7247\uff0c\u53ef\u4ee5\u901a\u8fc7\u7f51\u7edc\u722c\u866b\u6280\u672f\u3001\u4f7f\u7528\u76f8\u5173\u7684\u5e93\u5982requests\u3001BeautifulSo [&hellip;]","protected":false},"author":3,"featured_media":1183041,"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\/1183038"}],"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=1183038"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1183038\/revisions"}],"predecessor-version":[{"id":1183043,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1183038\/revisions\/1183043"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1183041"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1183038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1183038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1183038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}