wus点我
Scrapy
Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架。 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中。
其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的, 也可以应用在获取API所返回的数据(例如 Amazon Associates Web Services ) 或者通用的网络爬虫。Scrapy用途广泛,可以用于数据挖掘、监测和自动化测试。
Scrapy 使用了 Twisted异步网络库来处理网络通讯。整体架构大致如下5
深度
Scrapy主要包括了以下组件:
-
引擎(Scrapy)
用来处理整个系统的数据流处理, 触发事务(框架核心) -
调度器(Scheduler)
用来接受引擎发过来的请求, 压入队列中, 并在引擎再次请求的时候返回. 可以想像成一个URL(抓取网页的网址或者说是链接)的优先队列, 由它来决定下一个要抓取的网址是什么, 同时去除重复的网址 -
下载器(Downloader)
用于下载网页内容, 并将网页内容返回给蜘蛛(Scrapy下载器是建立在twisted这个高效的异步模型上的) -
爬虫(Spiders)
爬虫是主要干活的, 用于从特定的网页中提取自己需要的信息, 即所谓的实体(Item)。用户也可以从中提取出链接,让Scrapy继续抓取下一个页面 -
项目管道(Pipeline)
负责处理爬虫从网页中抽取的实体,主要的功能是持久化实体、验证实体的有效性、清除不需要的信息。当页面被爬虫解析后,将被发送到项目管道,并经过几个特定的次序处理数据。 -
下载器中间件(Downloader Middlewares)
位于Scrapy引擎和下载器之间的框架,主要是处理Scrapy引擎与下载器之间的请求及响应。 -
爬虫中间件(Spider Middlewares)
介于Scrapy引擎和爬虫之间的框架,主要工作是处理蜘蛛的响应输入和请求输出。 -
调度中间件(Scheduler Middewares)
介于Scrapy引擎和调度之间的中间件,从Scrapy引擎发送到调度的请求和响应。
Scrapy运行流程大概如下:
引擎从调度器中取出一个链接(URL)用于接下来的抓取
引擎把URL封装成一个请求(Request)传给下载器
下载器把资源下载下来,并封装成应答包(Response)
爬虫解析Response
解析出实体(Item),则交给实体管道进行进一步的处理
解析出的是链接(URL),则把URL交给调度器等待抓取
一、安装
Linux
pip3 install scrapy
Windows a. pip3 install wheel
b. 下载twisted http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted (版本号依据自己电脑和系统定)
c. 进入下载目录,执行 pip3 install Twisted‑17.1.0‑cp35‑cp35m‑win_amd64.whl (版本号依据自己电脑和系统定)
d. pip3 install scrapy
e. 下载并安装pywin32:https://sourceforge.net/projects/pywin32/files/
f. pip3 install scrapy -i
http://pypi.douban.com/simple --trusted-host
pypi.douban.com
g. pip3 install pywin32 -i
http://pypi.douban.com/simple --trusted-host
pypi.douban.com
g. pip3 install pywin32 -i
二、基本使用
1. 基本命令
1. scrapy startproject 项目名称 如 scrapy startproject xianglong

- 在当前目录中创建中创建一个项目文件(类似于Django)
cd xianglong #进入目录
scrapy genspider chouti chouti.com #创建一个爬虫项目 genspider 蜘蛛 创建之后可以看懂这些内容

2. scrapy genspider [-t template] <name> <domain> - 创建爬虫应用 如: scrapy gensipider -t basic oldboy oldboy.com scrapy gensipider -t xmlfeed autohome autohome.com.cn PS: 查看所有命令:scrapy gensipider -l 查看模板命令:scrapy gensipider -d 模板名称 3. scrapy list - 展示爬虫应用列表 4. scrapy crawl 爬虫应用名称 - 运行单独爬虫应用
scrapy crawl chouti --nolog 运行爬虫项目
运行爬虫
在cmd中先进入到项目目录中
创建完项目之后的目录结构

文件说明:
scrapy.cfg 项目的主配置信息。(真正爬虫相关的配置信息在settings.py文件中)
items.py 设置数据存储模板,用于结构化数据,如:Django的Model
pipelines 数据处理行为,如:一般结构化的数据持久化
settings.py 配置文件,如:递归的层数、并发数,延迟下载等
spiders 爬虫目录,如:创建文件,编写爬虫规则
选择器
5.scrapy查询语法:
from scrapy.selector import HtmlXPathSelector 此解析器的解析查找标签的方法
当我们爬取大量的网页,如果自己写正则匹配,会很麻烦,也很浪费时间,令人欣慰的是,scrapy内部支持更简单的查询语法,帮助我们去html中查询我们需要的标签和标签内容以及标签属性。下面逐一进行介绍:
-
查询子子孙孙中的某个标签(以div标签为例)://div 查询儿子中的某个标签(以div标签为例):/div 查询标签中带有某个class属性的标签://div[@class=’c1′]即子子孙孙中标签是div且class=‘c1’的标签 查询标签中带有某个class=‘c1’并且自定义属性name=‘alex’的标签://div[@class=’c1′][@name=’alex’] 查询某个标签的文本内容://div/span/text() 即查询子子孙孙中div下面的span标签中的文本内容 查询某个属性的值(例如查询a标签的href属性)://a/@href
实例
# -*- coding: utf-8 -*- import scrapy from scrapy.selector import HtmlXPathSelector from bs4 import BeautifulSoup class ChoutiSpider(scrapy.Spider): name = 'chouti' allowed_domains = ['chouti.com'] start_urls = ['http://dig.chouti.com/'] def parse(self, response):
#HtmlXPathSelector 是一个解析器, 把response穿进去实例化一个HtmlXPathSelector对象就能够对这个对象使用响应的规则进行解析找 标签等内容 hxs = HtmlXPathSelector(response=response) # 去下载的页面中:找新闻,去子子孙孙中去找div id='content-list'的标签, # 并去他的儿子中去找 div class='item' 的表桥 items = hxs.xpath("//div[@id='content-list']/div[@class='item']") for item in items: #在这儿加了一个 . 表示从当前位置(不加 . 表示从根目录开始找)的相对位置去找, # 索引为1的a标签, href1 = item.xpath('.//div[@class="part1"]//a[1]') #[<HtmlXPathSelector xpath='.//div[@class="part1"]//a[1]' data='<a href="https://www.weibo.com/394967112'>] #这是找到的内容,为标签对象
print(item.xpath('.//div[]#class="part1"]//a[1]/text()')) #想要直接打印标签内部的文本
href2 = item.xpath('.//div[@class="part1"]//a[1]/text()').extract_first() #/text()能够取出a不标签内的文本,但是还是标签对象,如果想要取出具体的内容,需要在加上.extract_first() #这样就能把里边的文本取出来了 # # print(href2.strip())
href3 = item.xpath('.//div[@class="part1"]//a[1]/@href').extract_first()#获取标签的属性 print(href3)
在浏览器上复制查询路径的方法
代码 解析器使用方法详解
#!/usr/bin/env python # -*- coding:utf-8 -*- from scrapy.selector import Selector, HtmlXPathSelector from scrapy.http import HtmlResponse html = """<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> <ul> <li class="item-"><a id='i1' href="link.html">first item</a></li> <li class="item-0"><a id='i2' href="llink.html">first item</a></li> <li class="item-1"><a href="llink2.html">second item<span>vv</span></a></li> </ul> <div><a href="llink2.html">second item</a></div> </body> </html> """ response = HtmlResponse(url='http://example.com', body=html,encoding='utf-8') # hxs = HtmlXPathSelector(response) # print(hxs) # hxs = Selector(response=response).xpath('//a') # print(hxs) # hxs = Selector(response=response).xpath('//a[2]') # print(hxs) # hxs = Selector(response=response).xpath('//a[@id]') # print(hxs) # hxs = Selector(response=response).xpath('//a[@> # print(hxs) # hxs = Selector(response=response).xpath('//a[@href="link.html"][@> # print(hxs) # hxs = Selector(response=response).xpath('//a[contains(@href, "link")]') # print(hxs) # hxs = Selector(response=response).xpath('//a[starts-with(@href, "link")]') # print(hxs) # hxs = Selector(response=response).xpath('//a[re:test(@id, "i\d+")]') # print(hxs) # hxs = Selector(response=response).xpath('//a[re:test(@id, "i\d+")]/text()').extract() # print(hxs) # hxs = Selector(response=response).xpath('//a[re:test(@id, "i\d+")]/@href').extract() # print(hxs) # hxs = Selector(response=response).xpath('/html/body/ul/li/a/@href').extract() # print(hxs) # hxs = Selector(response=response).xpath('//body/ul/li/a/@href').extract_first() # print(hxs) # ul_list = Selector(response=response).xpath('//body/ul/li') # for item in ul_list: # v = item.xpath('./a/span') # # 或 # # v = item.xpath('a/span') # # 或 # # v = item.xpath('*/a/span') # print(v)