网络爬虫(二)

上节学习了:网络爬虫框架(一)

1、HTML解析之BeautifulSoup

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
实践操作步骤:
1、在cmd窗口下安装bs4

pip install bs4

在这里插入图片描述
参照,官网文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc/
在这里插入图片描述
在这里插入图片描述
具体代码实现:

from bs4 import BeautifulSoup  # 导入BeautifulSoup库
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""
# 创建一个BeautifulSoup对象,获取页面正文
soup = BeautifulSoup(html_doc, features="lxml")
print(soup)                  # 打印解析的HTML代码

运行程序,控制台输出信息:
在这里插入图片描述
解决方法:
安装lxml的解析器,我们可以在cmd窗口安装

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

在这里插入图片描述
运行程序,控制台输出信息:
在这里插入图片描述
若:打印标题

print(soup.title)                  # 打印解析的HTML代码

在这里插入图片描述


2、网络爬虫开发常用框架

在这里插入图片描述

框架

1、Scrapy

在这里插入图片描述

优点:快速而强大、易于扩展、便携式,python
网站:https://scrapy.org/
在这里插入图片描述

2、Crawley Project

网站:http://project.crawley-cloud.com/
在这里插入图片描述

3、pyspider

源码地址:https://github.com/binux/pyspider/releases
在这里插入图片描述
文档地址:http://docs.pyspider.org/


3、搭建Scrapy爬虫框架

在这里插入图片描述
在这里插入图片描述
安装Twisted模块,操作步骤:
1、打开(https://www.lfd.uci.edu/~gohlke/pythonlibs/
在这里插入图片描述
在这里插入图片描述
接着,用cmd窗口运行Twisted4.whl文件路径
在这里插入图片描述
以管理员启动:
在这里插入图片描述


C:\Windows\system32>d:

D:\python\Twisted>cd D:\python\Twisted

D:\python\Twisted>pip install Twisted-19.10.0-cp38-cp38-win_amd64.whl

找到并安装二进制文件,命令:

pip install Twisted-19.10.0-cp38-cp38-win_amd64.whl

在这里插入图片描述
安装Scrapy框架

D:\python\Twisted> pip install Scrapy -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

在这里插入图片描述
在这里插入图片描述
检查是否安装成功,执行命令:scrapy
在这里插入图片描述
接着,安装pywin32

D:\python\Twisted> pip install pywin32 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

在这里插入图片描述
安装完成


4、创建Scrapy项目并创建爬虫

快捷键:shift+鼠标右键
在这里插入图片描述
在这里插入图片描述
执行cmd命令:

scrapy startproject scrapyDemo

在这里插入图片描述
桌面多了一个scrapyDemo文件夹
在这里插入图片描述
接着,我们使用PyCharm工具打开,如图所示:
在这里插入图片描述
网页地址:http://quotes.toscrape.com/
在这里插入图片描述
操作步骤:
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
具体代码实现:

import scrapy  # 导入框架
class QuotesSpider(scrapy.Spider):
    name = "quotes"  # 定义爬虫名称

    def start_requests(self):
        # 设置爬取目标的地址
        urls = [
            'http://quotes.toscrape.com/page/1/',
            'http://quotes.toscrape.com/page/2/',
        ]
        # 获取所有地址,有几个地址发送几次请求
        for url in urls:
            # 发送网络请求
            yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):
        # 获取页数
        page = response.url.split("/")[-2]
        # 根据页数设置文件名称
        filename = 'quotes-%s.html' % page
        # 写入文件的模式打开文件,如果没有该文件将创建该文件
        with open(filename, 'wb') as f:
            # 像文件中写入获取的html代码
            f.write(response.body)
        # 输出保存文件的名称
        self.log('Saved file %s' % filename)

接着,就是运行,方式有两种:
第一种,使用命令行实现操作
在这里插入图片描述

scrapy crawl quotes

在这里插入图片描述
出现两个html文件
在这里插入图片描述
当能生成这两个文件时,说明爬虫成功了!

第二种方式:
在第一种方式上添加如下代码:

 # 导入CrawlerProcess类
        from scrapy.crawler import CrawlerProcess
        # 导入获取项目设置信息
        from scrapy.utils.project import get_project_settings

        # 程序入口
        if __name__ == '__main__':
            # 创建CrawlerProcess类对象并传入项目设置信息参数
            process = CrawlerProcess(get_project_settings())
            # 设置需要启动的爬虫名称
            process.crawl('quotes')
            # 启动爬虫
            process.start()

具体代码实现如下:

import scrapy  # 导入框架
class QuotesSpider(scrapy.Spider):
    name = "quotes"  # 定义爬虫名称

    def start_requests(self):
        # 设置爬取目标的地址
        urls = [
            'http://quotes.toscrape.com/page/1/',
            'http://quotes.toscrape.com/page/2/',
        ]
        # 获取所有地址,有几个地址发送几次请求
        for url in urls:
            # 发送网络请求
            yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):
        # 获取页数
        page = response.url.split("/")[-2]
        # 根据页数设置文件名称
        filename = 'quotes-%s.html' % page
        # 写入文件的模式打开文件,如果没有该文件将创建该文件
        with open(filename, 'wb') as f:
            # 像文件中写入获取的html代码
            f.write(response.body)
        # 输出保存文件的名称
        self.log('Saved file %s' % filename)

        # 导入CrawlerProcess类
        from scrapy.crawler import CrawlerProcess
        # 导入获取项目设置信息
        from scrapy.utils.project import get_project_settings

        # 程序入口
        if __name__ == '__main__':
            # 创建CrawlerProcess类对象并传入项目设置信息参数
            process = CrawlerProcess(get_project_settings())
            # 设置需要启动的爬虫名称
            process.crawl('quotes')
            # 启动爬虫
            process.start()

在这里插入图片描述


5、获取数据

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值