百度抓图(python)

import requests
import re
import os
import urllib

keyword = ''


def getBaiduImage(url):
    try:
        rsp = requests.get(url)
        rsp.raise_for_status() 
    except:
        print('对不起,百度图片访问失败!程序退出')
        return
    pic_urls = re.findall('"objURL":"(.*?)",', rsp.text, re.S)
    nextpage=re.findall(re.compile(r'<a href="(.*)" class="n">下一页</a>'),rsp.text,flags=0)
    next_urls='http://image.baidu.com/'+nextpage[0]
    sl=downLoadImage(pic_urls,0)
    while (nextpage!=''):
        rsp = requests.get(next_urls)
        pic_urls = re.findall('"objURL":"(.*?)",', rsp.text, re.S)
        sl=downLoadImage(pic_urls,sl)
        if(sl>200):
            break
        nextpage=re.findall(re.compile(r'<a href="(.*)" class="n">下一页</a>'),rsp.text, flags=0)
        next_urls='http://image.baidu.com/'+nextpage[0]

def downLoadImage(pictures,cnt):
    count=cnt
    if not os.path.exists(keyword):
        os.mkdir(keyword)
    imgs = pictures
    for i in imgs:
        try:
           url = i 
           img = requests.get(url, timeout = 15) 
           img = img.content
           form=i[i.rfind('.'):]
           FileName = keyword+'\\'+keyword+str(count)+form
           file =  open(FileName,'bw') 
           file.write(img) 
           print('No.%d success' % count) 
        except :
           print('No.%d failed'%count)
           continue
        count += 1
    return count
        
# 用图片进行测试
  # 关键词, 改为你想输入的词即可
keyword=input("输入需要查找的主题")
url_init_first = 'http://image.baidu.com/search/flip?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1497491098685_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&ctd=1497491098685%5E00_1519X735&word='
url= url_init_first + urllib.parse.quote(keyword, safe='/')

getBaiduImage(url)
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值