赵走x博客
网站访问量:152032
首页
书籍
软件
工具
古诗词
搜索
登录
Python3网络爬虫实战:46、付费讯代理、阿布云代理的使用
Python3网络爬虫实战:45、代理池的维护
Python3网络爬虫实战:44、代理的设置
Python3网络爬虫实战:43、微博宫格验证码的识别
Python3网络爬虫实战:42、点触点选验证码的识别
Python3网络爬虫实战:41、极验滑动验证码的识别
Python3网络爬虫实战:40、图形验证码的识别
Python3网络爬虫实战:38、动态渲染页面抓取:Splash的使用
Python3网络爬虫实战:37、动态渲染页面抓取:Selenium
Python3网络爬虫实战:36、分析Ajax爬取今日头条街拍美图
Python3网络爬虫实战:35、 Ajax数据爬取
Python3网络爬虫实战:34、数据存储:非关系型数据库存储:Redis
Python3网络爬虫实战:33、数据存储:非关系型数据库存储:MongoDB
Python3网络爬虫实战:32、数据存储:关系型数据库存储:MySQL
Python3网络爬虫实战:31、数据存储:文件存储
Python3网络爬虫实战:30、解析库的使用:PyQuery
Python3网络爬虫实战:29、解析库的使用:BeautifulSoup
Python3网络爬虫实战:28、解析库的使用:XPath
Python3网络爬虫实战:27、Requests与正则表达式抓取猫眼电影排行
Python3网络爬虫实战:26、正则表达式
Python3网络爬虫实战:25、requests:高级用法
Python3网络爬虫实战:24、requests:基本使用
Python3网络爬虫实战:23、使用Urllib:分析Robots协议
Python3网络爬虫实战:21、使用Urllib:处理异常
Python3网络爬虫实战:22、使用Urllib:解析链接
Python3网络爬虫实战:20、使用Urllib发送请求
Python3网络爬虫实战:19、代理基本原理
Python3网络爬虫实战:18、Session和Cookies
Python3网络爬虫实战:17、爬虫基本原理
Python3网络爬虫实战:16、Web网页基础
Python3网络爬虫实战:15、爬虫基础:HTTP基本原理
Python3网络爬虫实战:14、部署相关库的安装:Scrapyrt、Gerapy
Python3网络爬虫实战:13、部署相关库的安装:ScrapydClient、ScrapydAPI
Python3网络爬虫实战:12、部署相关库的安装:Docker、Scrapyd
Python3网络爬虫实战:11、爬虫框架的安装:ScrapySplash、ScrapyRedis
Python3网络爬虫实战:10、爬虫框架的安装:PySpider、Scrapy
Python3网络爬虫实战:9、APP爬取相关库的安装:Appium的安装
Python3网络爬虫实战:8、APP爬取相关库的安装:MitmProxy的安装
Python3网络爬虫实战:7、APP爬取相关库的安装:Charles的安装
Python3网络爬虫实战:6、Web库的安装:Flask、Tornado
Python3网络爬虫实战:5、存储库的安装:PyMySQL、PyMongo、RedisPy、RedisDump
Python3网络爬虫实战:4、数据库的安装:MySQL、MongoDB、Redis
Python3网络爬虫实战:3、解析库的安装:LXML、BeautifulSoup、PyQuery、Tesserocr
Python3网络爬虫实战:2、安装:GeckoDriver、PhantomJS、Aiohttp
Python3网络爬虫实战:1、请求库安装:Requests、Selenium、ChromeDriver
Python3网络爬虫实战:1、请求库安装:Requests、Selenium、ChromeDriver
资源编号:75745
Python3网络爬虫实战
爬虫
热度:103
在第一步抓取页面的过程中,我们就需要模拟浏览器向服务器发出请求,所以需要用到一些 Python 库来实现 HTTP 请求操作,在本书中我们用到的第三方库有 Requests、Selenium、Aiotttp 等。
爬虫可以简单分为几步:抓取页面、分析页面、存储数据。 在第一步抓取页面的过程中,我们就需要模拟浏览器向服务器发出请求,所以需要用到一些 Python 库来实现 HTTP 请求操作,在本书中我们用到的第三方库有 Requests、Selenium、Aiotttp 等。 在本节我们介绍一下这些请求库的安装方法。 # 1.1.1 Requests的安装 由于 Requests 属于第三方库,也就是 Python 默认不会自带这个库,需要我们手动去安装,下面我们首先看一下它的安装过程。 #### 1. 相关链接 * GitHub:https://github.com/requests/requests * PyPy:https://pypi.python.org/pypi/requests * 官方文档:http://www.python-requests.org * 中文文档:http://docs.python-requests.org/zh_CN/latest #### 2. 安装 ``` pip3 install requests ``` #### 3. 验证安装 为了验证库是否已经安装成功,可以在命令行下测试一下: ``` $ python3 >>> import requests ``` 在命令行首先输入 python3,进入命令行模式,然后输入如上内容,如果什么错误提示也没有,那么就证明我们已经成功安装了 Requests。 # 1.1.2 Selenium的安装 Selenium 是一个自动化测试工具,利用它我们可以驱动浏览器执行特定的动作,如点击、下拉等等操作,对于一些 JavaScript 渲染的页面来说,此种抓取方式非常有效,下面我们来看下 Selenium 的安装过程。 #### 1. 相关链接 * 官方网站:http://www.seleniumhq.org * GitHub:https://github.com/SeleniumHQ/selenium/tree/master/py * PyPi:https://pypi.python.org/pypi/selenium * 官方文档:http://selenium-python.readthedocs.io * 中文文档:http://selenium-python-zh.readthedocs.io #### 2. 安装 ``` pip3 install selenium ``` #### 4. 验证安装 进入 Python 命令行交互模式,导入一下 Selenium 包,如果没有报错,则证明安装成功。 ``` $ python3 >>> import selenium ``` 但这样还不够,我们还需要浏览器如 Chrome、Firefox 等浏览器来配合 Selenium 工作。 下面我们会介绍 Chrome、Firefox、PhantomJS 三种浏览器的配置方式,有了浏览器我们才可以配合 Selenium 进行页面的抓取。 # 1.1.3 ChromeDriver的安装 在上节我们成功安装好了 Selenium 库,但是它是一个自动化测试工具,需要浏览器来配合它使用,那么本节我们就介绍一下 Chrome 浏览器及 ChromeDriver 驱动的配置。 首先需要下载一个 Chrome 浏览器,方法多样,在此不再赘述。 随后我们需要安装一个 ChromeDriver 才能驱动 Chrome 浏览器完成相应的操作,下面我们来介绍下怎样安装 ChromeDriver。 #### 1. 相关链接 * 官方网站:https://sites.google.com/a/chromium.org/chromedriver * 下载地址:https://chromedriver.storage.googleapis.com/index.html #### 2. MAC安装 ``` brew install chromedriver ``` 如果安装失败,使用下面方法 ``` brew cask install chromedriver ``` #### 3. 验证安装 配置完成之后,就可以在命令行下直接执行 chromedriver 命令了。 命令行下输入: ``` chromedriver ``` 输入控制台有类似输出,如图 1-17 所示:  图 1-17 控制台输出 如果有类似输出则证明 ChromeDriver 的环境变量配置好了。 随后再在程序中测试,执行如下 Python 代码: ``` from selenium import webdriver browser = webdriver.Chrome() ``` 运行之后会弹出一个空白的 Chrome 浏览器,证明所有的配置都没有问题,如果没有弹出,请检查之前的每一步的配置。 如果弹出之后闪退,则可能是 ChromeDriver 版本和 Chrome 版本不简容,请更换 ChromeDriver 版本。 如果没有问题,接下来我们就可以利用 Chrome 来做网页抓取了。 #### 4. 结语 既然 Chrome 可以通过 Selenium 驱动,Firefox 也可以,如果想要实现 Selenium 驱动 Firefox 浏览器可以参考下面的 GeckoDriver 的安装。