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()
llllll
最新推荐文章于 2024-10-04 20:57:51 发布