close

Python 小練習 : 下載網頁圖片到桌面的資料夾裡面

#檔案的複製、移動、刪除
shutil.copy(src, dst)
shutil.move(src, dst)
shutil.rmtree('dir') 

================樓下程式碼================================

import requests
from bs4 import BeautifulSoup
import  shutil
import os


html = requests.get("http://www.gamebase.com.tw/forum/64172/topic/96278769/1")
soup = BeautifulSoup(html.text,features="lxml")  # 把html 裝到 soup裡面
 

dir_path="C:/Users/User/Desktop/Ricky"

if( os.path.isdir(dir_path)):
    print('Exist')  
else :
    os.mkdir(dir_path) 


for img in soup.select(".img"):
    # print(img["src"],img["src"].split("/")[-1])
    fname=img["src"].split("/")[-1]                                                          # FileName
    res2 = requests.get(img["src"],stream=True)                                       # Getfileposition
   
    # give a absolute pos
    with open("C:/Users/User/Desktop/Ricky/"+fname,"bw") as f:
        shutil.copyfileobj(res2.raw,f)                                                         # downloadFile
        del res2
  
print("Finish")   

 

================樓上程式碼================================

 

arrow
arrow
    全站熱搜

    ricky10116r2d2 發表在 痞客邦 留言(0) 人氣()