1. 程式人生 > >下載捧腹網圖片

下載捧腹網圖片

bsp open 表示 meta dal content png images -type

# 鎖薇,捧腹網

import urllib.request, re# , sys

# 獲取網頁源碼
def page(pg):
	url = ‘https://www.pengfu.com/index_%s.html‘%pg
	# 頁面是utf8編碼,所有解碼成unicode
	html = urllib.request.urlopen(url).read().decode(‘utf8‘) # <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	# print(html)
	return html

# 獲取標題
def title(html):
	reg = re.compile(r‘<h1 class="dp-b"><a href=".*?" target="_blank">(.*?)</a>‘) # r表示防止轉義
	item = re.findall(reg, html)
	# print(item)
	return item

# 獲取圖片url
def content(html):
	# html = page(1)
	reg = r‘<img src="(.*?)" width=‘
	item = re.findall(reg, html)
	# print(item)
	return item

def download(url, name):
	path = ‘image\%s.jpg‘%name#.decode(‘utf-8‘).encode(‘gbk‘) # win下只識別gbk
	urllib.request.urlretrieve(url, path)

for i in range(5,9):
	html = page(i)
	title_list = title(html)
	content_list = content(html)
	for m, n in zip(title_list, content_list): # 把標題和圖片對個對應
		print(‘正在下載>>>>>:‘ + m, n)
		download(n, m)



	

  

技術分享

下載捧腹網圖片