1. 程式人生 > >Python下載圖片小程序

Python下載圖片小程序

pil aid 則表達式 src post int pre https 批評

歡迎大俠們指正批評

思路:

1、引入相關的python文件(import re import urllib)

2、讀取對應網頁的html文件(使用 urllib)

def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
print html
return html

3、在讀取的html文件中使用正則表達式匹配圖片路徑並保存圖片

def getimage(date):
imgR = r‘src="(.*?\.jpg)"‘
cimgR = re.compile(imgR)
imageList = cimgR.findall(date)
x = 0
for image in imageList:
urllib.urlretrieve(image,"%s.jpg"% x)
x = x +1
return imageList[1]
html = getHtml("https://tieba.baidu.com/p/5608328332")

Python下載圖片小程序