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的更多相关文章

  1. Python Tutorial 学习(二)--Using the Python Interpreter

    Using the Python Interpreter 2.1. Invoking the Interpreter The Python interpreter is usually install ...

  2. Python基础之JSON

    作用 对Python对象进行序列化,便于存储和传输 Python对象与JSON字符串相互转换 Python对象转JSON字符串 import json data = [ { 'a' : 1, 'b' ...

  3. [译]The Python Tutorial#7. Input and Output

    [译]The Python Tutorial#Input and Output Python中有多种展示程序输出的方式:数据可以以人类可读的方式打印出来,也可以输出到文件中以后使用.本章节将会详细讨论 ...

  4. Python Tutorial 学习(八)--Errors and Exceptions

    Python Tutorial 学习(八)--Errors and Exceptions恢复 Errors and Exceptions 错误与异常 此前,我们还没有开始着眼于错误信息.不过如果你是一 ...

  5. Python Tutorial 学习(七)--Input and Output

    7. Input and Output Python里面有多种方式展示程序的输出.或是用便于人阅读的方式打印出来,或是存储到文件中以便将来使用.... 本章将对这些方法予以讨论. 两种将其他类型的值转 ...

  6. 【转】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 ...

  7. [Python 3.x 官方文档翻译]The Python Tutorial Python教程

    Python is an easy to learn, powerful programming language. It has efficient high-level data structur ...

  8. 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 ...

  9. [译]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模块为与操作系统 ...

随机推荐

  1. 基于 自己定义注解 和 aop 实现使用memcache 对数据库的缓存 演示样例

    好久没更新blog了,在新公司打拼了两个月,每天都从早忙到晚,学到了非常多东西,可是没有时间来更新blog了.... 以下開始解说这次的主题 公司老大让我研究 ocs 就是阿里云的 开放缓存服务 点击 ...

  2. 最流行的Node.js应用开发框架简介

    最流行的Node.js应用开发框架简介 快速开发而又容易扩展,高性能且鲁棒性强.Node.js的出现让所有网络应用开发者的这些梦想成为现实.但是,有如其他新的开发语言技术一样,从头开始使用Node.j ...

  3. 探究Java中Map类

    Map以按键/数值对的形式存储数据,和数组非常相似,在数组中存在的索引,它们本身也是对象.       Map的接口       Map---实现Map       Map.Entry--Map的内部 ...

  4. Android的5样的调试信息

    Android的5样的调试信息 华清2014-10-23   北京海淀区  张俊浩 verbose:只是滤全部的信息. 啰嗦的意思. debug:debug调试的意思. info:一般提示的信息inf ...

  5. 单元测试之Qunit

    单元测试之Qunit 前言 因为公司开发了一套javascript SDK需要测试,在网上找了很久,找到了JQuery团队开发的QUnit,和基于JUnit的JsUnit,还有一些还没有看,先讲讲QU ...

  6. SQL点滴13—收集SQLServer线程等待信息

    原文:SQL点滴13-收集SQLServer线程等待信息 要知道线程等待时间是制约SQL Server效率的重要原因,这一个随笔中将学习怎样收集SQL Server中的线程等待时间,类型等信息,这些信 ...

  7. VS2015安装

    VS2015安装 Secondary Installer Setup Failed求解决方案 看到微软最近的一系列变化,着实让我等兴奋不已.VS2015下载地址就不说了.先来记录一下微软的几个变化吧. ...

  8. 前台框架的选择 EasyUI、DWZ、ligerui

    EasyUI1.3.1+MVC4.0+EF5.0 番外篇 关于前台框架的选择 EasyUI.DWZ.ligerui 昨天发了EasyUI1.3.1+MVC4.0+EF5.0实战之一 开篇及布局控件介绍 ...

  9. .net中,控件(Name)属性或ID属性的常见命名规则

    控件名称 缩写 介绍 公共控件   Button btn 按钮 CheckBox chk 复选框 CheckedListBox ckl 显示一个项列表,其中每一项左侧都有一个复选框 ComboBox ...

  10. 如何使用SetTimer MFC 不够具体

    转会:http://blog.csdn.net/ellor/article/details/1714741 Timer事件,即定时器事件,是在游戏编程中.常常使用的一个事件.借助它能够产生定时运行动作 ...