Skip to content

Instantly share code, notes, and snippets.

@moltak
Created January 8, 2014 00:08
Show Gist options
  • Save moltak/8309282 to your computer and use it in GitHub Desktop.
Save moltak/8309282 to your computer and use it in GitHub Desktop.
daum open api 사용
import urllib2
import json
prices = ''
pub_date = ''
categorys = ''
descs = ''
urls = ''
def retriveBookInfo(isbn):
global prices, pub_date, categorys, descs, urls
url = 'http://apis.daum.net/search/book?apikey=DAUM_SEARCH_DEMO_APIKEY&output=json&searchType=isbn&q=' + isbn
try:
data = urllib2.urlopen(url).read()
j = json.loads(data)
price = j['channel']['item'][0]['list_price']
date = j['channel']['item'][0]['pub_date']
category = j['channel']['item'][0]['category']
desc = j['channel']['item'][0]['description']
title = j['channel']['item'][0]['title']
url = j['channel']['item'][0]['link']
print title
prices += price + '\n'
pub_date += date + '\n'
categorys += category + '\n'
descs += desc + '\n'
urls += url + '\n'
except urllib2.HTTPError, e:
print "HTTP error: %d" % e.code
except urllib2.URLError, e:
print "Network error: %s" % e.reason.args[1]
def writeFile(title, data):
bookfile = open(title, 'wb')
bookfile.write(data)
bookfile.close()
#retriveBookInfo('9788936803872')
def main():
books = open('Book.json')
content = ''
for i in books.readlines():
content += i
#print i.split(':')[0].strip()
if i.split(':')[0].strip() == "\"isbn\"":
isbn = i.split(':')[1].strip().replace(',', '').replace('\"', '')
retriveBookInfo(isbn)
writeFile('prices.txt', prices)
writeFile('pub_date.txt', pub_date)
writeFile('categorys.txt', categorys.encode('utf-8'))
writeFile('descs.txt', descs.encode('utf-8'))
writeFile('urls.txt', urls.encode('utf-8'))
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment