爬取妹子图网站的图片

网站:http://www.meizitu.com/

目标:用BeautifulSoup解析网页源代码,获取图片.

图片链接:

# /home/wl/PycharmProjects/untitled
# -*- coding:utf-8 -*-
# author:龙

from bs4 import BeautifulSoup
import urllib.request
import os

def test():
    girl_url ='http://www.meizitu.com/'
    response = urllib.request.urlopen(girl_url).read()
    response = response.decode ('gb2312')
    #print (response)
    soup = BeautifulSoup(response,'html.parser')#创建对象
    imgs = soup.find_all('img')
    #print(imgs)
    for img in imgs:
        #print (img)
        #print (type(img))
        link = img.get('src')
        #print (link)
        name = img.get('alt')
        print("正在下载%s的图片"%name)
        urllib.request.urlretrieve(link,'image/{}.jpg'.format(name))


if __name__ == '__main__':
    if not os.path.exists('image'):
        os.mkdir('image')
    test()

猜你喜欢

转载自blog.csdn.net/wzyaiwl/article/details/81568975