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('爬取成功一張')
相關文章
相關標籤/搜索