Python爬取校花网源码

import re
import requests
'''
想要学习Python?Python学习交流群:1004391443满足你的需求,资料都已经上传群文件,可以自行下载!
'''
# 拿到校花网主页的内容
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('爬取成功一张')

 

展开阅读全文

没有更多推荐了,返回首页