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

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

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

01 构造请求头

  1. head = """ 
  2. Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 
  3. Accept-Encoding:gzip, deflate, br 
  4. Accept-Language:zh-CN,zh;q=0.8 
  5. Cache-Control:max-age=0 
  6. Connection:keep-alive 
  7. Host:maoyan.com 
  8. Upgrade-Insecure-Requests:1 
  9. Content-Type:application/x-www-form-urlencoded; charset=UTF-8 
  10. User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36 
  11. """ 
  12.  
  13. def str_to_dict(header): 
  14.     """ 
  15.     构造请求头,可以在不同函数里构造不同的请求头 
  16.     """ 
  17.     header_dict = {} 
  18.     header = header.split('n') 
  19.     for h in header: 
  20.         h = h.strip() 
  21.         if h: 
  22.             k, v = h.split(':', 1) 
  23.             header_dict[k] = v.strip() 
  24.     return header_dict 

因为索引页和详情页请求头不一样,这里为了简便,构造了一个函数。

02 获取电影详情页链接

  1. def get_url(): 
  2.     """ 
  3.     获取电影详情页链接 
  4.     """ 
  5.     for i in range(0, 300, 30): 
  6.         time.sleep(10) 
  7.         url = 'http://maoyan.com/films?showType=3&yearId=13&sortId=3&offset=' + str(i) 
  8.         host = """Referer:http://maoyan.com/films?showType=3&yearId=13&sortId=3&offset=0 
  9.         """ 
  10.         header = head + host 
  11.         headers = str_to_dict(header) 
  12.         response = requests.get(url=url, headers=headers) 
  13.         html = response.text 
  14.         soup = BeautifulSoup(html, 'html.parser') 
  15.         data_1 = soup.find_all('div', {'class': 'channel-detail movie-item-title'}) 
  16.         data_2 = soup.find_all('div', {'class': 'channel-detail channel-detail-orange'}) 
  17.         num = 0 
  18.         for item in data_1: 
  19.             num += 1 
  20.             time.sleep(10) 
  21.             url_1 = item.select('a')[0]['href'] 
  22.             if data_2[num-1].get_text() != '暂无评分': 
  23.                 url = 'http://maoyan.com' + url_1 
  24.                 for message in get_message(url): 
  25.                     print(message) 
  26.                     to_mysql(message) 
  27.                 print(url) 
  28.                 print('---------------^^^Film_Message^^^-----------------') 
  29.             else: 
  30.                 print('The Work Is Done') 
  31.                 break 

(编辑:核心网)

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

热点阅读