各位集美兄得看过来! 利用AI给青春有你2的选手们做数据分析挖掘(一):爬虫选手信息

Eilene ·
更新时间:2024-09-21
· 676 次阅读

各位集美兄得看过来! 利用AI给青春有你2的选手们做数据分析挖掘(一):爬虫选手信息

前言

最近爱奇艺真正热播的大型青春少女选秀节目—青春有你2,简直火爆全网,现在谁还不会几句,“淡黄的长裙,蓬松的头发”

img

好,我们走进正题,青春有你2就是各位大众通过投票的方式,选出9位少女成团,那我们AI在这个过程中能发挥什么作用呢?

我,我一个浪迹各种圈的工具人,内娱(内地娱乐)怎么会不懂,投票这种东西光靠自带粉丝是不够了,每期青春有你2播出,粉头们都会带着小粉丝,各种刷数据、刷流量、刷话题,增加自家艺人的热度,然后再成功圈一波又一波的新粉,一波又一波~~~~,这里提到数据、流量、话题,一般来说粉丝会自己进行统计,利用一些报表工具或者微博提供的热度分析工具等等,但是不够全面阿,这里我将使用AI Studio 给大家快速地实现如何利用AI对青春有你2的选手们进行数据分析数据挖掘,通过结果给大家呈现目前选手们的真实热度状况。

首先,我们从获取青春有你2的选手所有基本信息,包括获取姓名身高以及大量的图片。因此我们需要基于Python来做爬虫,爬虫的过程,就是模仿浏览器的行为,往目标站点发送请求,接收服务器的响应数据,提取需要的信息,并进行保存的过程,Python为爬虫的实现提供了工具:requests模块、BeautifulSoup库。

爬虫过程说明

一般爬虫的过程如下:

1.发送请求(requests模块)

2.获取响应数据(服务器返回)

3.解析并提取数据(BeautifulSoup查找或者re正则)

4.保存数据

实践 环境准备 基础环境

CPU: 2 Cores.

RAM: 8GB.

Disk: 100GB

Python: 3.7.4

核心使用Python库

request模块:

requests是python实现的简单易用的HTTP库,官网地址:http://cn.python-requests.org/zh_CN/latest/ requests.get(url)可以发送一个http get请求,返回服务器响应内容。

BeautifulSoup库:

BeautifulSoup 是一个可以从HTML或XML文件中提取数据的Python库。网址:https://beautifulsoup.readthedocs.io/zh_CN/v4.4.0/ BeautifulSoup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,其中一个是 lxml。 BeautifulSoup(markup, "html.parser")或者BeautifulSoup(markup, "lxml"),推荐使用lxml作为解析器,因为效率更高。 分析爬虫网站

由于我们现阶段目标是获取青春有你2的选手所有基本信息,因此我们选择百度作为我们的数据源,

具体数据URL:https://baike.baidu.com/item/青春有你第二季/23802025

我们进去看一下,可以发现,在参赛学员这个模块有很完整的选手信息,我们可以爬这个页面,再解析这部分数据
在这里插入图片描述

在爬虫之前,我们先看看他的页面结构,这块就是我们需要爬取的数据,他是一个table的页面接口,每一列对应选手的基本信息属性,我们爬到页面之后只需要按照标签的顺序取值。

在这里插入图片描述

爬《青春有你2》中所有参赛选手信息,返回页面数据

爬取页面具体实现:

import json import re import requests import datetime from bs4 import BeautifulSoup import os #获取当天的日期,并进行格式化,用于后面文件命名,格式:20200420 today = datetime.date.today().strftime('%Y%m%d') def crawl_wiki_data(): """ 爬取百度百科中《青春有你2》中参赛选手信息,返回html """ headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36' } url='https://baike.baidu.com/item/青春有你第二季' try: response = requests.get(url,headers=headers) print(response.status_code) #将一段文档传入BeautifulSoup的构造方法,就能得到一个文档的对象, 可以传入一段字符串 soup = BeautifulSoup(response.text,'html.parser') #返回的是class为table-view log-set-param的所有标签 tables = soup.find_all('table',{'class':'table-view log-set-param'}) crawl_table_title = "参赛学员" for table in tables: #对当前节点前面的标签和字符串进行查找 table_titles = table.find_previous('div').find_all('h3') for title in table_titles: if(crawl_table_title in title): return table except Exception as e: print(e) 解析页面获取数据

现在我们已经实现了爬取参赛学员的表格,然后按照我们前面说的进行解析表格,并存储到json文件

def parse_wiki_data(table_html): ''' 从百度百科返回的html中解析得到选手信息,以当前日期作为文件名,存JSON文件,保存到work目录下 ''' bs = BeautifulSoup(str(table_html),'html.parser') all_trs = bs.find_all('tr') error_list = ['\'','\"'] stars = [] for tr in all_trs[1:]: all_tds = tr.find_all('td') star = {} print(all_tds) #姓名 star["name"]=all_tds[0].text #个人百度百科链接 star["link"]= 'https://baike.baidu.com' + all_tds[0].find('a').get('href') #籍贯 star["zone"]=all_tds[1].text #星座 star["constellation"]=all_tds[2].text #身高 star["height"]=all_tds[3].text #体重 star["weight"]= all_tds[4].text #花语,去除掉花语中的单引号或双引号 flower_word = all_tds[5].text for c in flower_word: if c in error_list: flower_word=flower_word.replace(c,'') star["flower_word"]=flower_word #公司 if not all_tds[6].find('a') is None: star["company"]= all_tds[6].find('a').text else: star["company"]= all_tds[6].text stars.append(star) json_data = json.loads(str(stars).replace("\'","\"")) with open('work/' + today + '.json', 'w', encoding='UTF-8') as f: json.dump(json_data, f, ensure_ascii=False)

输出JSON数据结果如下:

在这里插入图片描述

分析如何爬取选手高清美照

这一步绝对是没有任何私心的,因为后面的课程需要,我们需要爬取选手的图片

就是下面这块图集,我们需要拿到选手百度词条的图集,而链接就在红色框框住的地方。

在这里插入图片描述

我们“F12”看看 页面结构找到图集的入口

在这里插入图片描述


然后跳转链接过去,继续F12可以看到'pic-item'的标签列表就是存放图册的,我们就可以根据这个标签获取图集所有的图片下载地址

在这里插入图片描述

到这里,我们再总结一下获取图片的流程

1.爬选手个人百度信息页面 2.解析信息页面的右侧图片跳转信息 3.通过2获取的URL,爬取选手的图集 4.解析图集,下载图片到指定目录 爬取选手高清美照链接

具体实现逻辑如下

def crawl_pic_urls(): ''' 爬取每个选手的百度百科图片,并保存 ''' with open('work/'+ today + '.json', 'r', encoding='UTF-8') as file: json_array = json.loads(file.read()) headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36' } for star in json_array: pic_urls = [] pic_set = set() name = star['name'] link = star['link'] #!!!请在以下完成对每个选手图片的爬取,将所有图片url存储在一个列表pic_urls中!!! response = requests.get(link,headers=headers) print(response.status_code) #将一段文档传入BeautifulSoup的构造方法,就能得到一个文档的对象, 可以传入一段字符串 soup = BeautifulSoup(response.text,'html.parser') #picture imgs_middle = soup.find_all('a',{'class':'image-link'}) imgs_left = soup.find_all('div',{'class':'summary-pic'}) imgs_url = [] imgs= soup.find_all('a',{'class':'lemma-album'}) # 这种方式获取全部图册 1052张 # for item in imgs: # imgResponse = requests.get("https://baike.baidu.com"+item['href'],headers=headers) # imgSoup = BeautifulSoup(imgResponse.text,'html.parser') # imgs_a= imgSoup.find_all('a',{'class':'pic-item'}) # for a_item in imgs_a: # if a_item.img['src'] not in pic_set: # pic_set.add(a_item.img['src']) # pic_urls.append(a_item.img['src']) # 这种方式获取词条图册 482张 for item in imgs_left: imgResponse2 = requests.get("https://baike.baidu.com"+item.a['href'],headers=headers) imgSoup2 = BeautifulSoup(imgResponse2.text,'html.parser') imgs_a2= imgSoup2.find_all('a',{'class':'pic-item'}) for a_item in imgs_a2: if a_item.img: if a_item.img['src'] not in pic_set: pic_set.add(a_item.img['src']) pic_urls.append(a_item.img['src']) #!!!根据图片链接列表pic_urls, 下载所有图片,保存在以name命名的文件夹中!!! down_pic(name,pic_urls) 下载高清美照def down_pic(name,pic_urls): ''' 根据图片链接列表pic_urls, 下载所有图片,保存在以name命名的文件夹中, ''' path = 'work/'+'pics/'+name+'/' if not os.path.exists(path): os.makedirs(path) print("正在下载:%s" %(str(name))) for i, pic_url in enumerate(pic_urls): try: pic = requests.get(pic_url, timeout=15) string = str(i + 1) + '.jpg' with open(path+string, 'wb') as f: f.write(pic.content) print('成功下载第%s张图片: %s' % (str(i + 1), str(pic_url))) except Exception as e: print('下载第%s张图片时失败: %s' % (str(i + 1), str(pic_url))) print(e) continue def show_pic_path(path): ''' 遍历所爬取的每张图片,并打印所有图片的绝对路径 ''' pic_num = 0 for (dirpath,dirnames,filenames) in os.walk(path): for filename in filenames: pic_num += 1 print("第%d张照片:%s" % (pic_num,os.path.join(dirpath,filename))) print("共爬取《青春有你2》选手的%d照片" % pic_num)

结果:
在这里插入图片描述

在这里插入图片描述

总结

首先要称赞一下AI studio的工具确实是好用,而且对于我这么一个不会Python什么的工具人,通过百度提供的AI课堂,也就很快上手了,毕竟有其他语言基础,接下来还会继续搞青春有你2助力榜数据分析挖掘阿、颜值打分、大众情感分析什么的,是一个不错的课程,敬请继续关注!

参考资料:

https://aistudio.baidu.com/aistudio/course


作者:Mark_MMXIX



看过 爬虫 数据 数据分析

需要 登录 后方可回复, 如果你还没有账号请 注册新账号