# 环境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")