施勢帆老師的教學網站

 找回密碼
 立即註冊
搜索
熱搜: 活動 交友 discuz
查看: 38|回復: 0

同時下載多張寶可夢圖片

[複製鏈接]

826

主題

842

帖子

4164

積分

管理員

Rank: 9Rank: 9Rank: 9

積分
4164
發表於 5 天前 | 顯示全部樓層 |閱讀模式
import requests
from bs4 import BeautifulSoup
import threading

def download(num):
    # 加入 try 保護避免遇到無法下載的狀況而發生錯誤
    try:
        web = requests.get(f'https://tw.portal-pokemon.com/play/pokedex/{num}')   # 使用變數替換網址
        soup = BeautifulSoup(web.text, "html.parser")
        img = soup.select('meta[property="og:image"]')
        imgUrl = img[0]['content']
        imgFile = requests.get(imgUrl)
        f = open(f'pokemon/{num}.png', 'wb')
        f.write(imgFile.content)
        f.close()
        print(num)
    except:
        print('error')
        pass
# 使用迴圈,一次可以下載 1~99 張圖片
for i in range(1,100):
    n = f'{i:04d}'
    threading.Thread(target=download, args=(n,)).start()

回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

Archiver|手機版|小黑屋|施勢帆老師的教學網站

GMT+8, 2026-3-15 10:39 , Processed in 0.058423 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回復 返回頂部 返回列表