python 爬取图片

python 爬取图片

# -*- coding: UTF-8 -*-
import json
import urllib
import urllib2

import os
import requests

def getkey(keyWord):
    keyWord=urllib.quote(keyWord)
    print(keyWord)
    # url="https://i01piccdn.sogoucdn.com/4f78a63a42a2f3d9"
    # url="https://pic.sogou.com/pics?query=%20%E6%B1%89%E6%9C%8D&w=05009900&p=&_asf=pic.sogou.com&_ast=155122993"
    # url="https://pic.sogou.com/pics?query="+keyWord+"&w=05009900&p=&_asf=pic.sogou.com&_ast=1551229939&sc=index&sut=6386&sst0=1551229939224"
    url="https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=cos&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=&z=&ic=&hd=&latest=&copyright=&word=cos&s=&se=&tab=&width=&height=&face=&istype=&qc=&nc=1&fr=&expermode=&force=&pn=60&rn=30&gsm=3c&1551236226042="
    # url = 'https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=' + keyWord + '&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&word=' + keyWord + '&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&cg=wallpaper&pn=0&rn=60s&gsm=1e&1534226537567='
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'}
    try:
        request=urllib2.Request(url,headers=headers)
        open=urllib2.urlopen(request,timeout=20).read()
        loads=json.loads(open)
        print  loads["data"]
        return  loads["data"]
    except urllib2.URLError, e:
        if hasattr(e,"code"):
            print(e.code)
        if hasattr(e,"reason"):
            print e.reason
def getImage(dataList,localPath):
    uipath=unicode(localPath,"UTF-8")
    if not os.path.exists(uipath):
        os.mkdir(uipath)
    i=0
    for i in range(i,len(dataList)):
          try:
            path=str(dataList[i]["thumbURL"])
            print path
          except KeyError,e:
            print"没有找到路径"
            continue
          if path!=None:
            print("正在下载 %s"%path )
            try:
                ir = requests.get(path)
                open(uipath + '%d.jpg' % i, 'wb').write(ir.content)
                i += 1
            except Exception, e:
                print '路径显示有问题,无法加载图片'
                continue
            else:
                print '无路径,无法加载图片'

if __name__ == '__main__':
    #你要在百度图片查找的名称
 dataList = getkey('cosplay')
    #本地地址
 getImage(dataList, 'D:/python/FileImage/')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Angzush

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值