Skip to content

Latest commit

 

History

History
82 lines (64 loc) · 1.59 KB

7. 成果.md

File metadata and controls

82 lines (64 loc) · 1.59 KB
marp theme paginate backgroundColor color backgroundImage
true
uncover
true
url("spider poster-light.svg")
<style> :root{ --color-background-code: #222222; --color-foreground: #FFF; } marp-pre,.language-python{ border-radius: .5em; color:#FFF; padding: 1.2em!important; } code{ border-radius: 7px; } </style>

成果

PYTHON爬蟲營隊
講師:毛宥鈞


下載圖片

import requests

url = "圖片的連結"  # 替換為要下載的圖片連結
response = requests.get(url)

# 檢查圖片是否下載成功
if response.status_code == 200:
    with open("圖片檔名.jpg", "wb") as file:
        file.write(response.content)
        print("圖片下載成功")
else:
    print("無法下載圖片")

迴圈

一次下載多張圖片

import requests

def download_images(image_links):
    for index, link in enumerate(image_links):
        try:
            response = requests.get(link)
            response.raise_for_status()
            with open(f"image_{index+1}.jpg", "wb") as file:
                file.write(response.content)
            print(f"Image {index+1} downloaded successfully.")
        except requests.exceptions.RequestException as e:
            print(f"Error downloading image {index+1}: {e}")

# Example usage
image_links = [
    "https://picsum.photos/200/300",
    "https://picsum.photos/200/300","https://picsum.photos/200/300","https://picsum.photos/200/300","https://picsum.photos/200/300",
]

download_images(image_links)

bg 80%