Python Tutorial - Parse JSON Objects with Python

时间:2023-03-09 06:38:36
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. :)"

当然吧上面的代码全部写道一行里面,也不是不行,是可以的。但是觉得是没有必要的。看起来太负责。之所以分开写,是为了便于理解。

下面是效果图。可以看一下

Python Tutorial - Parse JSON Objects with Python

一个比较简单的输出网站相似的功能。使用的是别的网站的API,很好用的说,是JSON版的哈哈。

这里,用来查看帮助:

Python Tutorial - Parse JSON Objects with Python

Python Tutorial - Parse JSON Objects with Python

弄得像个样子,呵呵。可以做一个简单的搜索同类网站的小程序了。还不错吧。

Hv Fun

Happy Coding