python URL 处理练习
来源:互联网 发布:php九九乘法表表格 编辑:程序博客网 时间:2024/06/07 04:53
https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13801380000
将get请求json结果转换为字典
import jsonimport urllibimport jsonmobile_url='https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13801380000'#打开一个URLmoblie_res = urllib.urlopen(mobile_url)#readres_str=moblie_res.read().decode('gbk').encode('UTF-8')#print type(res_str) #---> <type 'str'># print moblie_res.read().decode('gbk')# __GetZoneResult_ = {# mts:'1380138',# province:'北京',# catName:'中国移动',# telString:'13801380000',# areaVid:'29400',# ispVid:'3236139',# carrier:'北京移动'# }#写函数,找到字符串中json开始和结束的索引def json_start(str_tmp,str_s): for i in range(0, len(res_str)): if str_tmp[i]==str_s: return i#引用函数start = json_start(res_str,'{')end = json_start(res_str,'}')+1str_tmp =res_str[start:end]#print str_tmp#{# mts:'1380138',# province:'北京',# catName:'中国移动',# telString:'13801380000',# areaVid:'29400',# ispVid:'3236139',# carrier:'北京移动'# }#写函数,去掉字符串中的空格,空行,换行符,及加引号def delete_space(str_tmp): new_str = '' for i in range(0,len(str_tmp)): #去掉字符串中的空格,空行,换行符 if str_tmp[i]=='{': new_str = new_str + str_tmp[i]+"\"" elif str_tmp[i]==',': new_str = new_str + str_tmp[i] + "\"" elif str_tmp[i]==':': new_str = new_str + "\""+str_tmp[i] elif str_tmp[i]=='\r': pass elif str_tmp[i]==' ': pass elif str_tmp[i]=='\n': pass elif str_tmp[i]=='\t': pass else: new_str = new_str+str_tmp[i]#调用函数处理字符串new_str = delete_space(str_tmp)print new_str,type(new_str)#{"mts":'1380138',"province":'北京',"catName":'中国移动',"telString":'13801380000',"areaVid":'29400',"ispVid":'3236139',"carrier":'北京移动'}#将字符串转换成json字符串#res_dict1=json.dumps(new_str)#print "json:"+res_dict1,type(res_dict1)#将json 转换成字典??????#res_dict=json.loads(res_dict1)#print "dict:"+res_dict,type(res_dict)#json:"{\"mts\":'1380138',\"province\":'\u5317\u4eac',\"catName\":'\u4e2d\u56fd\u79fb\u52a8',\"telString\":'13801380000',\"areaVid\":'29400',\"ispVid\":'3236139',\"carrier\":'\u5317\u4eac\u79fb\u52a8'}" <type 'str'>#dict:{"mts":'1380138',"province":'北京',"catName":'中国移动',"telString":'13801380000',"areaVid":'29400',"ispVid":'3236139',"carrier":'北京移动'} <type 'unicode'>res_dict=eval(new_str)print "dict:",res_dict, type(res_dict)#dict: {'province': '\xe5\x8c\x97\xe4\xba\xac', 'mts': '1380138', 'ispVid': '3236139', 'telString': '13801380000', 'catName': '\xe4\xb8\xad\xe5\x9b\xbd\xe7\xa7\xbb\xe5\x8a\xa8', 'carrier': '\xe5\x8c\x97\xe4\xba\xac\xe7\xa7\xbb\xe5\x8a\xa8', 'areaVid': '29400'} <type 'dict'>
http://www.crifan.com/files/doc/docbook/json_tutorial/release/html/json_tutorial.html
0 0
- python URL 处理练习
- python中的url处理
- python处理url中文问题
- python处理浏览器URL编码
- python URLObject url处理模块
- 关于python的url处理
- 《Python自然语言处理》练习1
- Python执行DOS命令以及处理url
- Python练习——数组处理
- Python爬虫练习第三章【百度URL采…
- python+处理日志+处理URL防止SQL注入
- 【python 爬虫】python中url链接编码处理方法
- 【转】Python urllib2.urlopen打开中文url的编码处理
- python爬虫中对含中文的url处理
- Python CGi URL 中文以及特殊转义字符的处理
- 怎样处理python爬虫中有中文的url
- python的web 开发基础之url处理
- Python 小甲鱼教程 课后练习33 异常处理
- Android Studio中Button等控件的Text中字符串默认大写的解决方法
- Linux 常用命令
- 指向常量的指针和指针常量
- 机器学习与R语言(一)——Ubuntu下R语言的安装以及R语言的基本语法【待续】
- IDEA:Application Server was not connected before run configuration stop, reason: Unable to ping...
- python URL 处理练习
- 使用expect进行 rsync进行服务器同步备份
- android开发之AccessibilityService
- 第十周(Dynamic ProgrammingIV)
- 圆角选中的tablayout
- Android的四种启动模式,关系项目的生死的至关重要的一点
- java高级_工具类_日期类
- Spark Hive SQL实例
- oracle定时器