1. 概念
'''一 Scrapy 爬虫框架
发送请求 ---> 获取响应数据 ---> 解析数据 ---> 保存数据
** Scarpy 框架介绍 **
1, 引擎(EGINE)
引擎负责控制系统所有组件之间的数据流, 并在某些动作发生时触发事件. 有关详细信息, 请参见上面的数据流部分.
2, 调度器(SCHEDULER)
用来接受引擎发过来的请求, 压入队列中, 并在引擎再次请求的时候返回. 可以想像成一个 URL 的优先级队列, 由它来决定下一个要抓取的网址是什么, 同时去除重复的网址
3, 下载器(DOWLOADER)
用于下载网页内容, 并将网页内容返回给 EGINE, 下载器是建立在 twisted 这个高效的异步模型上的
4, 爬虫(SPIDERS)
SPIDERS 是开发人员自定义的类, 用来解析 responses, 并且提取 items, 或者发送新的请求
5, 项目管道(ITEM PIPLINES)
在 items 被提取后负责处理它们, 主要包括清理, 验证, 持久化 (比如存到数据库) 等操作
下载器中间件 (Downloader Middlewares) 位于 Scrapy 引擎和下载器之间, 主要用来处理从 EGINE 传到 DOWLOADER 的请求 request, 已经从 DOWNLOADER 传到 EGINE 的响应 response,
你可用该中间件做以下几件事:
(1) process a request just before it is sent to the Downloader (i.e. right before Scrapy sends the request to the website);
(2) change received response before passing it to a spider;
(3) send a new Request instead of passing received response to a spider;
(4) pass response to a spider without fetching a web page;
(5) silently drop some requests.
6, 爬虫中间件(Spider Middlewares)
位于 EGINE 和 SPIDERS 之间, 主要工作是处理 SPIDERS 的输入 (即 responses) 和输出(即 requests)
** Scarpy 安装 **
1,pip3 install wheel
2,pip3 install lxml
3,pip3 install pyopenssl
4,pip3 install pypiwin32
5, 安装 twisted 框架
下载 twisted
http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted
安装下载好的 twisted
pip3 install 下载目录 \ Twisted-17.9.0-cp36-cp36m-win_amd64.whl
6,pip3 install scrapy
** Scarpy 使用 **
1, 进入终端 cmd
- scrapy
C:\Users\administortra>scrapy
Scrapy 1.6.0 - no active project
2, 创建 scrapy 项目
1. 创建一个文件夹, 专门用于存放 scrapy 项目
- D:\Scrapy_prject
2.cmd 终端输入命令
scrapy startproject Spider_Project( 项目名)
- 会在 D:\Scrapy_prject 文件夹下会生成一个文件
Spider_Project : Scrapy 项目文件
3. 创建爬虫程序
cd Spider_Project # 切换到 scrapy 项目目录下
# 爬虫程序名称 目标网站域名
scrapy genspider baidu www.baidu.com # 创建爬虫程序
3, 启动 scrapy 项目, 执行爬虫程序
# 找到爬虫程序文件进行执行
scrapy runspider 只能执行某个 爬虫程序. py
# 切换到爬虫程序执行文件目录下
- cd D:\Scrapy_prject\Spider_Project\Spider_Project\spiders
- scrapy runspider baidu.py
# 根据爬虫名称找到相应的爬虫程序执行
scrapy crawl 爬虫程序名称
# 切换到项目目录下
- cd D:\Scrapy_prject\Spider_Project
- scrapy crawl baidu
'''
2. 如何使用
3.main.py
- # from scrapy.cmdline import execute
- # # 写终端的命令
- # # scrapy crawl
- # execute(["scrapy","crawl","baidu"])
- from scrapy.cmdline import execute
- # execute(["scrapy","genspider","lianjia","lianjia.com"])
- # execute("scrapy crawl lianjia".split(" "))
- execute("scrapy crawl --nolog lianjia".split(" "))
- # -*- coding: utf-8 -*-
- import scrapy
- from scrapy import Request
- # response 的类
- from scrapy.http.response.html import HtmlResponse
- class LianjiaSpider(scrapy.Spider):
- name = 'lianjia' # 爬虫程序名
- # 只保留包含 lianjia.com 的 url
- allowed_domains = ['lianjia.com'] # 限制域名
- # 存放初始请求 url
- start_urls = ['https://bj.lianjia.com/ershoufang/']
- def parse(self, response): # response 返回的响应对象
- # print(response)
- # print(type(response))
- # # 获取文本
- # print(response.text)
- # print(response.url)
- # 获取区域列表 url
- area_list = response.xpath('//div[@data-role="ershoufang"]/div/a')
- # 遍历所有区域列表
- for area in area_list:
- print(area)
- '''
- .extract()提取多个
- .extract_first()提取一个
- '''
- # 1, 区域名称
- area_name = area.xpath('./text()').extract_first()
- print(area_name)
- # 2, 区域二级 url
- area_url = 'https://bj.lianjia.com/' + area.xpath('./@href').extract_first()
- print(area_url)
- # 会把 area_url 的请求响应数据交给 callback 方法
- # yield 后面跟着的都会添加到生成器中
- yield Request(url=area_url, callback=self.parse_area)
- def parse_area(self, response):
- # print(response)
- house_list = response.xpath('//ul[@class="sellListContent"]')
- # print(house_list)
- if house_list:
- for house in house_list:
- house_name = house.xpath('.//div[@class="title"]/a/text()').extract_first()
- print(house_name)
- house_cost = house.xpath('.//div[@class="totalPrice]/text()').extract_first() +'万'
- print(house_cost)
- house_price = house.xpath('.//div[@class="unitPrice"]/span/text()').extract_first()
- print(house_price)
- pass
4. 微信机器人
- from wxpy import Bot
- from pyecharts import Pie
- import webbrowser
- # 实例化一个微信机器人对象
- bot = Bot()
- # 获取到微信的所有好友
- friends = bot.friends()
- # 设定男性 \ 女性 \ 位置性别好友名称
- attr = ['男朋友', '女朋友', '人妖']
- # 初始化对应好友数量
- value = [0, 0, 0]
- # 遍历所有的好友, 判断这个好友是男性还是女性
- for friend in friends:
- if friend.sex == 1:
- value[0] += 1
- elif friend.sex == 2:
- value[1] += 1
- else:
- value[2] += 1
- # 实例化一个饼状图对象
- pie = Pie('tank 的好友们!')
- # 图表名称 str, 属性名称 list, 属性所对应的值 list,is_label_show 是否现在标签
- pie.add('', attr, value, is_label_show=True)
- # 生成一个 HTML 文件
- pie.render('friends.html')
- # 打开 HTML 文件
- webbrowser.open('friends.html')
来源: http://www.bubuko.com/infodetail-3099820.html