# coding:utf-8 import requests import re import time proxies = { "http": "http://124.131.242.155:80" } headers = {'content-type': 'application/json', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36', 'host':'jw.zzti.edu.cn', 'Accept - Language':'zh - CN, zh;q = 0.8', 'Accept - Encoding': 'gzip, deflate, sdch' } url = 'http://luoyang.cncn.com/photo/' r = requests.get(url,headers=headers).text.encode("utf8") print r list_url_list = re.findall('data-original="(.*?.jpg)"', r) i = 1 for each in list_url_list: print "正在下载中 :"+ each # 休息10秒钟再去下载 time.sleep(1) pic = requests.get(each, headers=headers) # 打印出来是否请求成功,成功后返回状态码 200 print pic.status_code fp = open('pic\' + str(i) + '.jpg', 'wb') fp.write(pic.content) fp.close() i += 1 print i
此实例具有时效性,可能过一点时间网站更换主页,可能就不能实现抓取图片,注意在项目中新建 pic文件夹