Python之爬虫-校花网

Python之爬虫-校花网

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import re
import 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('爬取成功一张')
相关文章
相关标签/搜索