项目目标 request库和BeautifulSoup模块获取豆瓣民谣音乐数据,并用Tkinter建立图形化显示界面。 获取民谣网页 进入豆瓣音乐,选择分类浏览民谣: importrequestsheaders{UserAgent:Mozilla5。0(WindowsNT10。0;WOW64)AppleWebKit537。36(KHTML,likeGecko)Chrome56。0。2924。87Safari537。36}设置url地址urlhttps:music。douban。comtag民谣发送网络请求responserequests。get(url,headersheaders)print(response。text)BeautifulSoup解析数据BeautifulSoup是一个可以从HTML或XML文件中提取数据的Python库。其功能简单而强大、容错能力高、文档相对完善,清晰易懂。非Python标准模块,需要安装才能使用。 安装方式:pipinstallbs4 BeautifulSoup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,如果不安装第三方解析器,则Python会使用默认解析器。 BeautifulSoup提取数据的常用方法: CSS选择器: html文件:!DOCTYPEhtmlhtmllangenheadmetacharsetUTF8title黑猫编程titleheadbodyulnamelistli1lili2lili3liul黑猫编程在线测评题库spanidd1aaspanspanbbspanspanccspanbodyhtml python文件:frombs4importBeautifulSoupwithopen(test02。html,r,encodingutf8)asf:htmlf。read()print(html)print(type(html))bsBeautifulSoup(html,lxml)retbs。find(p,classbox)print(ret)retbs。findAll(p,classbox)print(ret)print(len(ret))retbs。find(ul,attrs{name:list})print(ret)retbs。select(d1)print(ret)retbs。select(。box)print(ret)retbs。titleprint(ret)retbs。p。attrsprint(ret)获取音乐详细信息创建BeautifulSoup对象soupBeautifulSoup(response。text,lxml)listResultsoup。select(tr。item,limit5)print(listResult)请在下方写你的代码定义列表resultList〔〕forresultinlistResult:resultDict{}resultDict〔musicImage〕result。select(img)〔0〕〔src〕resultDict〔musicName〕result。select(a)〔0〕〔title〕。split()〔1〕。strip()resultDict〔musicInfo〕result。select(p。pl)〔0〕。gettext()resultList。append(resultDict)print(resultList)展示音乐图片 放置Label组件使用相对于父容器的相对位置方式:relx0表示相对父容器的x坐标为0。rely0。2表示相对父容器的y坐标为20。windowtk。Tk()window。geometry(400x510)window。resizable(0,0)window。title(豆瓣民谣音乐)foriinrange(5):获取图片数据imgResponserequests。get(resultList〔i〕〔musicImage〕)生成Image对象imageImage。open(BytesIO(imgResponse。content))加载音乐图片photoImageImageTk。PhotoImage(image。resize((81,81)))显示音乐图片labelImgtk。Label(window,imagephotoImage,width81,height81)labelImg。place(relx0,relyi0。2)labelImg。imagephotoImagewindow。mainloop()完整项目 添加Frame组件:windowtk。Tk()window。geometry(400x510)window。resizable(0,0)window。title(豆瓣民谣音乐)foriinrange(5):请在下方写你的代码添加Frame组件frametk。Frame(window,width400,height100,bgwhite)frame。place(relx0,relyi0。2)获取图片数据imgResponserequests。get(resultList〔i〕〔musicImage〕)生成Image对象imageImage。open(BytesIO(imgResponse。content))加载音乐图片photoImageImageTk。PhotoImage(image。resize((81,81)))显示音乐图片labelImgtk。Label(frame,imagephotoImage,width81,height81)labelImg。place(relx0。04,rely0。05)labelImg。imagephotoImage显示音乐名称labelNametk。Label(frame,textresultList〔i〕〔musicName〕,bgwhite,fg37A,font(。fontsimhei。ttf,11))labelName。place(relx0。3,rely0。1)显示音乐信息labelInfotk。Label(frame,textresultList〔i〕〔musicInfo〕,bgwhite,fg666,font(。fontsimhei。ttf,10))labelInfo。place(relx0。3,rely0。6)window。mainloop()相关推荐什么是网络爬虫?自动采集互联网信息最人性化的Python网络爬虫requests模块,下载百度贴吧和博客标题PythonFlask登录和POST请求,云相册项目Flask模块,Python后端最简洁的服务器框架Flaskhttp协议和get请求一文详解PythonFlask模块设置Cookie和SessionPythonFlask服务器模块,模板for循环和模板过滤一文详解PythonFlask模块,自定义错误页面、日志和蓝图模块PythonFlask登录和POST请求,云相册项目Flask模块,Python后端最简洁的服务器框架Flaskhttp协议和get请求一文详解PythonFlask模块设置Cookie和SessionPythonFlask服务器模块,模板for循环和模板过滤一文详解PythonFlask模块,自定义错误页面、日志和蓝图模块视频讲解 视频加载中。。。 视频加载中。。。