
摘要本文介绍了Python网络爬虫开发中常用的模块和工具。首先讲解了如何使用virtualenv创建独立的Python开发环境包括安装、创建项目目录、激活虚拟环境及安装模块等步骤。重点介绍了四个核心爬虫工具Requests库轻量级HTTP请求库、Urllib3库URL数据处理工具常配合BeautifulSoup使用、Selenium浏览器自动化测试框架和Scrapy框架完整的爬虫解决方案。每个工具都提供了安装方法和基础使用示例如Requests获取网页HTML、Urllib3结合BeautifulSoup解析数据、Selenium控制浏览器操作等。这些工具为开发者提供了从简单网页抓取到复杂自动化爬虫的不同层级解决方案。目录Python 网络爬虫 —— 爬虫常用模块基于 virtualenv 搭建 Python 开发环境安装 virtualenv创建并进入项目目录初始化虚拟环境激活虚拟环境在虚拟环境中安装模块退出虚拟环境Python 网络爬虫常用模块Requests 库安装 Requests 库实用示例Urllib3 库安装 Urllib3 库实用示例结合 Urllib3 与 BeautifulSoup 实现爬虫Selenium 库安装 Selenium 库下载浏览器驱动实用示例Scrapy 框架安装 Scrapy 框架Python 网络爬虫 —— 爬虫常用模块本章我们将学习可用于 Python 网络爬虫开发的各类模块。基于 virtualenv 搭建 Python 开发环境virtualenv 是一款用于创建独立 Python 环境的工具。借助 virtualenv我们可以创建一个文件夹其中包含运行 Python 项目所需包的所有必要可执行文件。同时它允许我们在不访问全局 Python 安装环境的情况下添加和修改 Python 模块。安装 virtualenv可通过以下命令安装 virtualenvplaintext(myenv) D:\Projects\python\myenvpip3 install virtualenv Collecting virtualenv Downloading virtualenv-20.36.1-py3-none-any.whl.metadata (4.7 kB) Collecting distlib1,0.3.7 (from virtualenv) Downloading distlib-0.4.0-py2.py3-none-any.whl.metadata (5.2 kB) Collecting filelock4,3.20.1 (from virtualenv) Downloading filelock-3.20.3-py3-none-any.whl.metadata (2.1 kB) Requirement already satisfied: platformdirs5,3.9.1 in .\Lib\site-packages (from virtualenv) (4.5.1) Downloading virtualenv-20.36.1-py3-none-any.whl (6.0 MB) Downloading distlib-0.4.0-py2.py3-none-any.whl (469 kB) Downloading filelock-3.20.3-py3-none-any.whl (16 kB) Installing collected packages: distlib, filelock, virtualenv Successfully installed distlib-0.4.0 filelock-3.20.3 virtualenv-20.36.1创建并进入项目目录通过以下命令创建一个用于存放爬虫项目的目录再进入该目录plaintext(myenv) D:\Projects\python\myenvmkdir webscrap (myenv) D:\Projects\python\myenvcd webscrap初始化虚拟环境按如下方式初始化自定义的虚拟环境文件夹plaintext(base) D:\ProgramData\webscrapvirtualenv websc created virtual environment CPython3.14.2.final.0-64 in 981ms creator CPython3Windows(destD:\Projects\python\myenv\webscrap\websc, clearFalse, no_vcs_ignoreFalse, globalFalse) seeder FromAppData(downloadFalse, pipbundle, viacopy, app_data_dirC:\Users\mahes\AppData\Local\pypa\virtualenv) added seed packages: pip25.3 activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator激活虚拟环境使用以下命令激活虚拟环境激活成功后命令行左侧会出现带括号的虚拟环境名称plaintext(myenv) D:\Projects\python\myenv\webscrapwebsc\scripts\activate在虚拟环境中安装模块激活后即可在该独立环境中安装所需模块示例为安装 requests 库plaintext(websc) D:\Projects\python\myenv\webscrappip3 install requests Collecting requests Using cached requests-2.32.5-py3-none-any.whl.metadata (4.9 kB) Collecting charset_normalizer4,2 (from requests) Using cached charset_normalizer-3.4.4-cp314-cp314-win_amd64.whl.metadata (38 kB) Collecting idna4,2.5 (from requests) Using cached idna-3.11-py3-none-any.whl.metadata (8.4 kB) Collecting urllib33,1.21.1 (from requests) Downloading urllib3-2.6.3-py3-none-any.whl.metadata (6.9 kB) Collecting certifi2017.4.17 (from requests) Downloading certifi-2026.1.4-py3-none-any.whl.metadata (2.5 kB) Using cached requests-2.32.5-py3-none-any.whl (64 kB) Using cached charset_normalizer-3.4.4-cp314-cp314-win_amd64.whl (107 kB) Using cached idna-3.11-py3-none-any.whl (71 kB) Downloading urllib3-2.6.3-py3-none-any.whl (131 kB) Downloading certifi-2026.1.4-py3-none-any.whl (152 kB) Installing collected packages: urllib3, idna, charset_normalizer, certifi, requests Successfully installed certifi-2026.1.4 charset_normalizer-3.4.4 idna-3.11 requests-2.32.5 urllib3-2.6.3退出虚拟环境使用以下命令即可退出当前激活的虚拟环境命令行左侧的环境标识会消失plaintext(websc) D:\Projects\python\myenv\webscrapdeactivate D:\Projects\python\myenv\webscrapPython 网络爬虫常用模块网络爬虫是指构建一个代理程序使其能自动从网络中提取、解析、下载并整理有用信息。简单来说无需手动从网站保存数据爬虫软件可根据需求自动从多个网站加载并提取数据。本节将介绍适用于网络爬虫开发的实用 Python 库。Requests 库Requests 是一款轻量的 Python 网络爬虫库也是一款高效的 HTTP 库主要用于访问网页。借助 Requests我们可以获取网页的原始 HTML 代码再对其进行解析以提取数据。使用前需先完成安装。安装 Requests 库可在虚拟环境或全局 Python 环境中安装通过 pip 命令即可快速完成plaintext(myenv) D:\Projects\python\myenvpip3 install requests Requirement already satisfied: requests in .\Lib\site-packages (2.32.5) Requirement already satisfied: charset_normalizer2 in .\Lib\site-packages (from requests) (3.4.4) Requirement already satisfied: idna2.5 in .\Lib\site-packages (from requests) (3.11) Requirement already satisfied: urllib31.21.1 in .\Lib\site-packages (from requests) (2.6.2) Requirement already satisfied: certifi2017.4.17 in .\Lib\site-packages (from requests) (2025.11.12)实用示例本示例中我们将对某网页发起 GET HTTP 请求步骤如下进入 Python 交互环境导入 requests 库plaintext(websc) D:\Projects\python\myenv\webscrappy Python 3.14.2 (tags/v3.14.2:df79316, Dec 5 2025, 17:18:21) [MSC v.1944 64 bit (AMD64)] on win32 Type help, copyright, credits or license for more information. import requests调用 requests 库对目标网址发起 GET 请求plaintext r requests.get(https://authoraditiagarwal.com/)通过.text属性获取网页内容示例为提取前 200 个字符plaintext r.text[:200] !DOCTYPE htmlhtml langen-US idhtmlheadmeta charsetUTF-8 /meta http-equivX-UA-Compatible contentIE10 /link relprofile hrefhttp://gmpg.org/xfn/11 /link relpingback 执行后可得到如下输出结果截取前部分内容plaintextOut[5]: !DOCTYPE html\nhtml langen-US\n\titemscope\n\titemtypehttp://schema.org/WebSite \n\tprefixog: http://ogp.me/ns# \nhead\n\tmeta charsetUTF-8/\n\tmeta http-equivX-UA-Compatible contentIEUrllib3 库Urllib3 是另一款 Python 网络库功能与 Requests 类似均可从 URL 中获取数据。其详细技术文档可参考https://urllib3.readthedocs.io/en/latest/安装 Urllib3 库可通过 pip 命令在虚拟环境或全局环境中安装示例中同时安装 Urllib3 和 bs4 库plaintext(websc) D:\Projects\python\myenv\webscrappip3 install urllib3 bs4 Requirement already satisfied: urllib3 in d:\projects\python\myenv\webscrap\websc\lib\site-packages (2.6.3) Collecting bs4 Downloading bs4-0.0.2-py2.py3-none-any.whl.metadata (411 bytes) Collecting beautifulsoup4 (from bs4) Using cached beautifulsoup4-4.14.3-py3-none-any.whl.metadata (3.8 kB) Collecting soupsieve1.6.1 (from beautifulsoup4-bs4) Downloading soupsieve-2.8.3-py3-none-any.whl.metadata (4.6 kB) Collecting typing-extensions4.0.0 (from beautifulsoup4-bs4) Using cached typing_extensions-4.15.0-py3-none-any.whl.metadata (3.3 kB) Downloading bs4-0.0.2-py2.py3-none-any.whl (1.2 kB) Using cached beautifulsoup4-4.14.3-py3-none-any.whl (107 kB) Downloading soupsieve-2.8.3-py3-none-any.whl (37 kB) Using cached typing_extensions-4.15.0-py3-none-any.whl (44 kB) Installing collected packages: typing-extensions, soupsieve, beautifulsoup4, bs4 Successfully installed beautifulsoup4-4.14.3 bs4-0.0.2 soupsieve-2.8.3 typing-extensions-4.15.0实用示例结合 Urllib3 与 BeautifulSoup 实现爬虫本示例将结合 Urllib3 和 BeautifulSoup 实现网页爬取通过 Urllib3 替代 Requests 库获取网页原始 HTML 数据再通过 BeautifulSoup 解析该 HTML 数据。编写 main.py 文件python运行import urllib3 from bs4 import BeautifulSoup http urllib3.PoolManager() r http.request(GET, https://authoraditiagarwal.com) soup BeautifulSoup(r.data, html.parser) print(soup.title) print(soup.title.text)运行结果plaintexttitleLearn and Grow with Aditi Agarwal/title Learn and Grow with Aditi AgarwalSelenium 库Selenium 是一款开源的 Web 应用自动化测试套件支持在不同浏览器和平台上运行它并非单一工具而是一套软件集。目前 Selenium 提供了 Python、Java、C#、Ruby 和 JavaScript 等语言的绑定库本节将使用 Selenium 的 Python 绑定库实现网络爬虫。你可通过相关链接学习基于 Java 的 Selenium 使用方法Selenium Python 绑定库提供了便捷的 API可调用 Firefox、IE、Chrome、Remote 等各类 Selenium WebDriver 驱动。目前支持的 Python 版本为 2.7、3.5 及以上。安装 Selenium 库通过 pip 命令即可在虚拟环境或全局环境中安装plaintextpip install selenium下载浏览器驱动Selenium 需要对应的浏览器驱动才能与选定浏览器交互不同浏览器的驱动下载相关链接如下表表格浏览器驱动下载相关链接Chromehttps://www.google.com/intl/en_in/chrome/Edgehttps://developer.microsoft.com/Firefoxhttps://github.com/Safarihttps://webkit.org/实用示例本示例展示如何使用 Selenium 实现网络爬虫该方法也可用于 Selenium 自动化测试。下载对应浏览器版本的驱动后通过 Python 编写代码步骤如下从 selenium 库中导入 webdriver 模块python运行from selenium import webdriver配置下载好的浏览器驱动路径python运行path rC:\\Users\\gaurav\\Desktop\\Chromedriver browser webdriver.Chrome(executable_path path)让驱动控制浏览器打开目标网址python运行browser.get(https://authoraditiagarwal.com/leadershipmanagement)可通过 XPath 定位并爬取网页指定元素类似 lxml 的 XPath 用法python运行browser.find_element_by_xpath(/html/body).click()运行代码后可在 Python 脚本控制的浏览器中查看操作结果。Scrapy 框架Scrapy 是一款基于 Python 开发的快速开源网络爬虫框架它通过基于 XPath 的选择器从网页中提取数据。Scrapy 于 2008 年 6 月 26 日首次发布采用 BSD 开源协议2015 年 6 月发布了里程碑式的 1.0 版本。该框架提供了从网站提取、处理和结构化数据所需的所有工具。安装 Scrapy 框架通过 pip3 命令即可在虚拟环境或全局环境中安装plaintextpip3 install scrapy