爬取妹子图网站的图片

末鹿安然 提交于 2020-01-03 04:23:27

网站: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()

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!