python之curl命令读取elasticsearch数据

在shell中curl命令格式要求严格,细节之处以记录之
from subprocess import Popen, PIPE
import json
import codecs
import os
count = 0
file_count = 0
for i in range(600):
    cmd_shell= u"curl -XGET -H \"Content-Type:application/json\" -u elastic:aJh4HAugLsHT  es-cn-mp90zgtkc000kh34w.public.elasticsearch.aliyuncs.com:9200/content_pool_v3/_search?scroll=10m -d '{\"from\":\"%s\",\"size\":\"1000\"}\'"%(count)
    p = Popen(cmd_shell, stdout=PIPE,stderr=PIPE,stdin=PIPE,shell=True)
    json_data = json.loads(p.stdout.read())
    _scroll_id = json_data['_scroll_id']
    json_data_hits = json_data['hits']['hits']
    for i in range(len(json_data_hits)):
        title = json_data_hits[i]['_source']['title']
        summary = json_data_hits[i]['_source']['summary']
        region_tags = ",".join(json_data_hits[i]['_source']['region_tags'])
        try:
            filename = str(file_count) + "_summary_.txt"
            with codecs.open(filename, "a+", 'utf-8') as wfp:
                if round(os.path.getsize(filename) / float(1024 * 1024), 2) > 10:
                    file_count = file_count + 1
                wfp.write(title + u"。" + u"\n")
                wfp.write(summary + u"\n")
                wfp.write(region_tags + u"。" + u"\n")
        except:
            pass


    while True:
        cmd_shell = u"curl -XGET -H \"Content-Type:application/json\" -u elastic:aJh4HAugLsHT es-cn-mp90zgtkc000kh34w.public.elasticsearch.aliyuncs.com:9200/_search/scroll -d '{\"scroll\":\"10m\",\"scroll_id\":\"" + str(_scroll_id) + u"\"}'"
        p = Popen(cmd_shell,stdout=PIPE, stderr=PIPE, stdin=PIPE, shell=True)
        json_data = json.loads(p.stdout.read())
        if '_scroll_id' in json_data:
            _scroll_id = json_data['_scroll_id']
        else:
            break
        json_data_hits = json_data['hits']['hits']
        for i in range(len(json_data_hits)):
            title = json_data_hits[i]['_source']['title']
            print(title)
            summary = json_data_hits[i]['_source']['summary']
            region_tags = ",".join(json_data_hits[i]['_source']['region_tags'])
            try:
                filename = str(file_count) + "_summary_.txt"
                with codecs.open(filename, "a+", 'utf-8') as wfp:
                    if round(os.path.getsize(filename) / float(1024 * 1024), 2) > 10:
                        file_count = file_count + 1
                    wfp.write(title + u"。" + u"\n")
                    wfp.write(summary + u"\n")
                    wfp.write(region_tags + u"。" + u"\n")
            except:
                pass
        #content = i_json_data['_source']['content']
    count = count + 1000


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值