(个人纪录)pip install 豆瓣源的使用

使用豆瓣pypi镜像源
本文介绍如何利用豆瓣提供的pypi镜像源http://pypi.douban.com/simple/来解决在中国大陆地区访问官方Python包索引(pypi.python.org/pypi)不稳定的问题。通过修改pip的安装命令为sudopipinstall-ihttp://pypi.douban.com/simple/并添加受信任主机pypi.douban.com,可以显著提升第三方库的下载速度。

sudo pip install -i http://pypi.douban.com/simple/ saltTesting --trusted-host pypi.douban.com


虽然用easy_install和pip来安装第三方库很方便
它们的原理其实就是从Python的官方源pypi.python.org/pypi 下载到本地,然后解包安装。
不过因为某些原因,访问官方的pypi不稳定,很慢甚至有些还时不时的访问不了。

跟ubuntu的apt和centos的yum有各个镜像源一样,pypi也有。

在国内的强烈推荐豆瓣的源
http://pypi.douban.com/simple/

注意后面要有/simple目录。


超级好用


import requests from bs4 import BeautifulSoup import pandas as pd import time import os def crawl_douban_top250(): """ 爬取豆瓣电影Top250数据并保存为Excel文件 """ print('开始爬取豆瓣电影Top250...') all_movies = [] # 爬取10页数据 for page in range(0, 10): url = f'https://movie.douban.com/top250?start={page * 25}' print(f'正在爬取第{page + 1}页...') try: # 发送请求 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', 'Referer': 'https://movie.douban.com/' } response = requests.get(url, headers=headers, timeout=10) if response.status_code == 200: soup = BeautifulSoup(response.text, 'html.parser') movies = soup.select('.grid_view li') print(f" 找到 {len(movies)} 部电影") for i, movie in enumerate(movies): movie_data = extract_movie_data(movie) if movie_data: all_movies.append(movie_data) if i < 5: # 只显示前3部电影的进度 print(f" - {movie_data['电影中文名']}") print(f" - ....................") else: print(f" 请求失败,状态码: {response.status_code}") except Exception as e: print(f' 第{page + 1}页爬取失败: {e}') # 短暂延迟 time.sleep(1) # 保存数据 if all_movies: save_to_excel(all_movies) else: print("错误: 没有爬取到任何数据!") def extract_movie_data(movie): """ 从单个电影元素中提取数据 """ try: # 基础信息 movie_link = movie.select_one('.hd a')['href'] movie_image = movie.select_one('.pic img')['src'] # 电影名称 title_elements = movie.select('.title') chinese_name = title_elements[0].text if title_elements else '' foreign_name = title_elements[1].text if len(title_elements) > 1 else '' # 其他名称 other_elem = movie.select_one('.other') other_names = other_elem.text.strip() if other_elem else '' # 评分信息 rating = movie.select_one('.rating_num').text if movie.select_one('.rating_num') else '' rating_count_elem = movie.select_one('.star span') rating_count = rating_count_elem.text.replace('人评价', '') if rating_count_elem else '0' # 导演和演员信息 info_elem = movie.select_one('.bd p') director_actor = info_elem.contents[0].strip() if info_elem else '' # 简介 quote_elem = movie.select_one('.quote .inq') summary = quote_elem.text if quote_elem else '' # 类型信息(从导演信息中提取) movie_type = extract_movie_type(director_actor) return { '影片链接': movie_link, '影片图片': movie_image, '电影中文名': chinese_name, '电影外文名': foreign_name, '电影别名': other_names, '评分': rating, '评价人数': rating_count, '概况': summary, '导演/主演': director_actor, '类型': movie_type } except Exception as e: print(f' 提取电影数据失败: {e}') return None def extract_movie_type(director_actor): """ 从导演信息中提取电影类型 """ if not director_actor: return "" type_keywords = { '剧情': '剧情', '喜剧': '喜剧', '动作': '动作', '爱情': '爱情', '科幻': '科幻', '悬疑': '悬疑', '惊悚': '惊悚', '犯罪': '犯罪', '冒险': '冒险', '奇幻': '奇幻', '动画': '动画', '战争': '战争', '恐怖': '恐怖', '纪录片': '纪录片' } for keyword, movie_type in type_keywords.items(): if keyword in director_actor: return movie_type return "剧情" # 默认类型 def save_to_excel(movies_data): """ 保存数据到Excel文件 """ print(f"\n开始保存数据,共{len(movies_data)}条记录...") # 确保savefiles目录存在 if not os.path.exists('savefiles'): print("创建 savefiles 目录") os.makedirs('savefiles') # 创建DataFrame df = pd.DataFrame(movies_data) # 保存为Excel文件 excel_path = 'savefiles/douban.xlsx' try: df.to_excel(excel_path, index=False, engine='openpyxl') print(f"✓ 数据已成功保存到: {excel_path}") # 显示数据统计 print(f"\n数据统计:") print(f" 总电影数: {len(df)}") except Exception as e: print(f"✗ 保存Excel文件失败: {e}") print("尝试安装依赖: pip install openpyxl") if __name__ == '__main__': crawl_douban_top250() 用另一种python语法重写一下上述代码,使其功能保持不变但结构,形式上变化
最新发布
10-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值