本文共 727 字,大约阅读时间需要 2 分钟。
#!/usr/bin/env python# -*- coding:utf-8 -*-import reimport requests# 拿到校花网主页的内容response = requests.get('http://www.xiaohuar.com/')data = response.text# 拿到校花网所有的图片链接results = re.findall('lazysrc="(.*?)"', data)for result in results: # type:str # 判断是不是有链接的 if result.startswith('htt'): pass else: img_result = 'http://www.xiaohuar.com/' + result # 获取图片内容 img_response = requests.get(img_result) img_data = img_response.content img_name = result.split('/')[3] img_filename = img_name + '.jpg' print(img_filename) # 保存图片内容 with open(img_filename, 'wb') as f: # write,read,wb是写入二进制 f.write(img_data) print('爬取成功一张')
转载地址:http://hzgyz.baihongyu.com/