Вы находитесь на странице: 1из 5

#3.

Python: .
1.
.

. , ,
, ,
, 5- , ,
?
.
Python fetcher,
. :
API
Python , - ,
.
Loader, PostProcessor WBDataFetcher .
:
>>> from fetcher import Loader, PostProcessor, WBDataFetcher
>>> loader = Loader()
>>> postproc = PostProcessor()
>>> datafetcher = WBDataFetcher(loader, postproc)
>>> data = datafetcher.fetch(ukr, GDP)
datafetcher fetch loader.get(ukr, NY.GDP.MKTP.CD)
postproc.process(data).
,
, .
.
,

.
CachingLoader,
(.. , API
), , ,
, .
- (..

Loader).
, Loader PostProcessor
, . ,
WBDataFetcher , Loader
CachingLoader - get(country, indicator) .
, ,
. ,
- PostProcessor,
. HTML.
,
.
:
>>> from fetcher import CachingLoader
>>> from fetcher import HTMLPostProcessor, DBPostProcessor
>>> from fetcher import ComboPostProcessor
>>> from fetcher import WBDataFetcher
>>> loader = Loader()
>>> htmlpostproc = HTMLPostProcessor()
>>> dbpostproc = DBPostProcessor(data.sqlite)
>>> postproc = ComboPostProcessor(htmlpostproc, dbpostproc)
>>> datafetcher = WBDataFetcher(loader, postproc)
>>> data = datafetcher.fetch(ukr, GDP) #data not contains HTML
fetcher (Loader , , PostProcessor- ,
),
.
. ,
:
,
( ).

.
) get(...) Loader.
urllib2 json.
, :
1) urllib2.urlopen(URL)
URL. headers .
>>> import urllib2
>>> result = urllib2.urlopen(http://example.com).read()
>>> print result
<!doctype html>
<html>
<head>
... bunch of styles and metas here
</head>
<body>
... bunch of HTML here
</body>
</html>
2) json.loads(JSON_STRING)
JSON Python.
>>> import json
>>> json_string = [{"a": 1}, {"a": 2}]
>>> py_data = json.loads(json_string)
>>> print py_data
[{u'a': 1}, {u'a': 2}]
b) - PostProcessor.
, .
: , Loader.
World Bank Data API. ..
RESTful API. , HTTP
. API ,
:

http://api.worldbank.org/countries/<country-code>/indicators/<indicator>
country-code - , indicator - ,
, , (, ).
, , 10
(, ).
- xml. JSON ,
Python.
, .. current $ ( NY.GDP.MKTP.CD)
,
:
http://api.worldbank.org/countries/ukr/indicators/NY.GDP.MKTP.CD?MRV=10&form
at=json
:
[
{"page":1,"pages":1,"per_page":"50","total":10},
[<------------- start extracting data here
{"indicator":
{"id":"NY.GDP.MKTP.CD","value":"GDP (current US$)"},
"country":{"id":"UA","value":"Ukraine"},
"value":"176308825694.203","decimal":"0","date":"2012"},
{"indicator":
{"id":"NY.GDP.MKTP.CD","value":"GDP (current US$)"},
"country":{"id":"UA","value":"Ukraine"},
"value":"163422495339.803","decimal":"0","date":"2011"},
...]]

json.loads(...)
Python (, , ..),
.

2.
. -
GAE .
http://<you-app>.appspot.com/students/<student-id>,

student-id - ,
App Engine Datastore. ,
handler . Datastore
student-id.
. Github:
student@student:~> git clone https://github.com/PhtRaveller/kharkiv-hpc.git
, (.. ,
2) :
student@student:~/some/path> git pull -u origin
.
1. , Python, 3- , -, 2009.
2. urllib2 json:
http://docs.python.org/2/library/urllib2.html
http://docs.python.org/2/library/json.html
3. World Bank Data API:
http://data.worldbank.org/developers/api-overview

Оценить