使用python爬取百度的表情包

import requests
import os
import time

for n in range(1,3):

m = 1
p = int(input(“请输入你要爬取的页数:”))
for n in range (1,p+1):
#通过抓包获取url
url = “http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E8%A1%A8%E6%83%85&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=&z=&ic=&word=%E8%A1%A8%E6%83%85&s=&se=&tab=&width=&height=&face=&istype=&qc=&nc=1&fr=&pn={}&rn=30&gsm=186&1534833478899=”.format(n*60)

headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36","Referer":"http://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=%E8%A1%A8%E6%83%85","Host":"image.baidu.com",'Accept-Language':"en-US,en;q=0.9","Accept-Encoding":"gzip, deflate","Accept":"text/plain, */*; q=0.01"}
#请求url,得到参数
response = requests.request("get",url,headers=headers)
#格式,不然会乱码
response.encoding = "utf-8"
#获取图片的个数
b = len(response.json()['data'])-2 
for i in range(b):

    # imgurl = (response.json()['data'][i]['middleURL'])
    # print(imgurl)
    # imgurl=response.json()['data'][i]['middleURL']
    # print (imgurl)
    #请求图片的而连接
    data = requests.get(response.json()['data'][i]['middleURL'])
    # print(response.json()['data'][i]['middleURL'])
    
    # print(response)
    
    
    #检测文件夹,如果没有就去创建
    folder = os.path.exists("E:\\img\\")
    if not folder:
        os.makedirs("E:\\img\\")
    else:
        print ("========================")
        
        #保存图片
        
        
        with open ("E:\\img\\"+str(m)+'.jpg','wb') as f:
            # print(m)
            
            f.write(data.content)
            print("开始下载:"+"第{}张".format(m))
            time.sleep(1)
            m+=1

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值