WenQuanYi fonts' url

I'am under Kubuntu ,and the fcitx is in comming ..... so ,I must use my poor English here... :-( http://wqy.sourceforge.net/cgi-bin/index.cgi?HalfTeal_INSTALL_zh just for backup. this url contains almost anything about WenQuanYi.
import requests from bs4 import BeautifulSoup import pandas as pd import matplotlib.pyplot as plt import re from matplotlib.font_manager import FontProperties # 设置中文字体 try: font = FontProperties(fname=r"C:\Windows\Fonts\simhei.ttf") # Windows系统默认黑体 except: # 如果找不到指定字体,使用matplotlib支持的其他中文字体 font = FontProperties(family=["SimHei", "WenQuanYi Micro Hei", "Heiti TC"]) # 请求头,模拟浏览器访问 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} def crawl_douban_movies(url, limit=10): """爬取豆瓣电影数据""" try: response = requests.get(url, headers=headers) response.raise_for_status() # 检查请求是否成功 soup = BeautifulSoup(response.text, 'html.parser') movies = [] # 找到电影条目 items = soup.select('div.item') for item in items[:limit]: try: # 电影名称 title = item.select_one('span.title').text.strip() # 导演、主演、类型、制片国家、年份 info = item.select_one('div.bd p').text.strip() lines = info.split('\n') directors_actors = lines[0].strip() details = lines[1].strip() # 提取导演 director_match = re.search(r'导演: (.*?) ', directors_actors) director = director_match.group(1) if director_match else '未知' # 提取主演 actors_match = re.search(r'主演: (.*)', directors_actors) actors = actors_match.group(1) if actors_match else '未知' # 提取类型、制片国家、年份 year_country_genre = details.split('/') year = year_country_genre[0].strip() if year_country_genre else '未知' country = year_country_genre[1].strip() if len(year_country_genre) > 1 else '未知' genre = year_country_genre[2].strip() if len(year_country_genre) > 2 else '未知' # 评分 rating = item.select_one('span.rating_num').text.strip() movies.append({ '名称': title, '导演': director, '主演': actors, '类型': genre, '制片国家/地区': country, '年份': year, '评分': float(rating) }) except Exception as e: print(f"解析单个电影时出错: {e}") continue return pd.DataFrame(movies) except Exception as e: print(f"爬取电影数据时出错: {e}") return pd.DataFrame() def count_us_movies(df): """统计制片国家为美国的电影数量""" us_movies = df[df['制片国家/地区'].str.contains('美国', na=False)] return len(us_movies), us_movies def visualize_ratings(df): """可视化电影评分""" plt.figure(figsize=(12, 8)) # 创建评分柱状图 bars = plt.bar(df['名称'], df['评分'], color='skyblue') # 在柱状图上添加评分值 for bar in bars: height = bar.get_height() plt.text(bar.get_x() + bar.get_width()/2., height, f'{height:.1f}', ha='center', va='bottom', fontproperties=font) # 设置图表标题和标签 plt.title('豆瓣电影评分对比', fontproperties=font, fontsize=16) plt.xlabel('电影名称', fontproperties=font, fontsize=12) plt.ylabel('评分', fontproperties=font, fontsize=12) # 设置x轴标签旋转以便更好显示 plt.xticks(rotation=45, ha='right', fontproperties=font) # 设置y轴范围 plt.ylim(8, 10) # 添加网格线 plt.grid(axis='y', linestyle='--', alpha=0.7) # 调整布局 plt.tight_layout() return plt if __name__ == "__main__": # 豆瓣电影Top250页面 url = 'https://movie.douban.com/top250' # 爬取电影数据 print("正在爬取豆瓣电影数据...") movies_df = crawl_douban_movies(url, limit=10) if not movies_df.empty: # 保存数据到CSV文件 csv_file = '豆瓣电影Top10.csv' try: movies_df.to_csv(csv_file, index=False, encoding='utf-8-sig') print(f"\n数据已成功保存到 {csv_file}") except Exception as e: print(f"\n保存CSV文件时出错: {e}") # 显示爬取的数据 print("\n爬取的电影数据:") print(movies_df.to_csv(sep='\t', na_rep='nan')) # 统计美国电影数量 us_count, us_movies = count_us_movies(movies_df) print(f"\n制片国家/地区包含'美国'的电影数量: {us_count}") if not us_movies.empty: print("这些电影是:") print(us_movies[['名称', '制片国家/地区']]) # 可视化评分 print("\n生成评分可视化图表...") plot = visualize_ratings(movies_df) plot.show() else: print("未能爬取到电影数据。") 在原来数据不变的情况下让可是图表更加美观
06-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值