llllll

from bs4 import BeautifulSoup
import requests
import re
from tqdm import tqdm
import openpyxl
import time
import pandas as pd
import matplotlib.pyplot as plt

def get_data():
    wb = openpyxl.Workbook()
    sheet = wb.active
    sheet.append(['中文标题', '英文标题', '发行时间', '电影评分', '评价人数', '影片详细链接', '图片链接', '影片概况',
                  '导演名称'])
    wb.save('data.xlsx')
    for m in tqdm(range(0, 10)):  #
        nus = m * 25
        URL = f"https://movie.douban.com/top250?start={nus}&filter="
        Headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; an-ES; rv:1.9.0.20) Gecko/9663-05-06 15:44:33 Firefox/3.8'}
        response = requests.get(url=URL, headers=Headers)
        if response.status_code != 200:
            print(f"爬取失败,状态码是{response.status_code}")
        else:
            response.encoding = 'utf-8'
            soup = BeautifulSoup(response.text, 'html.parser')

            label_numbers = soup.select('#content>div>div.article>ol>li')
            for label_number in range(1, len(label_numbers) + 1):
                a1234 = f'#content > div > div.article > ol > li:nth-child({label_number}) '

                label1 = soup.select(a1234 + '> div > div.info > div.hd > a > span:nth-child(1)')

                label2 = soup.select(a1234 + f'> div > div.info > div.hd > a > span:nth-child(2)')
                english_label = label2[0].text.replace("/", "").strip()

                label4 = soup.select(a1234 + '> div > div.info > div.bd > p:nth-child(1)')

                start_time = re.findall(r'\d{4}', label4[0].text)

                label5 = soup.select(
                    f'#content > div > div.article > ol > li:nth-child({label_number}) > div > div.info > div.bd > div > span.rating_num')

                label6 = soup.select(
                    f'#content > div > div.article > ol > li:nth-child({label_number}) > div > div.info > div.bd > div > span:nth-child(4)')
                man_number = label6[0].text.rstrip('人评价')

                label7 = soup.select(
                    f'#content > div > div.article > ol > li:nth-child({label_number}) > div > div.info > div.hd > a')

                label8 = soup.select(
                    f'#content > div > div.article > ol > li:nth-child({label_number}) > div > div.pic > a > img')

                label10 = soup.select(
                    f'#content > div > div.article > ol > li:nth-child({label_number}) > div > div.info > div.bd > p:nth-child(1)')

                pattern = r'导演: ([\u4e00-\u9fa5]+)'
                directors = re.findall(pattern, label10[0].text)
                if directors == []:
                    director = '空'
                else:
                    director = directors[0]

                write_excel(label1[0].text, english_label,
                            start_time[0], label5[0].text, man_number
                            , label7[0]['href'], label8[0]['src'], label10[0].text, director)
                time.sleep(1)


def write_excel(a, b, c, d, e, f, g, h, i):
    wb = openpyxl.load_workbook('data.xlsx')
    sheet = wb.active
    sheet.append([a, b, c, d, e, f, g, h, i])
    wb.save('data.xlsx')


def turtle():
    wb = openpyxl.load_workbook('data.xlsx')

    plt.rcParams['font.sans-serif'] = 'SimHei'
    df = pd.read_excel('data.xlsx', sheet_name='Sheet')
    x = df['发行时间']
    y = df['评价人数']
    plt.figure(figsize=(5, 12), dpi=150)
    plt.title('202209424刘宁宇top250发行时间和评价人数')
    plt.xlabel('发行时间')
    plt.ylabel('评价人数')
    plt.bar(x, y, width=0.6)
    for a, b in zip(x, y):
        plt.text(x=a, y=b, s='{:.1f}万'.format(b / 1000000), color='black', fontsize=3, ha='center')
    plt.legend(['top250时间和人数'])
    time.sleep(1)
    mq = df.head(10)
    x = mq['中文标题']
    y = mq['电影评分']
    plt.figure(figsize=(5, 3), dpi=150)
    plt.title('202209424刘宁宇前十的电影评分')
    plt.stackplot(x, y)
    plt.xticks([])
    cellText = mq[['中文标题']].T.values.tolist()
    rowLabels = ['中文标题']
    colLabels = mq['电影评分'].values.tolist()
    plt.table(cellText=cellText, rowLabels=rowLabels, colLabels=colLabels)
    time.sleep(1)
    plt.figure(figsize=(5, 3), dpi=150)

    plt.pie(mq['电影评分'], labels=mq['中文标题'], autopct='%.2f%%')
    plt.title('202209424刘宁宇对应评分占比情况')
    time.sleep(1)
    x = mq['中文标题']
    y = mq['电影评分']
    plt.figure(figsize=(5, 3), dpi=150)
    plt.xlabel('中文标题')
    plt.ylabel('电影评分')
    plt.scatter(x, y)
    plt.show()


if __name__ == '__main__':
    get_data()
    turtle()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值