python基础(json,socket)
2015-12-01 16:21
344 查看
http://python-data.dr-chuck.net/geojson?sensor=false&address=Vilnius+University
![](http://images2015.cnblogs.com/blog/642837/201512/642837-20151201162103030-1682088386.jpg)
http://python-data.dr-chuck.net/comments_194528.json
{ "results" : [ { "address_components" : [ { "long_name" : "Vilnius", "short_name" : "Vilnius", "types" : [ "locality", "political" ] }, { "long_name" : "Vilnius city municipality", "short_name" : "Vilnius city municipality", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "Vilnius County", "short_name" : "Vilnius County", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "Lithuania", "short_name" : "LT", "types" : [ "country", "political" ] } ], "formatted_address" : "Vilnius, Lithuania", "geometry" : { "bounds" : { "northeast" : { "lat" : 54.832544, "lng" : 25.4814883 }, "southwest" : { "lat" : 54.567798, "lng" : 25.024376 } }, "location" : { "lat" : 54.6871555, "lng" : 25.2796514 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 54.832544, "lng" : 25.4814883 }, "southwest" : { "lat" : 54.567798, "lng" : 25.024376 } } }, "partial_match" : true, "place_id" : "ChIJ9QhkXPuT3UYRQNzpcIzRAAQ", "types" : [ "locality", "political" ] } ], "status" : "OK" }
import urllib import json url = 'http://python-data.dr-chuck.net/geojson?' while True: address = raw_input() serviceUrl = url + urllib.urlencode({'sensor': 'false', 'address': address}) print serviceUrl input = urllib.urlopen(serviceUrl).read() js = json.loads(input) print js['results'][0]['place_id']
![](http://images2015.cnblogs.com/blog/642837/201512/642837-20151201162103030-1682088386.jpg)
http://python-data.dr-chuck.net/comments_194528.json
{ "note":"This file contains the actual data for your assignment</note>", "comments":[ { "name":"Anastasija", "count":100 }, { "name":"Ridwan", "count":98 }, { "name":"Rory", "count":88 }, { "name":"Aeryn", "count":88 }, { "name":"Marcus", "count":84 }, { "name":"Jock", "count":83 }, { "name":"Talise", "count":81 }, { "name":"Rheanne", "count":80 }, { "name":"Wardah", "count":78 }, { "name":"Karyss", "count":74 }, { "name":"Mea", "count":70 }, { "name":"Maryk", "count":69 }, { "name":"Neshawn", "count":69 }, { "name":"Miyah", "count":63 }, { "name":"Janelle", "count":63 }, { "name":"Finnen", "count":60 }, { "name":"Michela", "count":59 }, { "name":"Yishuka", "count":57 }, { "name":"Kern", "count":57 }, { "name":"Tanner", "count":57 }, { "name":"Zhong", "count":56 }, { "name":"Merin", "count":54 }, { "name":"Jian", "count":53 }, { "name":"Khadijah", "count":52 }, { "name":"Lauren", "count":49 }, { "name":"Adam", "count":47 }, { "name":"Akan", "count":43 }, { "name":"Shauni", "count":43 }, { "name":"Klein", "count":40 }, { "name":"Brett", "count":38 }, { "name":"Ayat", "count":37 }, { "name":"Rayna", "count":35 }, { "name":"Duaa", "count":34 }, { "name":"Mariyah", "count":32 }, { "name":"Matthias", "count":30 }, { "name":"Olurotimi", "count":26 }, { "name":"Odynn", "count":23 }, { "name":"Shahna", "count":21 }, { "name":"Saphyre", "count":20 }, { "name":"Bowie", "count":19 }, { "name":"Keera", "count":18 }, { "name":"Ross", "count":14 }, { "name":"Caragh", "count":13 }, { "name":"Siena", "count":12 }, { "name":"Carley", "count":9 }, { "name":"Waqaas", "count":9 }, { "name":"Kira", "count":9 }, { "name":"Avinash", "count":3 }, { "name":"Alysia", "count":1 }, { "name":"Josephina", "count":1 } ] }
import urllib import json url = 'http://python-data.dr-chuck.net/comments_194528.json' input = urllib.urlopen(url).read() js = json.loads(input) sum = 0 for dict in js['comments']: sum += int(dict['count']) print sum
import socket import time mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) mysock.connect(('news.xinhuanet.com', 80)) mysock.send('GET http://news.xinhuanet.com/photo/2015-12/01/128485250_14489244150011n.jpg HTTP/1.0\n\n') count = 0 picture = ""; while True: data = mysock.recv(5120) if ( len(data) < 1 ) : break # time.sleep(0.25) count = count + len(data) print len(data),count picture = picture + data mysock.close() # Look for the end of the header (2 CRLF) pos = picture.find("\r\n\r\n") print 'Header length',pos print picture[:pos] # Skip past the header and save the picture data picture = picture[pos+4:] fhand = open("stuff.jpg","wb") fhand.write(picture) fhand.close()
相关文章推荐
- 检测输入的是几位数
- Python+Selenium自动化
- python 基本操作 多维数组 循环
- python根据百度地图api将地址转成经纬度
- The Zen of Python
- 决定配置完整的python ubuntu14.04 python-dev distribute
- Python 多线程学习03
- Python基础——使用with结构打开多个文件
- leetcode之Palindrome Linked List
- Python的几个开
- python [3.2] urllib的使用
- Python 多线程学习02
- python入门--基本数据类型
- 解决vim中Python代码不能识别中文
- Python——作用域基础
- python里爬取页面中图片命令
- 【知识】Python字符串操作
- python学习笔记
- 学好 Python 的 11 个优秀资源
- 远程调试Python进程的小工具