python3下載圖片


import urllib.request  
import socket  
import re  
import sys  
import os

targetDir = r"E:\\DATA\常用py腳本" 

def destFile(path):  
    if not os.path.isdir(targetDir):  
        os.mkdir(targetDir)  
    pos = path.rindex('/')  
    t = os.path.join(targetDir, path[pos+1:])  
    return t  
  

if __name__ == "__main__":  
    hostname = "http://www.douban.com"  
    req = urllib.request.Request(hostname)  
    webpage = urllib.request.urlopen(req)  
    html = webpage.read()  


    #\s:任意空白字符
    #^:匹配字符串的開頭
    #*:匹配前一個字符0次或無數次
    #?:匹配前一個字符0次或1次

    result=re.findall(r'(https:[^\s]*?(jpg|png|gif))', str(html))
    #print(result)

    for link, t in result:  
        #print(link,t)  
        urllib.request.urlretrieve(link, destFile(link)) 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM