【Python】爬取图片并保存到本地

 本项目主要用到的库

  • requests 
  • BeautifulSoup
  • os

 本项目练习目的

  • 熟悉爬虫常用的requests、BeatifulSoup库
  • 学习图片自动保存到本地的方法
#!/usr/bin/env python 
# -*- coding:utf-8 -*-
import requests
from bs4 import BeautifulSoup
import time
import os
import shutil

def load():
    url = "https://www.meitulu.com/t/nvshen/"
    html = get_url(url)
    soup = BeautifulSoup(html,'html.parser')
    href = soup.select('ul.img li p.p_title a')             
    for i in href:
        get_pics(i['href'])

def get_pics(url):
    html = get_url(url)
    soup = BeautifulSoup(html,'html.parser')
    pics = soup.select('div.content center img')
    for i in pics:
        pic = requests.get(i['src'])
        with open('pictures\{}.jpg'.format(i['alt']),'wb')as f:
            print('正在下载:{}'.format(i['alt']))
            f.write(pic.content)
            time.sleep(1)

def get_url(url):
    html = requests.get(url)
    html.encoding = 'utf-8'
    html = html.text
    return html

def make_dir():
    path = 'pictures'
    if(os.path.exists(path)):
        shutil.rmtree(path)
    os.mkdir(path)

def main():
    make_dir()
    load()

main()

 

你可能感兴趣的:(Python)