# 環境python 3.5.2 + beautifulsoup
# 準備工作:瞭解 python 自帶的模塊 urllib + beautifulsoup 的網頁解析
#爬取了一個圖片網站的部分圖片
from urllib import request
from bs4 import BeautifulSoup
import os
import os.path
import re
try:
response = request.urlopen('https://www.4493.com/weimeixiezhen/') #使用urlopen方法下載網頁
soup = BeautifulSoup(response,'html.parser', from_encoding = 'utf-8')
print("來自源URL要爬取的所有網頁")
links = soup.find_all('a', href = re.compile(r"/weimeixiezhen/120"))
count = 1 # 記錄下載的圖片數目
for link in links:
urls = link['href']
print(urls)
url = 'https://www.4493.com'+urls
print("具體網頁: ", url)
try:
response_1 = request.urlopen(url)
soup_1 = BeautifulSoup(response_1,'html.parser', from_encoding = 'utf-8')
img = soup_1.find('img', onload = re.compile(r"btn"))
except request.HTTPError as e:
print(e.code)
except request.URLError as e:
print(e.reason)
p = os.path.join('E:\\', "MV_img")
if not os.path.exists(p): # 判斷當前目錄是否存在
os.mkdir(p) # 如果存在,才創建新的目錄
url = img['src'] # 獲取圖片的資源鏈接url
request.urlretrieve(url, 'E:\\MV_img\\MM_%s.jpg' %count) # 根據圖片的資源鏈接下載圖片
print('第%d張圖片下載完成'%count)
count += 1
except request.HTTPError as e :
print(e.code)
except request.URLError as e :
print(e.reason)
else:
print("ok")