python请求乱码的示例

今日,同事写一个脚本,python老师中文乱码各种解决,都不行,求我协助,我这么好心的人自然不会袖手旁观,网上搜了一万种方法,

终于摸索出来了一种, 主要是  data = json.dumps(data_)  headers = {'content-type': 'charset=utf8'}, 之前一直用的是post, 如今改成post2就OK了,只怪python学艺不精啊。。。。

 

# -*- coding: utf-8 -*-

import json
import sys
import urllib
import urllib2


reload(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();



 

转载于:https://my.oschina.net/zz006/blog/1935224

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值