def regFromDouban(request): '''利用豆瓣API获取书籍信息加入数据库''' try: DOUBAN = "https://api.douban.com/v2/book/search?q=" q = request.REQUEST.get('q', '') q = urllib.quote(q.decode('utf8').encode('utf8')) DOUBAN += q req = urllib.urlopen(DOUBAN) resp = req.read() result = simplejson.loads(resp) books = result['books'] for i in range(len(books)): author = Author() author.name = books[i]['author'][0] author.desc = books[i]['author_intro'] author.save() try: book = Book.objects.get(isbn=books[i]['isbn13']) except Book.DoesNotExist: book = Book() book.name = books[i]['title'] book.author = author book.price = float(''.join([ item for item in books[i]['price'] if item in '1234567890.' ])) if not books[i]['isbn13']: book.isbn = books[i]['isbn10'] else: book.isbn = books[i]['isbn13'] book.press = books[i]['publisher'] book.desc = books[i]['summary'] book.binding = books[i]['binding'] book.pages = books[i]['pages'] book.spic = _downloadImg(books[i]['images']['small'], 'small') book.mpic = _downloadImg(books[i]['images']['medium'], 'medium') book.lpic = _downloadImg(books[i]['images']['large'], 'large') book.stock = 140 book.publish_date = books[i]['pubdate'] book.category = _getBookCate('letter') book.save() return HttpResponse('success') except Exception: return HttpResponse('error')