- 微信企业号api
#!/usr/bin/env python
#coding=utf-8
import os,urllib2,requests,sys,time,json,pickle
corpid = 'x'
secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
class Token(object):
"""Get AccessToken"""
def __init__(self, corpid, secret):
self.baseurl = 'url?corpid={0}&corpsecret={1}'.format(corpid, secret)
self.expire_time = sys.maxint
def get_token(self):
if self.expire_time > time.time():
request = urllib2.Request(self.baseurl)
response = urllib2.urlopen(request)
ret = response.read().strip()
ret = json.loads(ret)
#if 'errcode' in ret.keys():
# print >> ret['errmsg'], sys.stderr
# sys.exit(1)
self.expire_time = time.time() + ret['expires_in']
self.access_token = ret['access_token']
return self.access_token
def SendMsg(user, title, content):
gettoken = Token(corpid=corpid, secret=secret).get_token()
url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}".format(gettoken)
varload = {
"touser":"{0}".format(user),
"msgtype":"text",
"agentid":"1",
"text":{
"content":"{0}\n{1}".format(title, content)
},
"safe":"0"
}
ret = requests.post(url, data=json.dumps(varload, ensure_ascii=False))
print ret.json()
#if __name__ == '__main__':
SendMsg(sys.argv[1],sys.argv[2],sys.argv[3])
str(hit["_source"]).decode('unicode-escape')
其中hit["_source"]是dict类型,str()函数将其转换成字符串
#! /usr/bin/python
# -*- coding: utf-8 -*-
# coding=utf-8
'''
Created on 2017年2月16日
@author: u01
'''
import sys
from elasticsearch import Elasticsearch
print sys.getdefaultencoding();
es = Elasticsearch([
'http://conf:9200/'
])
page = es.search(
index='ware_index',
doc_type='ware_type',
scroll='2m',
search_type='scan',
size=1000,
body={
"query":{
"match":{"text":"菠萝"}
},
"filter":{
"term":{
"shop":"230"
}
}
}
)
def print_page(page):
for hit in page['hits']['hits']:
contentjson=str(hit["_source"]).decode('unicode-escape')
f=open('f.txt','w')
f.write(contentjson+'\n')
print(contentjson)
sid = page['_scroll_id']
scroll_size = page['hits']['total']
# Start scrolling
while(scroll_size >0):
print "Scrolling..."
page = es.scroll(scroll_id = sid, scroll ='2m')
print_page(page)
# Update the scroll ID
sid = page['_scroll_id']
# Get the number of results that we returned in the last scroll
scroll_size = len(page['hits']['hits'])
print "scroll size: "+ str(scroll_size)
# Do something with the obtained page