方法一:獲取網(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/')