python版本:3.5
源代码:
#coding=utf8
import re
import requests
import os
'''
爬取当当网五星级图书榜的图书链接,并下载图片到一个目录当中
网址:http://bang.dangdang.com/books/fivestars/01.00.00.00.00.00-recent30-0-0-1-1
'''
for page in range(1,26):
url = 'http://bang.dangdang.com/books/fivestars/01.00.00.00.00.00-recent30-0-0-1-' + str(page)
html = requests.get(url).text
#图片链接所对应的正则表达式
pat = '<div class="pic"><a href="(.*?)" target="_blank"><img src="(.*?)" alt=".*?"\s*title=".*?"/></a></div> '
pic_url = re.findall(pat,html,re.S)
for i in range(0,len(pic_url)):
each = pic_url[i]
print(each)
try:
pic= requests.get(each[1], timeout=10)
except requests.exceptions.ConnectionError:
print('【错误】当前图片无法下载')
continue
path = 'E:/pictest/dangdang_5star/'
#查看该目录是否存在,若不存在,则建一个
if not os.path.exists(path):
os.mkdir(path)
file = path + str(page) + '_' + str(i + 1) + '.jpg'
fp = open(file,'wb')
fp.write(pic.content)
fp.close()