豆瓣读书v2API访问小程序python.docx
《豆瓣读书v2API访问小程序python.docx》由会员分享,可在线阅读,更多相关《豆瓣读书v2API访问小程序python.docx(13页珍藏版)》请在冰豆网上搜索。
![豆瓣读书v2API访问小程序python.docx](https://file1.bdocx.com/fileroot1/2023-2/1/ade7f597-0574-4bf1-aabe-3df94cbd729f/ade7f597-0574-4bf1-aabe-3df94cbd729f1.gif)
豆瓣读书v2API访问小程序python
最初写这个小程序是因为有一门课要求我们写一个豆瓣api访问的小应用程序,要求如下:
1.访问豆瓣网站开发者页面,了解豆瓣V2API的内容
2.学习JSON格式,了解JSON格式的标准;利用你熟悉的语言(Python/Java等),学习解析JSON格式的数据
3.学习HTTP协议,了解HTTP协议的标准;利用你熟悉的语言(Python/Java等),学习如何完成一个HTTP请求
4.学习WxPython开发工具,编写GUI程序;或其它你熟悉的语言
5.了解Git源代码管理工具;学习使用GitHub网站提供的代码托管服务管理你的源代码
运行效果如图:
开发环境:
WxPython+python2.7.3
以下附上python版本的源码:
App1.py
#!
/usr/bin/envpython
#Boa:
App:
BoaApp
importwx
importFrame1
modules={'Frame1':
[1,'MainframeofApplication',u'Frame1.py']}
classBoaApp(wx.App):
defOnInit(self):
self.main=Frame1.create(None)
self.main.Show()
self.SetTopWindow(self.main)
returnTrue
defmain():
application=BoaApp(0)
application.MainLoop()
if__name__=='__main__':
main()
Frame1.py
#Boa:
Frame:
Frame1
importwx
importurllib2,json
importwx.richtext
defConnectionGetDataApi(url):
#returndatafromapi
res=urllib2.urlopen(url)
#printtype(res)
result=res.read()
#printtype(result)
printresult
json_result=json.loads(result)
#printtype(json_result)
returnjson_result
defConnectionGetImage(image_url,image_id):
#Dpwnloadimagesfrominternet
image_addr=image_url#theaddressofanimage
#printimage_url
res_image=urllib2.urlopen(image_addr)
result_image=res_image.read()
f=file(image_id+'.jpg','wb')
f.write(result_image)
f.close()
returnNone
defSetValue(bookInfo,self):
#setvalue
image_id=bookInfo["id"]
image_addr=bookInfo["image"]
book_id=bookInfo["id"]
book_title=bookInfo["title"]
book_author=bookInfo["author"]
book_price=bookInfo["price"]
book_pages=bookInfo["pages"]
book_publisher=bookInfo["publisher"]
book_summary=bookInfo["summary"]
book_isbn10=""
book_isbn13=""
book_isbn=""
try:
book_isbn10=bookInfo["isbn10"]
except:
print"isbn10except"
try:
book_isbn13=bookInfo["isbn13"]
except:
print"isbn13except"
if(book_isbn10==""):
book_isbn=book_isbn13
elif(book_isbn13==""):
book_isbn=book_isbn10
else:
book_isbn=book_isbn10+","+book_isbn13#getlastisbn
self.textCtrl1.SetValue(book_id)#setbookid
self.textCtrl2.SetValue(book_title)#setbooktitle
self.textCtrl3.SetValue(book_isbn)#setbookisbn
self.textCtrl4.SetEditable(True)
self.textCtrl4.SetValue(book_title)
self.textCtrl4.SetEditable(False)
self.textCtrl6.SetEditable(True)
self.textCtrl6.SetValue(book_isbn)
self.textCtrl6.SetEditable(False)
self.textCtrl7.SetEditable(True)
self.textCtrl7.SetValue(book_price)
self.textCtrl7.SetEditable(False)
self.textCtrl8.SetEditable(True)
self.textCtrl8.SetValue(book_pages)
self.textCtrl8.SetEditable(False)
self.textCtrl9.SetEditable(True)
self.textCtrl9.SetValue(book_publisher)
self.textCtrl9.SetEditable(False)
self.richTextCtrl1.SetEditable(True)
self.richTextCtrl1.SetValue(book_summary)
self.richTextCtrl1.SetEditable(False)
length=len(book_author)#getauthor
#printlength
if(length==0):
author=""
else:
author=book_author[len(book_author)-length]
whilelength>=1:
if(length==1):
length=length-1
else:
author=author+","+book_author[len(book_author)-length+1]
length=length-1
self.textCtrl5.SetEditable(True)
self.textCtrl5.SetValue(author)#setauthor
self.textCtrl5.SetEditable(False)
try:
ConnectionGetImage(image_addr,image_id)#getimage
except:
print"failedtodownloadimage!
!
"
if(image_addr=="
jpg=wx.NullBitmap
else:
jpg=wx.Image(image_id+'.jpg',wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
self.staticBitmap1.SetBitmap(wx.NullBitmap)
self.staticBitmap1.SetBitmap(jpg)#setimage
self.staticText8.SetLabel(""+str(self.value_current+1)+"/"+str(self.value_end+1))
defcreate(parent):
returnFrame1(parent)
[wxID_FRAME1,wxID_FRAME1BUTTON1,wxID_FRAME1BUTTON2,wxID_FRAME1BUTTON3,
wxID_FRAME1BUTTON4,wxID_FRAME1BUTTON5,wxID_FRAME1PANEL1,
wxID_FRAME1RICHTEXTCTRL1,wxID_FRAME1STATICBITMAP1,wxID_FRAME1STATICTEXT1,
wxID_FRAME1STATICTEXT2,wxID_FRAME1STATICTEXT3,wxID_FRAME1STATICTEXT4,
wxID_FRAME1STATICTEXT5,wxID_FRAME1STATICTEXT6,wxID_FRAME1STATICTEXT7,
wxID_FRAME1STATICTEXT8,wxID_FRAME1STATICTEXT9,wxID_FRAME1TEXTCTRL1,
wxID_FRAME1TEXTCTRL2,wxID_FRAME1TEXTCTRL3,wxID_FRAME1TEXTCTRL4,
wxID_FRAME1TEXTCTRL5,wxID_FRAME1TEXTCTRL6,wxID_FRAME1TEXTCTRL7,
wxID_FRAME1TEXTCTRL8,wxID_FRAME1TEXTCTRL9,
]=[wx.NewId()for_init_ctrlsinrange(27)]
classFrame1(wx.Frame):
def_init_ctrls(self,prnt):
#generatedmethod,don'tedit
wx.Frame.__init__(self,id=wxID_FRAME1,name='',parent=prnt,
pos=wx.Point(439,121),size=wx.Size(416,538),
style=wx.DEFAULT_FRAME_STYLE,title=u'\u8c46\u74e3\u67e5\u8be2')
self.SetClientSize(wx.Size(400,500))
self.SetMaxSize(wx.Size(416,538))
self.SetMinSize(wx.Size(416,538))
self.panel1=wx.Panel(id=wxID_FRAME1PANEL1,name='panel1',parent=self,
pos=wx.Point(0,0),size=wx.Size(400,500),
style=wx.TAB_TRAVERSAL)
self.textCtrl1=wx.TextCtrl(id=wxID_FRAME1TEXTCTRL1,name='textCtrl1',
parent=self.panel1,pos=wx.Point(16,32),size=wx.Size(100,22),
style=0,value=u'1220562')
self.textCtrl2=wx.TextCtrl(id=wxID_FRAME1TEXTCTRL2,name='textCtrl2',
parent=self.panel1,pos=wx.Point(144,32),size=wx.Size(100,22),
style=0,value=u'\u6ee1\u6708\u4e4b\u591c\u767d\u9cb8\u73b0')
self.textCtrl3=wx.TextCtrl(id=wxID_FRAME1TEXTCTRL3,name='textCtrl3',
parent=self.panel1,pos=wx.Point(280,32),size=wx.Size(100,22),
style=0,value=u'9787543632608')
self.button1=wx.Button(id=wxID_FRAME1BUTTON1,
label=u'\u56fe\u4e66ID\u67e5\u8be2',name='button1',
parent=self.panel1,pos=wx.Point(32,64),size=wx.Size(80,24),
style=0)
self.button1.Bind(wx.EVT_BUTTON,self.OnButton1Button,
id=wxID_FRAME1BUTTON1)
self.button2=wx.Button(id=wxID_FRAME1BUTTON2,
label=u'\u5173\u952e\u5b57\u540d\u67e5\u8be2',name='button2',
parent=self.panel1,pos=wx.Point(152,64),size=wx.Size(80,24),
style=0)
self.button2.Bind(wx.EVT_BUTTON,self.OnButton2Button,
id=wxID_FRAME1BUTTON2)
self.button3=wx.Button(id=wxID_FRAME1BUTTON3,
label=u'\u56fe\u4e66ISBN\u67e5\u8be2',name='button3',
parent=self.panel1,pos=wx.Point(288,64),size=wx.Size(80,24),
style=0)
self.button3.Bind(wx.EVT_BUTTON,self.OnButton3Button,
id=wxID_FRAME1BUTTON3)
self.staticText1=wx.StaticText(id=wxID_FRAME1STATICTEXT1,
label=u'\u4e66\u540d',name='staticText1',parent=self.panel1,
pos=wx.Point(176,112),size=wx.Size(28,14),style=0)
self.staticText2=wx.StaticText(id=wxID_FRAME1STATICTEXT2,
label=u'\u4f5c\u8005',name='staticText2',parent=self.panel1,
pos=wx.Point(176,144),size=wx.Size(28,14),style=0)
self.staticText3=wx.StaticText(id=wxID_FRAME1STATICTEXT3,
label=u'ISBN',name='staticText3',parent=self.panel1,
pos=wx.Point(176,176),size=wx.Size(26,14),style=0)
self.staticText4=wx.StaticText(id=wxID_FRAME1STATICTEXT4,
label=u'\u4ef7\u683c',name='staticText4',parent=self.panel1,
pos=wx.Point(176,208),size=wx.Size(28,14),style=0)
self.staticText5=wx.StaticText(id=wxID_FRAME1STATICTEXT5,
label=u'\u4e66\u9875',name='staticText5',parent=self.panel1,
pos=wx.Point(176,240),size=wx.Size(28,14),style=0)
self.staticText6=wx.StaticText(id=wxID_FRAME1STATICTEXT6,
label=u'\u51fa\u7248\u793e',name='staticText6',
parent=self.panel1,pos=wx.Point(176,272),size=wx.Size(36,14),
style=0)
self.richTextCtrl1=wx.richtext.RichTextCtrl(id=wxID_FRAME1RICHTEXTCTRL1,
parent=self.panel1,pos=wx.Point(0,336),size=wx.Size(400,160),
style=wx.richtext.RE_MULTILINE,
value=u'\u90a3\u4e00\u5e74\uff0c\u662f\u542c\u83ab\u624e\u7279\u3001\u9493\u9c88\u9c7c\u548c\u5bb6\u5ead\u7834\u88c2\u7684\u4e00\u5e74\u3002\u8bf4\u5230\u5bb6\u5ead\u7834\u88c2\uff0c\u6bcd\u4eb2\u602a\u81ea\u5df1\u5f53\u521d\u6ca1\u6709\u627e\u5230\u597d\u7537\u4eba\uff0c\u7236\u4eb2\u5219\u8ba4\u4e3a\u5f53\u65f6\u662f\u88ab\u72d0\u72f8\u7cbe\u8ff7\u4f4f\u4e86\u773c\uff0c\u5931\u5e38\u7684\u662f\u6bcd\u4eb2\uff0c\u4f46\u51fa\u95ee\u9898\u7684\u662f\u7236\u4eb2\u2026\u2026\u3002')
self.richTextCtrl1.SetLabel(u'richText')
self.richTextCtrl1.SetEditable(False)
self.textCtrl4=wx.TextCtrl(id=wxID_FRAME1TEXTCTRL4,name='textCtrl4',
parent=self.panel1,pos=wx.Point(216,112),size=wx.Size(168,22),
style=0,value=u'\u6ee1\u6708\u4e4b\u591c\u767d\u9cb8\u73b0')
self.textCtrl4.SetEditable(False)
self.textCtrl5=wx.TextCtrl(id=wxID_FRAME1TEXTCTRL5,name='textCtrl5',
parent=self.panel1,pos=wx.Point(216,144),size=wx.Size(168,22),
style=0,value=u'[\u65e5]\u7247\u5c71\u606d\u4e00')
self.textCtrl5.SetEditable(False)
self.textCtrl6=wx.TextCtrl(id=wxID_FRAME1TEXTCTRL6,name='textCtrl6',
parent=self.panel1,pos=wx.Point(216,176),size=wx.Size(168,22),
style=0,value=u'9787543632608')
self.textCtrl6.SetEditable(False)
self.textCtrl7=wx.TextCtrl(id=wxID_FRAME1TEXTCTRL7,name='textCtrl7',
parent=self.panel1,pos=wx.Point(216,208),size=wx.Size(168,22),
style=0,value=u'15.00\u5143')
self.textCtrl7.SetEditable(False)
self.textCtrl8=wx.TextCtrl(id=wxID_FRAME1TEXTCTRL8,name='textCtrl8',
parent=self.panel1,pos=wx.Point(216,240),size=wx.Size(168,22),
style=0,value=u'180')
self.textCtrl8.SetEditable(False)
self.textCtrl9=wx.TextCtrl(id=wxID_FRAME1TEXTCTRL9,name='textCtrl9',
parent=self.panel1,pos=wx.Point(216,272),size=wx.Size(168,22),
style=0,value=u'\u9752\u5c9b\u51fa\u7248\u793e')
self.textCtrl9.SetEditable(False)
self.staticText7=wx.StaticText(id=wxID_FRAME1STATICTEXT7,
label=u'\u56fe\u4e66\u7b80\u4ecb',name='staticText7',
parent=self.panel1,pos=wx.Point(8,312),size=wx.Size(72,14),
style=0)
self.staticBitmap1=wx.StaticBitmap(bitmap=wx.NullBitmap,
id=wxID_FRAME1STATICBITMAP1,name='staticBitmap1',
parent=self.panel1,pos=wx.Point(8,104),size=wx.Size(144,184),
style=0)
self.staticBitmap1.SetMaxSize(wx.Size(144,184))
se