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. Java发送get及post请求工具方法

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...

  2. mongodb group包(最具体的、最受欢迎、最容易理解的解释)

    和数据库一样group经常常使用于统计.MongoDB的group还有非常多限制,如:返回结果集不能超过16M, group操作不会处理超过10000个唯一键.好像还不能利用索引[不非常确定]. Gr ...

  3. location将地址栏参数拆分成键值对的对象

    window.location可获取地址栏的一系列信息,并且每个浏览器都支持该属性,非常方便.而获取到的问号后面的参数可以进行加工转变成我们所想要的键值对. location的属性: 属性名 例子 说 ...

  4. 多线程下HashMap的死循环是如何产生的

    前言 HashMap不是线程安全的,如果需要在多线程环境中使用Map,那么我们可以使用ConcurrentHashmap. 1.举例说明: package com.test; import java. ...

  5. jquery中的一点工作小记

    在做一个页面的时候,网页中由许多块相同的地方 ,例如页面中会有多个地方用到TAB 效果,在使用each()遍历以后  不一定会正常显示,这是因为写的html静态页面中只有当前板块中有这个的类,若是用J ...

  6. Windows环境搭建Web自动化测试框架Watir

    Windows环境搭建Web自动化测试框架Watir 一.前言     Web自动化测试一直是一个比较迫切的问题,对于现在web开发的敏捷开发,却没有相对应的敏捷测试,故开此主题,一边研究,一边将We ...

  7. HPQC HP Quality Center windows 服务

    HPQC HP Quality Center windows 服务已经启动的话,就不用运行run.bat 两个是一样的效果.

  8. 华丽的NHibernate

    华丽的NHibernate http://www.cnblogs.com/kissdodog/archive/2013/02/21/2919886.html 华丽的NHibernate NHibern ...

  9. Linux 下 Vi 配置文件 .vimrc 文件

    Linux 下 Vi 配置文件 .vimrc 文件 配置 vim 的方法是在用户主目录下建立个.vimrc文件,我一般使用root帐户,所以就在/root/下建立一个.vimrc文件:vi /root ...

  10. 编写Windows Service 备忘

    项目需求要做一个定时扫表,将按条件查询到的数据插入或者更新到另一个数据表的需求,老大要求让用window service来做 因为以前没有做过,把这次的经历写出来.作为备忘. 1.什么是windows ...