python爬虫与数据可视化(一) |
您所在的位置:网站首页 › 猫眼电影爬虫及分析 › python爬虫与数据可视化(一) |
又是新的一年,让我们一起来看一下刚刚过去的2018留下了哪些经典影片吧! 一、获取电影详情页链接 进入猫眼官网,按图中的顺序点击,得到2018年按评分排序的影片进入猫眼官网,按图中的顺序点击,得到2018年按评分排序的影片页面![]() ![]() ![]() 在开发者工具中可以看到数字并没有像网页上正常显示出来,这是猫眼做了反爬虫处理 ![]() ![]() 因为每个字体文件数字顺序和编码都不同,实际使用中这段代码需要你把base_num_list ,base_unicode_list 改成你在百度字体编辑器打开你自己保存的base.woff文件的序列 maoyan.woff是爬虫遇到的字体文件,返回值是破解后的数字和编码序列,后面要用于替换 4. 贴代码(注意代码中的utf-8编码,不能漏掉!!): # 解析电源详细页面,获得影片名称,票房等 def get_data(film_url, headers):for url in film_url:data = {}response = get_page(url, headers)soup = BeautifulSoup(response, 'html.parser')data['_id'] = url[len('https://maoyan.com/films/'):]data["name"] = soup.find_all('h3', class_='name')[0].get_text()data["type"] = soup.find_all('li', class_='ellipsis')[0].get_text()data["country"] = soup.find_all('li', class_='ellipsis')[1].get_text().split('/')[0].strip()data['time'] = soup.find_all('li', class_='ellipsis')[1].get_text().split('/')[1].strip()# 获取被编码的数字score_code = soup.find_all('span', class_='stonefont')[0].get_text().strip().encode('utf-8')data['score'] = str(score_code, encoding='utf-8')score_num_code = soup.find_all('span', class_='stonefont')[1].get_text().strip().encode('utf-8')data['score_num'] = str(score_num_code, encoding='utf-8')booking_office_code = soup.find_all('span', class_='stonefont')[2].get_text().strip().encode('utf-8')data['booking_office'] = str(booking_office_code, encoding='utf-8')# 票房单位unit = soup.find_all('span', class_='unit')[0].get_text().strip()# 破解maoyan_num_list, utf8last = get_numbers(response)# print(maoyan_num_list, utf8last)# 进行替换得到正确的结果for i in range(len(utf8last)):data['score'] = data['score'].replace(utf8last[i], maoyan_num_list[i])data['score_num'] = data['score_num'].replace(utf8last[i], maoyan_num_list[i])data['booking_office'] = data['booking_office'].replace(utf8last[i], maoyan_num_list[i])# 对单位进行换算if '万' in data['score_num']:data['score_num'] = int(float(data['score_num'][:len(data['score_num'])-1]) * 10000)if '万' == unit:data['booking_office'] = int(float(data['booking_office']) * 10000)elif '亿' == unit:data['booking_office'] = int(float(data['booking_office']) * 100000000)# print(data)to_mongodb(data)这样就拿到了电影名称,票房,评分之类的信息啦,放入数据库就好了。下面我们随便做一点数据可视化就好了,不能光放数据嘛,拿出来看看 三、数据可视化用pyecharts随便做了三张图表看看,要直接生成图片好像还得安装node.js,这三张图片是生成html再下载下来的,代码在github |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |