今日,同事写一个脚本,python老师中文乱码各种解决,都不行,求我协助,我这么好心的人自然不会袖手旁观,网上搜了一万种方法,
终于摸索出来了一种, 主要是 data = json.dumps(data_) headers = {'content-type': 'charset=utf8'}, 之前一直用的是post, 如今改成post2就OK了,只怪python学艺不精啊。。。。
# -*- coding: utf-8 -*-import jsonimport sysimport urllibimport urllib2reload(sys)sys.setdefaultencoding('utf8')def post(url, data_): data = urllib.urlencode(data_) req = urllib2.Request(url=url, data=data) res = urllib2.urlopen(req) res_ = res.read() return res_ def post2(url, data_): data = json.dumps(data_) headers = {'content-type': 'charset=utf8'} req = urllib2.Request(url=url, data=data, headers=headers) res = urllib2.urlopen(req) res_ = res.read() return res_def get_scene(): match = {'mid': "12ced2cf001200000f4243dd"} project = {'name': 1, 'ctime': 1, 'mid': 1} param = {'match': match, 'project': project} scene_response = post("http://xxx/search.json", param) scene_resp = json.loads(scene_response.encode("utf-8")) if scene_resp['e']['code'] == 0: keyword = scene_resp['data'][0]['name'] ctime = scene_resp['data'][0]['ctime'] param = {"type": 18, "uid": "10741992", "histories": {"keyword": keyword, "ct": ctime, "keywordId": "12ced2cf001200000f4243dd"}} res = post2("http://yyy/insert_unification_search.json", param2)if __name__ == '__main__': get_scene();