marp | theme | paginate | backgroundColor | color | backgroundImage |
---|---|---|---|---|---|
true |
uncover |
true |
url("spider poster-light.svg") |
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)