python抓取百度图片

#!/usr/bin/env python3
#coding=utf-8
import re
import requests
import os
def dowmloadPic(html, keyword):
    pic_url = re.findall('"objURL":"(.*?)",', html, re.S)
    global num
    print('find:  ' + keyword + '  pictures ')
    for each in pic_url:
        print('Downloading' + str(num) + 'st  url:' + str(each))
        try:
            pic = requests.get(each, headers=headers,timeout=10)
        except requests.exceptions.ConnectionError:
            print('Error  this picture cant download')
            continue
 
        image_name = '%4d.jpg'%num
        dir = os.path.join(outp_dir,image_name)
        fp = open(dir, 'wb')
        fp.write(pic.content)
        fp.close()
        num += 1
 
 
if __name__ == '__main__':
    word = '马赛克的图片'
    outp_dir = '/home/tarena/projects/'
    #url = 'http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=' + word + '&ct=201326592&v=flip'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Mobile Safari/537.36'}
    pagestart = 0
    pageend = 100
    num = 0
    '''添加代理
    proxies = {
        http:'http://...'
        https:'https://...'
    }
    '''
    for idx in range(pagestart,pageend):
        url = 'http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word='+word+'&pn=%d&gsm=3c&ct=&ic=0&lm=-1&width=0&height=0'%idx*20
        #result = requests.get(url,headers=headers,proxies=proxies)
        result = requests.get(url, headers=headers)
        dowmloadPic(result.text, word)
        if num >=1000:
            break
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值