Python爬取图片并存储

'''
思路: 从缩略图页面开始爬取
1) 先爬取所有缩略图的a标签
2)
3)
'''
import requests
from bs4 import BeautifulSoup
import os
if not os.path.exists("男神图集"):
    os.makedirs("男神图集")
import lxml
from urllib import request

url = "http://www.shuaia.net/index_50.html"

if __name__ == '__main__':
    for i in range(5):
        url = "http://www.shuaia.net/index_5%d.html"%i
        req = requests.get(url = url)
        req.encoding = 'utf-8'
        print("响应码: ", req.status_code)
        html = req.text
        bf2 = BeautifulSoup(html, 'lxml')
        a_list = bf2.find_all("a", class_="itemimg-view")
        for item in a_list:
            print (item)
            #print(item.get('href'), item.get("title"))
            name = item.get("title")
            down_url = "http://www.shuaia.net" + item.get("href")
            #下载
            request.urlretrieve(filename="男神图集/%s.jpg" %name, url = down_url)
            print("%s下载成功"%name + '>'*10)

 

你可能感兴趣的:(就爱敲代码,爬虫)