Python大作业——爬虫+可视化+数据分析+数据库(数据库篇)

1,163次阅读
没有评论

相关链接

Python大作业——爬虫+可视化+数据分析+数据库(简介篇)

Python大作业——爬虫+可视化+数据分析+数据库(爬虫篇)

Python大作业——爬虫+可视化+数据分析+数据库(数据分析篇)

Python大作业——爬虫+可视化+数据分析+数据库(可视化篇)

由于该程序可以进行歌曲得收藏和下载,并导出用户的所收藏和下载的歌曲,所以需要数据库来进行不同用户信息的存储

一、注册登录

首先需要建立一个用户表

为了简单,登录注册只需要账号密码

所以用户表中只设置两个字段,username和password,数据类型为varchar型

以username为主键间接实现用户名不可重复(如果insert语句操作失败,则直接提示用户名已存在)

Python大作业——爬虫+可视化+数据分析+数据库(数据库篇)

建立好表后就可以进行数据库操作了

首先先对输入数据进行简单的判断,如是否输入长度过长或者没有输入

确定没有异常后再进行数据库操作

使用pymysql.connect进行数据库的连接并获取数据库交互对象cursor,用try块包裹,如果出错则抛出数据库连接失败的消息提示框并结束该函数

正确连接数据库之后,将输入框获得的username和password作为参数封装SQL语句sql = 'insert into users values("%s","%s")' % (username, password)

随后执行sql语句,如果成功执行则结束事务conn.commit()出现异常则进行事务回滚conn.rollback()并提示用户名已存在(因为username设置为主键,重复则会出现异常,至于数据长度方面的异常已经在最开始排除)

最后不要忘记关闭数据库连接

<span class="token keyword">def</span> <span class="token function">register</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> event<span class="token punctuation">)</span><span class="token punctuation">:</span>  <span class="token comment"># 注册</span>
    username <span class="token operator">=</span> self<span class="token punctuation">.</span>nameEd1<span class="token punctuation">.</span>text<span class="token punctuation">(</span><span class="token punctuation">)</span>
    password <span class="token operator">=</span> self<span class="token punctuation">.</span>nameEd2<span class="token punctuation">.</span>text<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> username <span class="token operator">==</span> <span class="token string">""</span> <span class="token keyword">or</span> password <span class="token operator">==</span> <span class="token string">""</span><span class="token punctuation">:</span>
        msg_box <span class="token operator">=</span> QMessageBox<span class="token punctuation">(</span>QMessageBox<span class="token punctuation">.</span>Warning<span class="token punctuation">,</span> <span class="token string">'温馨提示'</span><span class="token punctuation">,</span> <span class="token string">'用户名和密码不能为空'</span><span class="token punctuation">)</span>
        msg_box<span class="token punctuation">.</span>exec_<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span>

    <span class="token keyword">if</span> <span class="token builtin">len</span><span class="token punctuation">(</span>username<span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">10</span> <span class="token keyword">or</span> <span class="token builtin">len</span><span class="token punctuation">(</span>password<span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">16</span><span class="token punctuation">:</span>
        msg_box <span class="token operator">=</span> QMessageBox<span class="token punctuation">(</span>QMessageBox<span class="token punctuation">.</span>Warning<span class="token punctuation">,</span> <span class="token string">'温馨提示'</span><span class="token punctuation">,</span> <span class="token string">'用户名或密码过长'</span><span class="token punctuation">)</span>
        msg_box<span class="token punctuation">.</span>exec_<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span>
    
    <span class="token keyword">try</span><span class="token punctuation">:</span>
        conn <span class="token operator">=</span> pymysql<span class="token punctuation">.</span>connect<span class="token punctuation">(</span>host<span class="token operator">=</span><span class="token string">'localhost'</span><span class="token punctuation">,</span> user<span class="token operator">=</span><span class="token string">'root'</span><span class="token punctuation">,</span> password<span class="token operator">=</span><span class="token string">'129496'</span><span class="token punctuation">,</span> db<span class="token operator">=</span><span class="token string">'pyhomework'</span><span class="token punctuation">)</span>
        cur <span class="token operator">=</span> conn<span class="token punctuation">.</span>cursor<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">except</span><span class="token punctuation">:</span>
        msg_box <span class="token operator">=</span> QMessageBox<span class="token punctuation">(</span>QMessageBox<span class="token punctuation">.</span>Warning<span class="token punctuation">,</span> <span class="token string">'错误提示'</span><span class="token punctuation">,</span> <span class="token string">'数据库连接失败'</span><span class="token punctuation">)</span>
        msg_box<span class="token punctuation">.</span>exec_<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span>

    sql <span class="token operator">=</span> <span class="token string">'insert into users values("%s","%s")'</span> <span class="token operator">%</span> <span class="token punctuation">(</span>username<span class="token punctuation">,</span> password<span class="token punctuation">)</span>
    <span class="token keyword">try</span><span class="token punctuation">:</span>
        cur<span class="token punctuation">.</span>execute<span class="token punctuation">(</span>sql<span class="token punctuation">)</span>
        conn<span class="token punctuation">.</span>commit<span class="token punctuation">(</span><span class="token punctuation">)</span>
        msg_box <span class="token operator">=</span> QMessageBox<span class="token punctuation">(</span>QMessageBox<span class="token punctuation">.</span>Warning<span class="token punctuation">,</span> <span class="token string">'恭喜'</span><span class="token punctuation">,</span> <span class="token string">'注册成功'</span><span class="token punctuation">)</span>
        msg_box<span class="token punctuation">.</span>exec_<span class="token punctuation">(</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>nameEd1<span class="token punctuation">.</span>setText<span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>nameEd2<span class="token punctuation">.</span>setText<span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span>
    <span class="token keyword">except</span><span class="token punctuation">:</span>
        conn<span class="token punctuation">.</span>rollback<span class="token punctuation">(</span><span class="token punctuation">)</span>
        msg_box <span class="token operator">=</span> QMessageBox<span class="token punctuation">(</span>QMessageBox<span class="token punctuation">.</span>Warning<span class="token punctuation">,</span> <span class="token string">'温馨提示'</span><span class="token punctuation">,</span> <span class="token string">'用户名已经存在'</span><span class="token punctuation">)</span>
        msg_box<span class="token punctuation">.</span>exec_<span class="token punctuation">(</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>nameEd1<span class="token punctuation">.</span>setText<span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>nameEd2<span class="token punctuation">.</span>setText<span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span>
    cur<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
    conn<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>

登录功能类似,先进行简单的输入判断,无误后连接数据库并通过输入的用户名执行查询语句

使用user = cur.fetchone()语句获取查询的第一个结果集

如果user为null则证明查询不到该用户

否则将输入的密码与user[1]进行比较(密码是表中的第二个字段,所以使用下标1)

相同则证明登录成反之则证明密码错误

<span class="token keyword">def</span> <span class="token function">login</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> event<span class="token punctuation">)</span><span class="token punctuation">:</span>  <span class="token comment"># 登录</span>
    username <span class="token operator">=</span> self<span class="token punctuation">.</span>nameEd1<span class="token punctuation">.</span>text<span class="token punctuation">(</span><span class="token punctuation">)</span>
    password <span class="token operator">=</span> self<span class="token punctuation">.</span>nameEd2<span class="token punctuation">.</span>text<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> username <span class="token operator">==</span> <span class="token string">""</span> <span class="token keyword">or</span> password <span class="token operator">==</span> <span class="token string">""</span><span class="token punctuation">:</span>
        msg_box <span class="token operator">=</span> QMessageBox<span class="token punctuation">(</span>QMessageBox<span class="token punctuation">.</span>Warning<span class="token punctuation">,</span> <span class="token string">'温馨提示'</span><span class="token punctuation">,</span> <span class="token string">'请输入用户名和密码'</span><span class="token punctuation">)</span>
        msg_box<span class="token punctuation">.</span>exec_<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span>

    <span class="token keyword">try</span><span class="token punctuation">:</span>
        conn <span class="token operator">=</span> pymysql<span class="token punctuation">.</span>connect<span class="token punctuation">(</span>host<span class="token operator">=</span><span class="token string">'localhost'</span><span class="token punctuation">,</span> user<span class="token operator">=</span><span class="token string">'root'</span><span class="token punctuation">,</span> password<span class="token operator">=</span><span class="token string">'129496'</span><span class="token punctuation">,</span> db<span class="token operator">=</span><span class="token string">'pyhomework'</span><span class="token punctuation">)</span>
        cur <span class="token operator">=</span> conn<span class="token punctuation">.</span>cursor<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">except</span><span class="token punctuation">:</span>
        msg_box <span class="token operator">=</span> QMessageBox<span class="token punctuation">(</span>QMessageBox<span class="token punctuation">.</span>Warning<span class="token punctuation">,</span> <span class="token string">'温馨提示'</span><span class="token punctuation">,</span> <span class="token string">'数据库连接错误'</span><span class="token punctuation">)</span>
        msg_box<span class="token punctuation">.</span>exec_<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span>

    <span class="token keyword">try</span><span class="token punctuation">:</span>
        sql <span class="token operator">=</span> <span class="token string">'select * from users where username="%s"'</span> <span class="token operator">%</span> <span class="token punctuation">(</span>username<span class="token punctuation">)</span>
        cur<span class="token punctuation">.</span>execute<span class="token punctuation">(</span>sql<span class="token punctuation">)</span>
        conn<span class="token punctuation">.</span>commit<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">except</span><span class="token punctuation">:</span>
        msg_box <span class="token operator">=</span> QMessageBox<span class="token punctuation">(</span>QMessageBox<span class="token punctuation">.</span>Warning<span class="token punctuation">,</span> <span class="token string">'错误提示'</span><span class="token punctuation">,</span> <span class="token string">'系统错误'</span><span class="token punctuation">)</span>
        msg_box<span class="token punctuation">.</span>exec_<span class="token punctuation">(</span><span class="token punctuation">)</span>
        cur<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
        conn<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span>

    user <span class="token operator">=</span> cur<span class="token punctuation">.</span>fetchone<span class="token punctuation">(</span><span class="token punctuation">)</span>
    cur<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
    conn<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> user <span class="token keyword">is</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
        msg_box <span class="token operator">=</span> QMessageBox<span class="token punctuation">(</span>QMessageBox<span class="token punctuation">.</span>Warning<span class="token punctuation">,</span> <span class="token string">'错误提示'</span><span class="token punctuation">,</span> <span class="token string">'该用户不存在'</span><span class="token punctuation">)</span>
        msg_box<span class="token punctuation">.</span>exec_<span class="token punctuation">(</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>nameEd1<span class="token punctuation">.</span>setText<span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>nameEd2<span class="token punctuation">.</span>setText<span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span>

    <span class="token keyword">if</span> username <span class="token operator">==</span> user<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token keyword">and</span> password <span class="token operator">==</span> user<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
        msg_box <span class="token operator">=</span> QMessageBox<span class="token punctuation">(</span>QMessageBox<span class="token punctuation">.</span>Warning<span class="token punctuation">,</span> <span class="token string">'恭喜'</span><span class="token punctuation">,</span> <span class="token string">'登陆成功'</span><span class="token punctuation">)</span>
        msg_box<span class="token punctuation">.</span>exec_<span class="token punctuation">(</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>username <span class="token operator">=</span> username
        self<span class="token punctuation">.</span>accept<span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token keyword">else</span><span class="token punctuation">:</span>
        msg_box <span class="token operator">=</span> QMessageBox<span class="token punctuation">(</span>QMessageBox<span class="token punctuation">.</span>Warning<span class="token punctuation">,</span> <span class="token string">'错误提示'</span><span class="token punctuation">,</span> <span class="token string">'用户名或者密码错误'</span><span class="token punctuation">)</span>
        msg_box<span class="token punctuation">.</span>exec_<span class="token punctuation">(</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>nameEd1<span class="token punctuation">.</span>setText<span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>nameEd2<span class="token punctuation">.</span>setText<span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span>

二、收藏与下载

首先需要建立对应的收藏表和下载表

其中song_name字段可以设置为主键,这样的话就可以避免重复地收藏数据,当然如此做的话还需要进行额外的判断,具体操作同上(即在执行语句出现异常时提示该歌曲已保存)
Python大作业——爬虫+可视化+数据分析+数据库(数据库篇)

下载表也以完全相同的方式建立

在用户登录进入主界面的时候获取当前登录的用户名

随后用户进行收藏和下载操作时,则将这个用户名,以及收藏或下载的歌曲名字,歌手名字,歌曲资源地址插入对应的表中

用户查询收藏歌曲和下载歌曲时,则通过用户名到对应表中查询数据

此处只演示收藏操作和查询收藏操作

其中查询收藏会在访问完数据之后使用xlsxwriter将数据写入excel表格中

<span class="token keyword">def</span> <span class="token function">my_collects</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">try</span><span class="token punctuation">:</span>
        conn <span class="token operator">=</span> pymysql<span class="token punctuation">.</span>connect<span class="token punctuation">(</span>host<span class="token operator">=</span><span class="token string">'localhost'</span><span class="token punctuation">,</span> user<span class="token operator">=</span><span class="token string">'root'</span><span class="token punctuation">,</span> password<span class="token operator">=</span><span class="token string">'129496'</span><span class="token punctuation">,</span> db<span class="token operator">=</span><span class="token string">'pyhomework'</span><span class="token punctuation">)</span>
        cur <span class="token operator">=</span> conn<span class="token punctuation">.</span>cursor<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">except</span><span class="token punctuation">:</span>
        msg_box <span class="token operator">=</span> QMessageBox<span class="token punctuation">(</span>QMessageBox<span class="token punctuation">.</span>Warning<span class="token punctuation">,</span> <span class="token string">'温馨提示'</span><span class="token punctuation">,</span> <span class="token string">'数据库连接错误'</span><span class="token punctuation">)</span>
        msg_box<span class="token punctuation">.</span>exec_<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span>

    <span class="token keyword">try</span><span class="token punctuation">:</span>
        sql <span class="token operator">=</span> <span class="token string">'select * from collects where user="%s"'</span> <span class="token operator">%</span> <span class="token punctuation">(</span>self<span class="token punctuation">.</span>username<span class="token punctuation">)</span>
        cur<span class="token punctuation">.</span>execute<span class="token punctuation">(</span>sql<span class="token punctuation">)</span>
        conn<span class="token punctuation">.</span>commit<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">except</span><span class="token punctuation">:</span>
        msg_box <span class="token operator">=</span> QMessageBox<span class="token punctuation">(</span>QMessageBox<span class="token punctuation">.</span>Warning<span class="token punctuation">,</span> <span class="token string">'错误提示'</span><span class="token punctuation">,</span> <span class="token string">'系统错误'</span><span class="token punctuation">)</span>
        msg_box<span class="token punctuation">.</span>exec_<span class="token punctuation">(</span><span class="token punctuation">)</span>

    row <span class="token operator">=</span> <span class="token number">0</span>
    collects <span class="token operator">=</span> cur<span class="token punctuation">.</span>fetchall<span class="token punctuation">(</span><span class="token punctuation">)</span>
    cur<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
    conn<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
    workbook <span class="token operator">=</span> xlsxwriter<span class="token punctuation">.</span>Workbook<span class="token punctuation">(</span>self<span class="token punctuation">.</span>username<span class="token operator">+</span><span class="token string">'\'s collects.xlsx'</span><span class="token punctuation">)</span>
    worksheet <span class="token operator">=</span> workbook<span class="token punctuation">.</span>add_worksheet<span class="token punctuation">(</span><span class="token string">'music'</span><span class="token punctuation">)</span>
    worksheet<span class="token punctuation">.</span>write_row<span class="token punctuation">(</span>row<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'歌手'</span><span class="token punctuation">,</span> <span class="token string">'歌名'</span><span class="token punctuation">,</span> <span class="token string">'歌曲资源地址'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token keyword">for</span> collect <span class="token keyword">in</span> collects<span class="token punctuation">:</span>
        row <span class="token operator">+=</span> <span class="token number">1</span>
        worksheet<span class="token punctuation">.</span>write_row<span class="token punctuation">(</span>row<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>collect<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> collect<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> collect<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    workbook<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
    os<span class="token punctuation">.</span>startfile<span class="token punctuation">(</span>self<span class="token punctuation">.</span>username<span class="token operator">+</span><span class="token string">'\'s collects.xlsx'</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">collect</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">try</span><span class="token punctuation">:</span>
        conn <span class="token operator">=</span> pymysql<span class="token punctuation">.</span>connect<span class="token punctuation">(</span>host<span class="token operator">=</span><span class="token string">'localhost'</span><span class="token punctuation">,</span> user<span class="token operator">=</span><span class="token string">'root'</span><span class="token punctuation">,</span> password<span class="token operator">=</span><span class="token string">'129496'</span><span class="token punctuation">,</span> db<span class="token operator">=</span><span class="token string">'pyhomework'</span><span class="token punctuation">)</span>
        cur <span class="token operator">=</span> conn<span class="token punctuation">.</span>cursor<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">except</span><span class="token punctuation">:</span>
        msg_box <span class="token operator">=</span> QMessageBox<span class="token punctuation">(</span>QMessageBox<span class="token punctuation">.</span>Warning<span class="token punctuation">,</span> <span class="token string">'温馨提示'</span><span class="token punctuation">,</span> <span class="token string">'数据库连接错误'</span><span class="token punctuation">)</span>
        msg_box<span class="token punctuation">.</span>exec_<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span>
    
    sql <span class="token operator">=</span> <span class="token string">'insert into collects values("%s","%s", "%s", "%s")'</span> <span class="token operator">%</span> <span class="token punctuation">(</span>self<span class="token punctuation">.</span>username<span class="token punctuation">,</span> self<span class="token punctuation">.</span>keyword<span class="token punctuation">,</span> self<span class="token punctuation">.</span>names<span class="token punctuation">[</span>self<span class="token punctuation">.</span>play_index_now<span class="token punctuation">]</span><span class="token punctuation">,</span> self<span class="token punctuation">.</span>musics<span class="token punctuation">[</span>self<span class="token punctuation">.</span>play_index_now<span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token keyword">try</span><span class="token punctuation">:</span>
        cur<span class="token punctuation">.</span>execute<span class="token punctuation">(</span>sql<span class="token punctuation">)</span>
        conn<span class="token punctuation">.</span>commit<span class="token punctuation">(</span><span class="token punctuation">)</span>
        msg_box 
        <span class="token operator">=</span> QMessageBox<span class="token punctuation">(</span>QMessageBox<span class="token punctuation">.</span>Warning<span class="token punctuation">,</span> <span class="token string">'提示'</span><span class="token punctuation">,</span> <span class="token string">'收藏成功'</span><span class="token punctuation">)</span>
        msg_box<span class="token punctuation">.</span>exec_<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">except</span><span class="token punctuation">:</span>
        conn<span class="token punctuation">.</span>rollback<span class="token punctuation">(</span><span class="token punctuation">)</span>
        msg_box <span class="token operator">=</span> QMessageBox<span class="token punctuation">(</span>QMessageBox<span class="token punctuation">.</span>Warning<span class="token punctuation">,</span> <span class="token string">'温馨提示'</span><span class="token punctuation">,</span> <span class="token string">'收藏出错'</span><span class="token punctuation">)</span>
        msg_box<span class="token punctuation">.</span>exec_<span class="token punctuation">(</span><span class="token punctuation">)</span>
    cur<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
    conn<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>

执行下载操作时,使用如下语句唤出文件保存窗口

其中第二个参数为窗口名称,第三个参数为窗口打开时的默认路径(.标识当前项目所在路径),第四个参数表示保存格式

filename <span class="token operator">=</span> QFileDialog<span class="token punctuation">.</span>getSaveFileName<span class="token punctuation">(</span>self<span class="token punctuation">,</span> <span class="token string">'下载文件'</span><span class="token punctuation">,</span> <span class="token string">'.'</span><span class="token punctuation">,</span> <span class="token string">'音乐文件(*.MP3)'</span><span class="token punctuation">)</span>  <span class="token comment"># 保存歌曲</span>

该语句执行后返回保存的文件名,如果不为空则证明成功保存而不是点击取消

随后即可将内容写入该文件,完成下载到本地的操作

<span class="token keyword">if</span> filename<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">!=</span> <span class="token string">''</span><span class="token punctuation">:</span>
    <span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span>filename<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">'wb'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> m<span class="token punctuation">:</span>
        m<span class="token punctuation">.</span>write<span class="token punctuation">(</span>response<span class="token punctuation">.</span>content<span class="token punctuation">)</span>

总的数据表展示

Python大作业——爬虫+可视化+数据分析+数据库(数据库篇)

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:Python教程2022-10-20发表,共计5241字。
新手QQ群:570568346,欢迎进群讨论 Python51学习