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

623次阅读
没有评论

相关链接

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

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

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

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

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

以上是这次大作业的大致内容,可以看到其中需要爬虫实现的主要有三个方面:

  1. 根据搜索内容爬取搜索结果的信息(包括歌词)
  2. 爬取热门歌曲
  3. 爬取热门歌手及其歌曲

一、根据搜索内容爬取搜索结果的信息

获得具体歌曲网址

这个部分我们爬取的网站地址为:https://www.8lrc.com/search

尝试在这个页面进行搜索后我们很容易发现**,搜素框输入的结果直接作为get请求的参数,键为‘key’**

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

那么就很简单了,我们通过在界面搜索框的回车事件绑定爬虫函数,将搜索框中的内容作为参数传给函数,之后将参数拼接到https://www.8lrc.com/search后面并发送get请求即可得到响应体

随后使用BeautifulSoup中的html解析器对响应体文本进行解析

接下来通过f12查看页面元素

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

我们会发现,查询到的每首歌曲的跳转信息都在一个类名为”tGequ“的a标签里

那么我们就可以使用findAll函数获得所有歌曲的a标签

<span class="token keyword">def</span> <span class="token function">search</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> keyword<span class="token punctuation">)</span><span class="token punctuation">:</span>
    self<span class="token punctuation">.</span>play_index_now <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span>  <span class="token comment"># 每次重新搜索都将当前播放序号设置为-1</span>
    urlbase <span class="token operator">=</span> <span class="token string">r'https://www.8lrc.com/search'</span>  <span class="token comment"># 搜索的基础地址</span>
    params <span class="token operator">=</span> <span class="token punctuation">{<!-- --></span><span class="token string">'key'</span><span class="token punctuation">:</span> keyword<span class="token punctuation">}</span>  <span class="token comment"># 封装搜索的参数</span>
    res_body <span class="token operator">=</span> requests<span class="token punctuation">.</span>get<span class="token punctuation">(</span>urlbase<span class="token punctuation">,</span> params<span class="token punctuation">)</span>  <span class="token comment"># 拼接url,发送请求</span>
    soup_body <span class="token operator">=</span> BeautifulSoup<span class="token punctuation">(</span>res_body<span class="token punctuation">.</span>text<span class="token punctuation">,</span> <span class="token string">'html.parser'</span><span class="token punctuation">)</span>  <span class="token comment"># html解析获得响应文本</span>
    self<span class="token punctuation">.</span>tags <span class="token operator">=</span> soup_body<span class="token punctuation">.</span>findAll<span class="token punctuation">(</span>class_<span class="token operator">=</span><span class="token string">'tGequ'</span><span class="token punctuation">)</span>  <span class="token comment"># 得到查询结果</span>

通过tag[‘href’]即可得到其中的href属性值

由于这是一个相对地址,只要在前面加上baseurl:https://www.8lrc.com即可得到对应歌曲的链接

获取歌曲音频资源及歌词

接着我们访问具体歌曲如https://www.8lrc.com/geci/1130167.htm,希望获得其歌曲的资源以及歌词

同样打开开发者工具,可以发现在第四个script标签中的setPlayer函数里有一个url地址,且其以.mp3为后缀,结合这是一个播放器,我们不难想到这就是歌曲的音频资源,而下面的显然就是我们所需要的歌曲的歌词

知道了这些以后,就可以开始爬虫获取了

首先我们通过soup_body.select("body script")[3].get_text()语句获得这个script标签的文本

通过观察,url地址都是以”url“:"为起始,以引号"作为结束,所以我们不难写出匹配url地址的正则表达式r'"url":"([^"]*)"'

括号中的内容即我们希望获得的mp3资源

通过re.search(pattern1, script).group(1)语句即可获得括号中的内容

由于获得的地址中会有转义字符/,所以我们需要replace(r'\/', '/')将其置换为/

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

<span class="token keyword">def</span> <span class="token function">get_song_detail</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> keyword<span class="token punctuation">)</span><span class="token punctuation">:</span>  <span class="token comment"># 呈现搜索结果于界面</span>
    result <span class="token operator">=</span> <span class="token string">r'https://www.8lrc.com'</span>  <span class="token comment"># 搜索结果的基础地址</span>
    pattern <span class="token operator">=</span> <span class="token string">r'(.*) - (.*)'</span>  <span class="token comment"># 得到歌名的正则表达式</span>
    pattern1 <span class="token operator">=</span> <span class="token string">r'"url":"([^"]*)"'</span>  <span class="token comment"># 得到歌曲资源的正则表达式</span>
    self<span class="token punctuation">.</span>keyword <span class="token operator">=</span> keyword
    self<span class="token punctuation">.</span>names <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    self<span class="token punctuation">.</span>musics <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    self<span class="token punctuation">.</span>lyric <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    self<span class="token punctuation">.</span>num <span class="token operator">=</span> <span class="token number">0</span>
    <span class="token keyword">for</span> tag <span class="token keyword">in</span> self<span class="token punctuation">.</span>tags<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
        <span class="token keyword">if</span> re<span class="token punctuation">.</span>match<span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> tag<span class="token punctuation">.</span>text<span class="token punctuation">)</span><span class="token punctuation">:</span>
            res_body <span class="token operator">=</span> requests<span class="token punctuation">.</span>get<span class="token punctuation">(</span>result <span class="token operator">+</span> tag<span class="token punctuation">[</span><span class="token string">'href'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
            soup_body <span class="token operator">=</span> BeautifulSoup<span class="token punctuation">(</span>res_body<span class="token punctuation">.</span>text<span class="token punctuation">,</span> <span class="token string">'html.parser'</span><span class="token punctuation">)</span>
            <span class="token keyword">if</span> soup_body<span class="token punctuation">.</span>text<span class="token punctuation">.</span>__contains__<span class="token punctuation">(</span><span class="token string">"404"</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">continue</span>
            script <span class="token operator">=</span> soup_body<span class="token punctuation">.</span>select<span class="token punctuation">(</span><span class="token string">"body script"</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">.</span>get_text<span class="token punctuation">(</span><span class="token punctuation">)</span>
            pre_music <span class="token operator">=</span> re<span class="token punctuation">.</span>search<span class="token punctuation">(</span>pattern1<span class="token punctuation">,</span> script<span class="token punctuation">)</span><span class="token punctuation">.</span>group<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
            self<span class="token punctuation">.</span>names<span class="token punctuation">.</span>append<span class="token punctuation">(</span>re<span class="token punctuation">.</span>split<span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> tag<span class="token punctuation">.</span>text<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment"># 获取歌名</span>
            self<span class="token punctuation">.</span>musics<span class="token punctuation">.</span>append<span class="token punctuation">(</span>pre_music<span class="token punctuation">.</span>replace<span class="token punctuation">(</span><span class="token string">r'\/'</span><span class="token punctuation">,</span> <span class="token string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 获取歌曲资源</span>
            self<span class="token punctuation">.</span>num <span class="token operator">=</span> self<span class="token punctuation">.</span>num<span class="token operator">+</span><span class="token number">1</span>
    self<span class="token punctuation">.</span>song_show<span class="token punctuation">(</span><span class="token punctuation">)</span>

此处会有可能会出现一个问题,就是通过第一步获得的歌曲网址未必能打开,有的歌曲没有版权所以访问会出现404的情况,为了避免程序报错,我们需要跳过404的页面,所以有语句:if soup_body.text.__contains__("404"): continue

歌词方面同上都是先写出正则表达式,然后匹配得到结果,此处便不再演示,需要注意的是得到的歌词都带着\r\n字符,这显然不是我们所希望的,所以我们使用lyric.replace(r'\r\n', '\n')语句将这些字符转换为换行符,即可实现每句歌词占一行

二、爬取热门歌曲

这个部分我们爬取的网站地址为:http://m.yue365.com/bang/box100_w.shtml

步骤与上文无太大差别

值得一提的是该网站使用的编码是’gb2312’,而我们的编译器一般默认编码是utf-8,所以会出现乱码的情况

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

所以我们应该对获得的响应体设置编码

对于获取到的数据,我们采用excel存储

首先使用xlsxwriter.Workbook('popsongs.xlsx')创建一个名为popsongs的excel文件,可以再参数中设置格式,也可以利用add_format()创建格式

随后创建一个表单workbook.add_worksheet('pop500')命名为pop500,并使用 worksheet.set_column()设置列宽(第一个参数为起始列,第二个参数为终止列,第三个参数为长度,行数列数都从0开始)

之后就可以使用worksheet.write_row(row, 0, ['排名', '歌名', '歌曲url', '歌曲热度'], first_format)对每一行写入数据了,第一个参数为目标行,第二个参数为起始列,第三个参数为写入的值,从起始列开始写入列表中的值,第四个参数为之前所创建的格式

最后记得关闭excel文件,便大功告成了

<span class="token keyword">def</span> <span class="token function">pop_songs</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
    row <span class="token operator">=</span> <span class="token number">0</span>
    base <span class="token operator">=</span> <span class="token string">'http://m.yue365.com/'</span>
    url <span class="token operator">=</span> <span class="token string">'http://m.yue365.com/bang/box100_w.shtml'</span>
    pattern <span class="token operator">=</span> <span class="token string">r'width:(.*)%'</span>
    self<span class="token punctuation">.</span>hot <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
    res_body <span class="token operator">=</span> requests<span class="token punctuation">.</span>get<span class="token punctuation">(</span>url<span class="token punctuation">)</span>
    res_body<span class="token punctuation">.</span>encoding <span class="token operator">=</span> <span class="token string">'gb2312'</span>
    soup_body <span class="token operator">=</span> BeautifulSoup<span class="token punctuation">(</span>res_body<span class="token punctuation">.</span>text<span class="token punctuation">,</span> <span class="token string">'lxml'</span><span class="token punctuation">)</span>
    songs <span class="token operator">=</span> soup_body<span class="token punctuation">.</span>findAll<span class="token punctuation">(</span>class_<span class="token operator">=</span><span class="token string">'name'</span><span class="token punctuation">)</span>
    hot <span class="token operator">=</span> soup_body<span class="token punctuation">.</span>findAll<span class="token punctuation">(</span><span class="token string">'span'</span><span class="token punctuation">,</span> class_<span class="token operator">=</span><span class="token string">'dib'</span><span class="token punctuation">)</span>
    workbook <span class="token operator">=</span> xlsxwriter<span class="token punctuation">.</span>Workbook<span class="token punctuation">(</span><span class="token string">'popsongs.xlsx'</span><span class="token punctuation">)</span>
    first_format <span class="token operator">=</span> workbook<span class="token punctuation">.</span>add_format<span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span><span class="token string">'align'</span><span class="token punctuation">:</span> <span class="token string">'center'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
    second_format <span class="token operator">=</span> workbook<span class="token punctuation">.</span>add_format<span class="token punctuation">(</span><span class="token punctuation">{<!-- --></span><span class="token string">'align'</span><span class="token punctuation">:</span> <span class="token string">'left'</span><span class="token punctuation">}</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">'pop500'</span><span class="token punctuation">)</span>
    worksheet<span class="token punctuation">.</span>set_column<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">)</span>
    worksheet<span class="token punctuation">.</span>set_column<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span>
    worksheet<span class="token punctuation">.</span>set_column<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">46</span><span class="token punctuation">)</span>
    worksheet<span class="token punctuation">.</span>set_column<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">10</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">'歌曲url'</span><span class="token punctuation">,</span> <span class="token string">'歌曲热度'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> first_format<span class="token punctuation">)</span>
    <span class="token keyword">for</span> song <span class="token keyword">in</span> songs<span class="token punctuation">:</span>
        self<span class="token punctuation">.</span>hot<span class="token punctuation">.</span>append<span class="token punctuation">(</span>re<span class="token punctuation">.</span>split<span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> hot<span class="token punctuation">[</span>row<span class="token operator">*</span><span class="token number">2</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'style'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
        row <span class="token operator">+=</span> <span class="token number">1</span>
        song_url <span class="token operator">=</span> base<span class="token operator">+</span>song<span class="token punctuation">.</span>a<span class="token punctuation">[</span><span class="token string">'href'</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>row<span class="token punctuation">,</span> song<span class="token punctuation">.</span>a<span class="token punctuation">.</span>text<span class="token punctuation">,</span> song_url<span class="token punctuation">,</span> self<span class="token punctuation">.</span>hot<span class="token punctuation">[</span>row<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span> second_format<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><span class="token string">'popsongs.xlsx'</span><span class="token punctuation">)</span>

使用os.startfile('popsongs.xlsx')在写完数据后自动打开文件

三、爬取热门歌手及其歌曲

这个部分我们爬取的网站地址为:https://www.9ku.com/geshou/all-all-liuxing.htm

值得一提的是使用songs = soup_body.findAll(class_="songNameA")最后会多获取到一些我们不需要的数据,且都是在末尾的十八条,故通过以下代码晒去后面十八条信息

<span class="token keyword">for</span> song <span class="token keyword">in</span> songs<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">18</span><span class="token punctuation">]</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>song<span class="token punctuation">.</span>text<span class="token punctuation">,</span> url_base<span class="token operator">+</span>song<span class="token punctuation">[</span><span class="token string">'href'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> url_base<span class="token operator">+</span>lyrics<span class="token punctuation">[</span>row<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">'href'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    row <span class="token operator">+=</span> <span class="token number">1</span>

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

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

相关文章:

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