加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

Python数据可视化:2018年电影分析 -

发布时间:2018-11-22 09:42:53 所属栏目:教程 来源:法纳斯特
导读:双11已经过去,双12即将来临,离2018年的结束也就2个月不到,还记得年初立下的flag吗? 完成了多少?相信很多人和我一样,抱头痛哭... 本次利用猫眼电影,实现对2018年的电影大数据进行分析。 1 网页分析 01 标签 通过点击猫眼电影已经归类好的标签,得到网

03 获取电影详情页信息

  1. def get_message(url): 
  2.     """ 
  3.     获取电影详情页里的信息 
  4.     """ 
  5.     time.sleep(10) 
  6.     data = {} 
  7.     host = """refer: http://maoyan.com/news 
  8.     """ 
  9.     header = head + host 
  10.     headers = str_to_dict(header) 
  11.     response = requests.get(url=url, headers=headers) 
  12.     u = response.text 
  13.     # 破解猫眼文字反爬 
  14.     (mao_num_list, utf8last) = get_numbers(u) 
  15.     # 获取电影信息 
  16.     soup = BeautifulSoup(u, "html.parser") 
  17.     mw = soup.find_all('span', {'class': 'stonefont'}) 
  18.     score = soup.find_all('span', {'class': 'score-num'}) 
  19.     unit = soup.find_all('span', {'class': 'unit'}) 
  20.     ell = soup.find_all('li', {'class': 'ellipsis'}) 
  21.     name = soup.find_all('h3', {'class': 'name'}) 
  22.     # 返回电影信息 
  23.     data["name"] = name[0].get_text() 
  24.     data["type"] = ell[0].get_text() 
  25.     data["country"] = ell[1].get_text().split('/')[0].strip().replace('n', '') 
  26.     data["length"] = ell[1].get_text().split('/')[1].strip().replace('n', '') 
  27.     data["released"] = ell[2].get_text()[:10] 
  28.     # 因为会出现没有票房的电影,所以这里需要判断 
  29.     if unit: 
  30.         bom = ['分', score[0].get_text().replace('.', '').replace('万', ''), unit[0].get_text()] 
  31.         for i in range(len(mw)): 
  32.             moviewish = mw[i].get_text().encode('utf-8') 
  33.             moviewish = str(moviewish, encoding='utf-8') 
  34.             # 通过比对获取反爬文字信息 
  35.             for j in range(len(utf8last)): 
  36.                 moviewish = moviewish.replace(utf8last[j], maoyan_num_list[j]) 
  37.             if i == 0: 
  38.                 data["score"] = moviewish + bom[i] 
  39.             elif i == 1: 
  40.                 if '万' in moviewish: 
  41.                     data["people"] = int(float(moviewish.replace('万', '')) * 10000) 
  42.                 else: 
  43.                     data["people"] = int(float(moviewish)) 
  44.             else: 
  45.                 if '万' == bom[i]: 
  46.                     data["box_office"] = int(float(moviewish) * 10000) 
  47.                 else: 
  48.                     data["box_office"] = int(float(moviewish) * 100000000) 
  49.     else: 
  50.         bom = ['分', score[0].get_text().replace('.', '').replace('万', ''), 0] 
  51.         for i in range(len(mw)): 
  52.             moviewish = mw[i].get_text().encode('utf-8') 
  53.             moviewish = str(moviewish, encoding='utf-8') 
  54.             for j in range(len(utf8last)): 
  55.                 moviewish = moviewish.replace(utf8last[j], maoyan_num_list[j]) 
  56.             if i == 0: 
  57.                 data["score"] = moviewish + bom[i] 
  58.             else: 
  59.                 if '万' in moviewish: 
  60.                     data["people"] = int(float(moviewish.replace('万', '')) * 10000) 
  61.                 else: 
  62.                     data["people"] = int(float(moviewish)) 
  63.         data["box_office"] = bom[2] 
  64.     yield data 

4 数据存储

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读