python 抓取網(wǎng)頁中的圖片并下載

方法一:獲取網(wǎng)頁內(nèi)容,寫入本地txt文件,然后通過正則表達式獲取圖片

# -*- coding: UTF-8 -*-

import urllib2, re, urllib, socket

# socket.setdefaulttimeout(60) # 全局超時時間設(shè)置

# 打開網(wǎng)頁,將網(wǎng)頁內(nèi)容寫入到 textName 文件中
def openHTML(htmlUrl, textName):
    # 打開網(wǎng)頁 設(shè)置超時時間
    page = urllib2.urlopen(htmlUrl, timeout=60)
    # 讀取頁面源碼
    htmlcode = page.read()
    # 轉(zhuǎn)換編碼
    htmlcode = urllib2.unquote(htmlcode);

    # 新建txt文件
    file = open('%s.txt' % textName, "a+")

    # 將 html 寫入到本地txt中
    file.write(htmlcode)

    # 添加換行,用于分割
    file.write('\n--------------------------------------\n\n\n')

    # 正則,獲取 以src=' 開頭  以 jpg 結(jié)尾的字符串
    reg = r'src=\'(.+?\.jpg)'

    # 編譯一下,運行更快
    reg_img = re.compile(reg)

    # 得到列表
    imglist = reg_img.findall(htmlcode)

    # 列表的每一個元素前添加 https: 完善為完整的圖片地址
    imglistNew = ['https:'+i for i in imglist]

    # 下載圖片 urlretrieve(地址,名字,進度回調(diào))
    name = 1
    for item in imglistNew:
        urllib.urlretrieve(item, 'img/%s.jpg' % name, downLoadCallBack)
        name +=1

    # 將圖片列表轉(zhuǎn)成字符串寫入txt文件
    imsliststring = '\n'.join(imglistNew)
    file.write(imsliststring)

    file.close()

def downLoadCallBack(blocknum, blocksize, totalsize):
    print 'blocknum=',blocknum # 第幾次下載
    print 'blocksize=',blocksize # 第幾次下載的大小
    print 'totalsize=',totalsize # 總大小
    progress = float(blocknum) * blocksize / totalsize * 100
    if progress > 100 :
        progress = 100
    print '圖片下載進度=%0.2f' % progress + '%'
    print "%.2f%%" % progress # python中格式化字符 %% ,可以輸出%

openHTML('https://www.feizl.com/feizhuliu/hui/', 'html')

urllib.urlretrieve('https://pic.feizl.com/upload/allimg/190814/gxtxvyz3ngavqaa.jpg', '123.jpg', downLoadCallBack)

image.png

方法二:

from bs4 import BeautifulSoup

#pip install --user 安裝包


def SoupHtml(htmlUrl):
    # 打開網(wǎng)頁 設(shè)置超時時間
    page = urllib2.urlopen(htmlUrl, timeout=60)
    # 讀取頁面源碼
    htmlcode = page.read()
    # 轉(zhuǎn)換編碼
    htmlcode = urllib2.unquote(htmlcode);

    soup = BeautifulSoup(htmlcode, "html.parser")

    imgs = soup.find_all('img',{"src":True}) # 找到img標簽 且 存在src的屬性 返回列表
    print imgs

    for item in imgs:
        print item.get('src') # get 得到屬性值


SoupHtml('https://www.feizl.com/feizhuliu/hui/')
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容