- Python网络爬虫从入门到精通
- 明日科技编著
- 2066字
- 2025-02-14 21:27:51
1.4 搭建开发环境
1.4.1 安装Anaconda
Anaconda是一个完全免费的大规模数据处理、预测分析和科学计算工具。该工具中不仅集成了Python解析器,还有很多用于数据处理和科学计算的第三方模块,其中也包含许多网络爬虫所需要使用的模块,如requests模块、Beautiful Soup模块、lxml模块等。
在Windows系统下的浏览器中打开Anaconda的官方地址(https://www.anaconda.com/distribution/)下载对应的安装文件,如图1.2所示。

图1.2 下载Anaconda
这里笔者所选择的是Windows(64-Bit Graphical Installer为当时的最新版本),下载完成后直接双击运行下载的文件,在Welcome to Anaconda3(自己下载的版本)窗口中直接单击Next按钮,如图1.3所示。
在License Agreement窗口中直接单击I Agree按钮,如图1.4所示。

图1.3 Welcome to Anaconda3窗口

图1.4 License Agreement窗口
在Select Installation Type窗口内选中All Users(requires admin privileges)单选按钮,然后单击Next按钮,如图1.5所示。
在Choose Install Location窗口中选择自己的安装路径(建议不要使用中文路径),这里笔者选择一个自定义的安装路径,然后单击Next按钮,如图1.6所示。

图1.5 选中All Users(requires admin privileges)单选按钮

图1.6 选择安装路径
在Advanced Installation Options窗口中,选中第一个复选框,将Anaconda加入环境变量,然后单击Install按钮进行安装,如图1.7所示。
由于Anaconda中包含的模块较多,所以在安装过程中需要等待的时间较长,安装进度如图1.8所示。

图1.7 将Anaconda加入环境变量

图1.8 安装进度
安装进度完成以后,将进入Installation Complete窗口中,在该窗口中直接单击Next按钮,如图1.9所示。
由于Anaconda与JetBrains为合作关系,所以官方推荐使用PyCharm开发工具,在该窗口中直接单击Next按钮,如图1.10所示。

图1.9 安装完成

图1.10 PyCharm开发工具提示
最后在“Thanks for installing Anaconda3!”窗口中根据个人需求,选中或取消选中(笔者选择取消选中)两个复选框,再单击Finish按钮,如图1.11所示。
将Anaconda安装完成以后并保证已经添加系统环境变量的情况下,打开“命令提示符”窗口,然后输入“conda list”后按Enter键,即可查看当前Anaconda已经安装好的所有模块,如图1.12所示。

图1.11 安装结束

图1.12 查看当前Anaconda已经安装好的所有模块
1.4.2 PyCharm的下载与安装
PyCharm是由JetBrains公司开发的Python集成开发环境,由于其具有智能代码编辑器,可实现自动代码格式化、代码完成、智能提示、重构、单元测试、自动导入和一键代码导航等功能,目前已成为Python专业开发人员和初学者使用的有力工具。
打开PyCharm官网的下载地址(https://www.jetbrains.com/pycharm/download/),然后选择下载PyCharm的操作系统平台为Windows,单击开始下载社区版PyCharm(Community),如图1.13所示。

图1.13 PyCharm环境与版本下载选择页面
双击PyCharm安装包进行安装,在欢迎界面单击Next按钮进入软件安装路径设置界面,如图1.14所示。
在Choose Install Location窗口中选择一个需要安装的路径,这里不建议将安装路径设置在默认的C盘中,笔者选择自定义安装路径,确认安装路径后单击Next按钮,如图1.15所示。

图1.14 PyCharm欢迎界面

图1.15 设置PyCharm安装路径
在Installation Options窗口中首先在桌面快捷方式(Create Desktop Shortcut)中设置PyCharm程序的快捷方式,笔者系统为64位,所以选中64-bit launcher复选框,然后设置关联文件(Create Associations),选中“.py”复选框,这样以后再打开.py(.py文件是Python脚本文件,接下来编写的很多程序都是后缀名为.py的文件)文件时,会默认调用PyCharm打开,如图1.16所示。
在Choose Start Menu Folder窗口中直接单击Install按钮,如图1.17所示。

图1.16 设置快捷方式和关联

图1.17 选择开始菜单文件夹窗口
安装进度完成以后,在Completing PyCharm Community Edition Setup窗口中,在不直接运行PyCharm开发工具的情况下,单击Finish按钮即可,如图1.18所示。

图1.18 完成安装
1.4.3 配置PyCharm
双击PyCharm桌面快捷方式,启动PyCharm程序。选择是否导入开发环境配置文件,这里选择不导入,单击OK按钮,进入阅读协议页,如图1.19所示。

图1.19 环境配置文件窗口
在Set UI theme窗口中可以根据个人需求选择开发工具的主题样式,笔者这里选中Light,使用白色的主题颜色,然后单击Next:Featured plugins按钮,如图1.20所示。
在Download featured plugins窗口中,直接单击Start using PyCharm按钮,如图1.21所示,此时程序将进入欢迎界面。
进入PyCharm欢迎页,单击Create New Project,创建一个新工程文件,如图1.22所示。
在New Project窗口中,首先选择工程文件保存的路径,然后单击Create按钮,如图1.23所示。

图1.20 选择主题颜色

图1.21 下载特色插件

图1.22 PyCharm欢迎界面

图1.23 设置Python存储路径
工程创建完成以后,关闭Tip of the Day窗口,然后依次选择File→Settings选项,如图1.24所示。

图1.24 打开设置窗口
在Settings窗口中依次选择Project:demo(demo为自己编写的工程名称)→Project Interpreter,然后在右侧的下拉列表中选择Show All…,将打开Project Interpreters窗口,如图1.25所示。

图1.25 进入设置窗口
在Project Interpreters窗口中,单击右侧的“+”按钮,如图1.26所示。

图1.26 单击按钮
在Add Python Interpreter窗口中,首先单击左侧的System Interpreter选项,然后在右侧的下拉列表中选择Anaconda中的python.exe,最后单击OK按钮,如图1.27所示。

图1.27 添加Python编译器
返回Project Interpreters窗口后,选择新添加的Anaconda中的python.exe编译器,然后单击OK按钮,如图1.28所示。

图1.28 选择Anaconda中的Python编译器
返回Settings窗口,此时窗口中将自动显示出Anaconda内已经安装的所有Python模块,然后单击OK按钮,如图1.29所示。

图1.29 显示Anaconda内已经安装的Python模块
1.4.4 测试PyCharm
右击新建好的demo项目,在弹出的快捷菜单中选择New→Python File命令(一定要选择Python File项,这个至关重要,否则无法后续学习),如图1.30所示。

图1.30 新建Python文件
在新建文件对话框输入要建立的Python文件名hello world,如图1.31所示。随后按Enter键,即可完成新建Python文件工作。

图1.31 输入新建的Python文件名称
在新建文件的代码编辑区输入代码“print ("hello world!")”,如图1.32所示。

图1.32 输入代码
在编写代码的区域右击,在弹出的快捷菜单中选择Run 'hello world'命令,运行测试代码,如图1.33所示。

图1.33 运行Python测试代码
如果程序代码没有错误,那么将显示运行结果,如图1.34所示。

图1.34 显示程序运行结果