python requests用url爬妹子套图保存在文件夹

参考文章:http://cuiqingcai.com/3179.html

代码可封装成函数,此仅为学习

第一种:

import requests
headers = {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
           'Accept-Encoding':'gzip, deflate, sdch',
           'Accept-Language':'zh-CN,zh;q=0.8',
           'Cache-Control':'max-age=0',
           'Connection':'keep-alive',
           'Host':'i.meizitu.net', # host不使用也可以,但是如果用,要用正确
           'Referer':'http://www.mzitu.com/100761/3', # 判断上一级地址,此网站防爬虫方式就是以此为判断依据的
           'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
           }
for i in range(1,66):
    yema = '%02d' % i # 1~10要显示为01,02,03
    response = requests.get('http://i.meizitu.net/2017/08/20b' + yema + '.jpg',headers = headers)
    print '正在爬取第' + yema + '张......'
    img = response.content # 取二进制的图片内容用.content
    name = u'C:/Users/Test/Desktop/妹子/' + yema + '.jpg'
    with open(name,'ab') as f: # 二进制文件,用ab
        f.write(img)
print name # 检查name字符串是否显示正确
print type(name) # 检查加u后name的类型

第二种:

自动获取桌面位置并保存

# coding:utf-8
import requests,os

def Get_Desktop_Path():
    return os.path.join(os.path.expanduser("~"), 'Desktop')
try:
    os.mkdir(Get_Desktop_Path() + '\meizitu')
except BaseException as e:
    print e
wjj = Get_Desktop_Path() +'\meizitu' # 打印文件夹地址
print wjj

headers = {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
           'Accept-Encoding':'gzip, deflate, sdch',
           'Accept-Language':'zh-CN,zh;q=0.8',
           'Cache-Control':'max-age=0',
           'Connection':'keep-alive',
           'Host':'i.meizitu.net', # host不使用也可以,但是如果用,要用正确
           'Referer':'http://www.mzitu.com/100761/3', # 判断上一级地址,此网站防爬虫方式就是以此为判断依据的
           'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
           }
for i in range(1,10):
    yema = '%02d' % i # 1~10要显示为01,02,03
    response = requests.get('http://i.meizitu.net/2017/08/20b' + yema + '.jpg',headers = headers)
    print '正在爬取第' + yema + '张......'
    img = response.content # 取二进制的图片内容用.content
    name = wjj + '/' + u'惊不惊喜?意不意外?刺不刺激?' + yema + '.jpg'
    print name
    with open(name,'ab') as f: # 二进制文件,用ab
        f.write(img)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值