爬取网站所有图片

主要功能:
1.爬取图片
2.保存到本地

源代码:

from http.client import IncompleteRead
import requests
import re
import urllib.request
import time
new_data = requests.get("https://www.***.com/g/xingxiu", verify=False).text
qq = re.findall("https://[a-zA-Z\.\/_0-9]{1,100}jpg", new_data)
old_file_name = "c:/gg"
i = 0
for url in qq:
    if url:
        img_net = urllib.request.urlopen(url)
        try:
            content_i = img_net.read()
        except IncompleteRead as e:
            buffers = e.partial
        time.sleep(2)
        print(url)
        with open(old_file_name + "/" + str(i) + '.jpg', 'wb') as f:
            time.sleep(2)
            if content_i:
                f.write(content_i)
            else:
                f.write(buffers)
        if i>len(qq):
            break
        i += 1

你可能感兴趣的:(爬虫,图片,python)