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. windows下exfat无法写入修复

    为了可以实现mac与windows文件共享,把移动硬盘格式化为exfat了,但是在osx中放入文件后,在windows上紧进行读取写入时出现错误,提示使用chkdsk进行修正,以下是修正步骤. ①wi ...

  2. 动态创建一些常的html标签

    原文:动态创建一些常的html标签 一段时间来,不管是在学习还是应用asp.net mvc应用程序,较多情况之下,需要动态创建一些html标签.如这篇<文本框下面有两个铵钮,点就加点减就减> ...

  3. WCF常见问题(1) -- WebService/WCF Session Cookie

    原文:WCF常见问题(1) -- WebService/WCF Session Cookie 在.net 3.0推出WCF之前使用的WebService,有的应用有使用Session保持一些信息,在不 ...

  4. C语言学习-数据结构 - 倒插法顺序表

    // test20161106.cpp : Defines the entry point for the console application. // #include "stdafx. ...

  5. selenium之多线程启动grid分布式测试框架封装(一)

    一.设计思路 在国内市场上,IE内核的浏览器占据了绝大部分的市场份额,那么此次框架封装将进行IE系列的浏览器进行多线程并发执行分布式测试的封装. 运行时主进程与多线程关系如下:

  6. 非常多人不愿意承认汉澳sinox已经超过windows

    汉澳sinox採用的zfs和jail打造高可靠性存储server和矩阵计算机,这不是windows和linux能相提并论的. 只是非常多人立即出来出来反驳说,windows驱动程序多(就是支持硬件多) ...

  7. OpenGL+VS2013+WIN7(64)组态

    1.下载windows在下面glut安装文件:http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip 2. 拆开发现五个文件 ...

  8. Java 之Integer相等比较

    1.问题提出 今天在和同事讨论问题的时候,无意间谈到了Integer对象的比较,先看下代码: package test; public class IntegerEqual { /** * @para ...

  9. javascript模拟title提示效果

    本代码可以实现如下情况: 1.js实现title是为了解决自定义样式和格式! 2.希望传入的格式可以不限制是否包含html结构! 3.可以自定义显示层的位置! js如下: function GetAb ...

  10. 对Extjs中store的多种操作

    Store.getCount()返回的是store中的所有数据记录,然后使用for循环遍历整个store,从而得到每条记录. 除了使用getCount()的方法外,还可以使用each()函数,如下面的 ...