Python Tutorial - Parse JSON Objects with Python
This tutorial is copied from youtube.com
Here is the link: http://www.youtube.com/watch?v=RXqo3lC-JPI&list=PL6-GrNvaJuAhLWFJVwCC2qHCECEWUJtU6&index=5
I uploaded it to youku.com
Hope you guys enjoy it.
And here is the source codes:
Take notice of the list comprehensions :
[v for k, v in something.items() if type(v)==unicode and v!='ok']
下面的是在python promote 里面做的。
alex@universe ~/sandbox/env_27_bottle/bottle_json $ ../bin/python
Python 2.7.3 (default, Apr 10 2013, 06:20:15)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> import json
>>> urllib.urlopen('http://www.similarsitesearch.com/api/similar/ebay.com').read()
'{"num":20,"status":"ok","r0":"http:\\/\\/us.ebid.net\\/","r1":"http:\\/\\/www.auctionzip.com\\/","r2":"http:\\/\\/www.propertyroom.com\\/","r3":"http:\\/\\/www.trademe.co.nz\\/","r4":"http:\\/\\/www.bidspotter.com\\/","r5":"http:\\/\\/www.bidcactus.com\\/","r6":"http:\\/\\/www.purplewave.com\\/","r7":"http:\\/\\/www.beezid.com\\/","r8":"http:\\/\\/www.cqout.com\\/","r9":"http:\\/\\/www.shopgoodwill.com\\/","r10":"http:\\/\\/www.ha.com\\/","r11":"http:\\/\\/www.rasmus.com\\/","r12":"http:\\/\\/www.webidz.com\\/","r13":"http:\\/\\/www.ebay.co.uk\\/","r14":"http:\\/\\/online-auction-sites.toptenreviews.com\\/","r15":"http:\\/\\/www.policeauctions.com\\/","r16":"http:\\/\\/www.quibids.com\\/","r17":"http:\\/\\/www.icollector.com\\/","r18":"http:\\/\\/www.ioffer.com\\/","r19":"http:\\/\\/www.auctionlotwatch.co.uk\\/"}'
>>> json.loads( urllib.urlopen('http://www.similarsitesearch.com/api/similar/ebay.com').read() )
{u'r16': u'http://www.quibids.com/', u'num': 20, u'status': u'ok', u'r17': u'http://www.icollector.com/', u'r14': u'http://online-auction-sites.toptenreviews.com/', u'r15': u'http://www.policeauctions.com/', u'r12': u'http://www.webidz.com/', u'r13': u'http://www.ebay.co.uk/', u'r10': u'http://www.ha.com/', u'r11': u'http://www.rasmus.com/', u'r18': u'http://www.ioffer.com/', u'r19': u'http://www.auctionlotwatch.co.uk/', u'r4': u'http://www.bidspotter.com/', u'r5': u'http://www.bidcactus.com/', u'r6': u'http://www.purplewave.com/', u'r7': u'http://www.beezid.com/', u'r0': u'http://us.ebid.net/', u'r1': u'http://www.auctionzip.com/', u'r2': u'http://www.propertyroom.com/', u'r3': u'http://www.trademe.co.nz/', u'r8': u'http://www.cqout.com/', u'r9': u'http://www.shopgoodwill.com/'}
>>> [v for k, v in json.loads( urllib.urlopen('http://www.similarsitesearch.com/api/similar/ebay.com').read() ).items() ]
[u'http://www.quibids.com/', 20, u'ok', u'http://www.icollector.com/', u'http://online-auction-sites.toptenreviews.com/', u'http://www.policeauctions.com/', u'http://www.webidz.com/', u'http://www.ebay.co.uk/', u'http://www.ha.com/', u'http://www.rasmus.com/', u'http://www.ioffer.com/', u'http://www.auctionlotwatch.co.uk/', u'http://www.bidspotter.com/', u'http://www.bidcactus.com/', u'http://www.purplewave.com/', u'http://www.beezid.com/', u'http://us.ebid.net/', u'http://www.auctionzip.com/', u'http://www.propertyroom.com/', u'http://www.trademe.co.nz/', u'http://www.cqout.com/', u'http://www.shopgoodwill.com/']
>>> [v for k, v in json.loads( urllib.urlopen('http://www.similarsitesearch.com/api/similar/ebay.com').read() ).items() if type(v)==unicode and v!='ok']
[u'http://www.quibids.com/', u'http://www.icollector.com/', u'http://online-auction-sites.toptenreviews.com/', u'http://www.policeauctions.com/', u'http://www.webidz.com/', u'http://www.ebay.co.uk/', u'http://www.ha.com/', u'http://www.rasmus.com/', u'http://www.ioffer.com/', u'http://www.auctionlotwatch.co.uk/', u'http://www.bidspotter.com/', u'http://www.bidcactus.com/', u'http://www.purplewave.com/', u'http://www.beezid.com/', u'http://us.ebid.net/', u'http://www.auctionzip.com/', u'http://www.propertyroom.com/', u'http://www.trademe.co.nz/', u'http://www.cqout.com/', u'http://www.shopgoodwill.com/']
可以参考下面我写的全文:
这里是全部写好的源代码:
还没有做错误处理,比如说用户输入的网站有问题,还有,用户的internet 没有链接上。
以后可以在except 里面着重强调一下。
先上代码为快:
'''
To search similar site
For example:
$python parseJson.py ebay.com
This will return a result like this blow:
[u'http://www.quibids.com/', u'http://www.icollector.com/', ... ] Coded by Spaceship9 '''
import urllib
import json
from os import sys
def searchSite(site):
'''
To return those similar sites which are the same as the variable site
'''
try:
# the variable site in this case should be like this
# It is a website domain name
# For instance: ebay.com
# To open the URL
# This will return a socket._fileobject which would look like
# <addinfourl at 11215648 whose fp = <socket._fileobject object at 0xaa7a50>>
site = 'http://www.similarsitesearch.com/api/similar/' + site
tmp = urllib.urlopen(site) # To get all the content of the response
# In this case, we will get a JSON-like dictionary object
# It would look like
# '{"num":20,"status":"ok","r0":"http:\\/\\/us.ebid.net\\/","r1":"http:\\/\\/www.auctionzip.com\\/", ... }'
tmp = tmp.read() # Here the http:\\/\\/us.ebid.net\\/ refers to http://...
# Now we are going to make it be like http://
# To make it be a JSON object in python, which is in a format of dictionary in this case
tmp = json.loads(tmp) # Now print them out using list comprehension
# under some conditions
return [ v for k,v in tmp.items() if type(v)==unicode and v!='ok' ]
except:
return "Please check your internet connect or the website spelling :)" if __name__ == "__main__":
if len(sys.argv[:])==2:
print searchSite(sys.argv[1])
else:
print "Please input one site every time you want to search. :)"
当然吧上面的代码全部写道一行里面,也不是不行,是可以的。但是觉得是没有必要的。看起来太负责。之所以分开写,是为了便于理解。
下面是效果图。可以看一下
一个比较简单的输出网站相似的功能。使用的是别的网站的API,很好用的说,是JSON版的哈哈。
这里,用来查看帮助:
弄得像个样子,呵呵。可以做一个简单的搜索同类网站的小程序了。还不错吧。
Hv Fun
Happy Coding
Python Tutorial - Parse JSON Objects with Python的更多相关文章
- Python Tutorial 学习(二)--Using the Python Interpreter
Using the Python Interpreter 2.1. Invoking the Interpreter The Python interpreter is usually install ...
- Python基础之JSON
作用 对Python对象进行序列化,便于存储和传输 Python对象与JSON字符串相互转换 Python对象转JSON字符串 import json data = [ { 'a' : 1, 'b' ...
- [译]The Python Tutorial#7. Input and Output
[译]The Python Tutorial#Input and Output Python中有多种展示程序输出的方式:数据可以以人类可读的方式打印出来,也可以输出到文件中以后使用.本章节将会详细讨论 ...
- Python Tutorial 学习(八)--Errors and Exceptions
Python Tutorial 学习(八)--Errors and Exceptions恢复 Errors and Exceptions 错误与异常 此前,我们还没有开始着眼于错误信息.不过如果你是一 ...
- Python Tutorial 学习(七)--Input and Output
7. Input and Output Python里面有多种方式展示程序的输出.或是用便于人阅读的方式打印出来,或是存储到文件中以便将来使用.... 本章将对这些方法予以讨论. 两种将其他类型的值转 ...
- 【转】Multithreaded Python Tutorial with the “Threadworms” Demo
The code for this tutorial can be downloaded here: threadworms.py or from GitHub. This code works wi ...
- [Python 3.x 官方文档翻译]The Python Tutorial Python教程
Python is an easy to learn, powerful programming language. It has efficient high-level data structur ...
- Introspection in Python How to spy on your Python objects Guide to Python introspection
Guide to Python introspection https://www.ibm.com/developerworks/library/l-pyint/ Guide to Python in ...
- [译]The Python Tutorial#10. Brief Tour of the Standard Library
[译]The Python Tutorial#Brief Tour of the Standard Library 10.1 Operating System Interface os模块为与操作系统 ...
随机推荐
- [DevEpxress]GridControl 显示Gif动画
原文:[DevEpxress]GridControl 显示Gif动画 如果没有对进行设置,那么GridControl列中gif在编辑状态下,才能显示动画效果,如果要设置列自动显示动画效果,可以进行如下 ...
- window批量-6 rem
行动: 凝视命令,加大对批量处理相应的描述性信息 格公式: rem [comment] demo: bat @echo off pause rem 这是对pause的解释 echo xxx pause ...
- .net使用cefsharp开源库开发chrome
.net使用cefsharp开源库开发chrome 离上篇写介绍pc端的混合开发和为什么以cefsharp入手研究混合开发已经有好几天,一直忙,抽不出时间继续写怎么搭建cefsharp开发环境.其实没 ...
- openwrt补丁
http://wiki.openwrt.org/doc/devel/patches 中文文档:http://andelf.diandian.com/post/2013-05-22/4005067737 ...
- [译]Java 设计模式之桥接
(文章翻译自Java Design Pattern: Bridge) 简单来说,桥梁设计模式是一个两层的抽象. 桥接模式就是从一个抽象中实现中解耦以便两个都可以独立的改变.桥接使用封装聚合而且使用继承 ...
- WPF随手小记之二 ——改变DataGrid样式
大部分时候我们并不需要手动改变DataGrid的样式,因为用默认的其实也看得过去,而且在DataGrid中的数据也远比外观重要. 但总有时候,我们需要做点必要的UI修改来使DataGrid更美观. 一 ...
- Plugin For KanColleViewer – Provissy Tools V1.0
これはKanColleViewerためのプラグインです,KanColleViewerの機能を拡張する. #介绍 / Introduction / 紹介这是一个KanColleViewer(俗称”提督很 ...
- javascript 函数介绍
javascript函数使用的时候,往往都比较单一,这里介绍几种不同于我们之前使用的函数调用方式! 1.函数表达式包含名称,用于递归 var f = function s(num) { if (num ...
- Office文档在线编辑的实现之二
讲述了如何通过iis的webdav支持实现客户端的office直接编辑服务器上的文件,本篇将讲解如何实现客户端的office直接编辑数据库中的二进制形式保存的office文件. 实现的关键:模拟IIS ...
- sugarcrm关于邮件设置几个不好理解的地方
陈沙克日志 把我的过程记录下来,以免以后忘了 2008-06-11 12:32 sugarcrm关于邮件设置几个不好理解的地方 最近看sugarcrm的使用,别的基本使用,没有什么问题,几天就 ...