天天看點

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)