import urllib
import urllib.request
import? re #正則表達(dá)式
def load_page(url):
? ? request=urllib.request.Request(url)#發(fā)送網(wǎng)絡(luò)請(qǐng)求
? ? response=urllib.request.urlopen(request)#根據(jù)url打開頁面
? ? data=response.read()#獲取頁面響應(yīng)數(shù)據(jù)
? ? return? data#返回響應(yīng)數(shù)據(jù)
def get_image(html):
? ? regx=r'http://[\S]*jpg'#存儲(chǔ)正則表達(dá)式
? ? pattern=re.compile(regx)#編譯表達(dá)式構(gòu)造匹配模式
? ? get_image=re.findall(pattern,repr(html))#進(jìn)行正則匹配并返回結(jié)果
? ? num=1
? ? #遍歷獲取的圖片
? ? for img in get_image:
? ? ? ? image=load_page(img)
? ? ? ? #將圖片存入到指定文件夾
? ? ? ? with open('D:\\photo\\%s.jpg' %num,'wb')as fb:
? ? ? ? ? ? fb.write(image)
? ? ? ? ? ? print('正在現(xiàn)在第%s張圖片' %num)
? ? ? ? ? ? num=num+1
? ? print("下載完成")
url='http://p.weather.com.cn/2018/01/2832905.shtml#p=1'
html=load_page(url)
get_image(html)