爬虫下载网站图片

# coding:utf-8

import requests
import re

def load_page(url):
    # request = requests.Request(url=url)
    # response = urllib2.urlopen(request)
    # data = response.read()    #得到网页全部内容
    data = requests.get(url=url).content
    print(data)
    return data

def get_image(html):
    regx = r'http.?://[\S]*.jpg'
    pattern = re.compile(regx)    #用Python的方法正则匹配
    get_image = re.findall(pattern, repr(html))

    num = 1
    for img in get_image:
        image = load_page(img)
        with open(r'D:\图片\%s.jpg' %num, 'wb') as fb:
            fb.write(image)
            print('正在下载第%s张图片' %num)
            num = num+1
    print('下载完成!')

url='https://www.xxx.com'
html = load_page(url)
get_image(html)

  

posted @ 2021-03-18 09:23  one*love  阅读(33)  评论(0编辑  收藏  举报