Python的一個僞爬蟲,獲取圖片鏈接後,用於下載網頁圖片

 

 

 

import time
import requests
import os

int_path = r"F:\image\dcm"
for root, dirs, files in os.walk(int_path):
    for filename in files:  # 遍歷所有文件
        txt_path = os.path.join(root, filename)
        print(txt_path)
        dcm_name = filename.split(".txt")[0]

        fopen = open(txt_path, 'r')
        lines = str(fopen.readlines())

        all = lines.split(".dcm")
        n=0
        for e in all:
            n+=1
            url = e.split("https")[-1]
            new_url = "https" + url + ".dcm"

            file_name = url.split("/")[-1]+".dcm"
            print(new_url)
            print(file_name)
            if os.path.exists(os.path.join(os.path.join("./test", dcm_name), file_name)):
                print("已存在")
                pass
            else:
                try:
                    res = requests.get(new_url)
                    with open(os.path.join(os.path.join("./test", dcm_name), file_name), 'wb') as f:
                        f.write(res.content)
                    #urlretrieve(new_url, file_name)
                    time.sleep(1)
                except:
                    print("產生異常")

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章