天天看点

python——批量爬取网页图片

关键点

  • 如果网页地址会变动则需要找到下一张图片的网页地址
  • 需要找到网页中图片的保存地址

    演示代码如下:

import urllib.request
import os 

#打开网址
def open_url(url):
    req = urllib.request.Request(url)
    req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36')
    response = urllib.request.urlopen(req)
    html = response.read()
    return html
#获取下一张图片的网站   
def get_url(url):
    html = open_url(url)
    html = html.decode('utf-8')
    find_url = html.find('fr direction-after')   
    find_url_first = html.find('href=',find_url -80)
    find_url_end = html.find('.cn"',find_url - 15)
    find_url_first += 6
    find_url_end += 3
    return html[find_url_first:find_url_end]
    
 #获取图片路径   
def  get_img(url):
    html = open_url(url)
    html = html.decode('utf-8')
    find_img = html.find('content-pic')  
    find_img_first = find_img + 29
    find_img_end = find_img + 44
    return html[find_img_first:find_img_end]

#下载图片
def down_picture(count,folder='ooxx'):
    if os.path.exists(folder) == False:
        os.mkdir(folder)
    os.chdir(folder) 
    while count :     
        url = 'http://towmu.huanxiangdianyingyuan.cn'
        img_url = get_img(url)
        end = str(img_url.split('/')[2])
        end = end.split('.')
        enddouble = str(end[1]).split('"')[0]
        with open(end[0] +'.'+ enddouble,'wb') as f:
            _url = url + img_url
            img = open_url(_url)
            f.write(img)
        url = get_url(url)
        count = count - 1
#交互界面
count = int(input('输入的下载的张数:'))
folder =input('输入保存的文件夹:')
down_picture(count,folder)