DERECK数据爬取

豆瓣网:

import requests
from lxml import etree
from time import sleep
import numpy as np
from openpyxl import Workbook
# 进行UA伪装
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.41'}
# 定义空列表存放电影数据
titles_cn = []  # 中文标题
titles_en = []  # 英文标题
links = []  # 详情页链接
director = []  # 导演
actors = []  # 演员
years = []  # 上映年份
nations = []  # 国籍
types = []  # 类型
scores = []  # 评分
rating_nums = []  # 评分人数
# 创建一个Workbook对象
wb = Workbook()
# 激活当前工作表
ws = wb.active
# 写入表头
ws.append(
    ['电影中文名', '电影英文名', '电影详情页链接', '导演', '演员', '上映年份', '国籍', '类型', '评分', '评分人数'])
for i in range(0, 226, 25):
    url = f'https://movie.douban.com/top250?start={i}&filter='
    response = requests.get(url, headers=headers)
    sleep(1)
    html = response.text
    data = etree.HTML(html)
    li_list = data.xpath('//*[@id="content"]/div/div[1]/ol/li')
    for each in li_list:
        title1 = each.xpath('./div/div[2]/div[1]/a/span[1]/text()')[0]
        titles_cn.append(title1)
        title2 = each.xpath('./div/div[2]/div[1]/a/span[2]/text()')[0].strip('\xa0/\xa0')
        titles_en.append(title2)
        link = each.xpath('./div/div[2]/div[1]/a/@href')[0]
        links.append(link)
        info1 = each.xpath('./div/div[2]/div[2]/p[1]/text()[1]')[0].strip()
        split_info1 = info1.split('\xa0\xa0\xa0')
        dirt = split_info1[0].strip('导演: ')
        director.append(dirt)
        if len(split_info1) == 2:
            ac = split_info1[1].strip('主演: ')
            actors.append(ac)
        else:
            actors.append(np.nan)
        info2 = each.xpath('./div/div[2]/div[2]/p[1]/text()[2]')[0].strip()
        split_info2 = info2.split('\xa0/\xa0')
        year = split_info2[0]
        nation = split_info2[1]
        ftype = split_info2[2]
        years.append(year)
        nations.append(nation)
        types.append(ftype)
        score = each.xpath('./div/div[2]/div[2]/div/span[2]/text()')[0]
        scores.append(score)
        num = each.xpath('./div/div[2]/div[2]/div/span[4]/text()')[0].strip('人评价')
        rating_nums.append(num)
        ws.append([title1, title2, link, dirt, ac, year, nation, ftype, score, num])
    print(f'————————————第{int((i / 25) + 1)}页爬取完毕!——————————————')
# 保存工作簿
wb.save('douban_top250.xlsx')
print('——————————————————————————————————爬虫结束!!!!!————————————————————————————————————————————————')

电影大数据描述性统计分析:

import csv
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pandas as pd
import random
#导入
data_df=pd.read_excel('douban_top250.xlsx')
#清洗
rows_with_na=data_df[data_df.isnull().values]
cleaned=data_df.dropna()
#保存
cleaned.to_excel('cleaned_top250.xlsx', index=False)
data2_df=pd.read_excel('cleaned_top250.xlsx')

print(data2_df.describe())
print(data2_df.info())

pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)  # 注意这里是max_rows,不是max_row
pd.set_option('display.width', None)
plt.rcParams['font.sans-serif'] = ['SimHei']

#显示全部列
pd.set_option('display.max_columns', None)
#显示全部行
pd.set_option('display.max_row', None)
#设置数据的显示长度(解决自动换行)
pd.set_option('display.width', None)

# 中文乱码
plt.rcParams['font.sans-serif']=['SimHei']

# 设置画布大小
plt.figure(figsize=(10, 6))

# 设置网格线
plt.grid(axis='y', linestyle=':')

# 读表
df = pd.read_excel('cleaned_top250.xlsx')
df_mov=df.head(10)
df_select=df_mov['电影中文名']
x = df_mov['电影中文名']
y = df_mov['评分']

# 设置标签
plt.xlabel('电影中文名')
plt.ylabel('评分')
plt.title('学号加姓名电影评分', fontsize=18)

# 设置文本标签
for a, b in zip(x, y):
    plt.text(a, b, format(b, ','), ha='center', va='bottom', alpha=0.9)

plt.bar(x, y, alpha=0.5, label='评分')
# 设置图例
plt.legend()

plt.show()


pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)  # 注意这里是max_rows,不是max_row
pd.set_option('display.width', None)
plt.rcParams['font.sans-serif'] = ['SimHei']

df = pd.read_excel('cleaned_top250.xlsx')

director_counts = df['导演'].value_counts().reset_index()
director_counts.columns = ['导演', '电影数量']

top4_directors = director_counts.nlargest(4, '电影数量')

print(top4_directors)

plt.figure(figsize=(10, 6))
plt.bar(top4_directors['导演'], top4_directors['电影数量'], alpha=0.5)
plt.xlabel('导演')
plt.ylabel('上榜电影数量')
plt.title('学号加姓名前四位导演上榜电影数量', fontsize=18)

for index, row in top4_directors.iterrows():
    plt.text(row['导演'], row['电影数量'], str(row['电影数量']), ha='center', va='bottom', alpha=0.9)

plt.grid(axis='y', linestyle=':')
plt.show()







pd.set_option('display.max_columns', None)
#显示全部行
pd.set_option('display.max_row', None)
#设置数据的显示长度(解决自动换行)
pd.set_option('display.width', None)
# 中文乱码
plt.rcParams['font.sans-serif']=['SimHei']
# 设置画布大小
plt.figure(figsize=(10, 6))
# 设置网格线
plt.grid(axis='x', linestyle=':')
# 读表
df = pd.read_excel('cleaned_top250.xlsx')
df_mov=df.head(10)
df_select=df_mov['电影中文名']
x = df_mov['电影中文名']
y = df_mov['评分人数']
# 设置标签
plt.xlabel('电影中文名')
plt.ylabel('评分人数')
plt.title('学号电影评分人数', fontsize=18)
# 设置文本标签
for a, b in zip(x, y):
    plt.text(a, b, format(b, ','), ha='center', va='bottom', alpha=0.9)
plt.plot(x, y, alpha=0.5, label='评分人数')
# 设置图例
plt.legend()
plt.show()

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值