一、scrapy介紹和快速使用
scrapy是python的爬蟲框架,類似于django(python的web框架)。
安裝:
- Mac、Linux
執(zhí)行pip3 install scrapy
,不存在任何問題 - Windows
執(zhí)行pip3 install scrapy
,如果安裝失敗,執(zhí)行下面步驟:
(1)安裝wheel(為支持通過文件安裝軟件):pip3 install wheel
(wheel官網(wǎng))
(2)安裝lxml:pip3 install lxml
(3)安裝pyopenssl:pip3 install pyopenssl
(4)下載并安裝pywin32(pywin32官網(wǎng) 或 github地址)
(5)下載twisted的wheel文件(twisted官網(wǎng)),執(zhí)行pip3 install 下載目錄\Twisted-17.9.0-cp36-cp36m-win_amd64.whl
(6)最后執(zhí)行pip3 install scrapy
注:twisted是一個(gè)流行的事件驅(qū)動(dòng)的python網(wǎng)絡(luò)框架,性能很高的網(wǎng)絡(luò)框架。
使用:
1 使用命令行創(chuàng)建scrapy項(xiàng)目
scrapy startproject 項(xiàng)目名
2 創(chuàng)建爬蟲(相當(dāng)于django創(chuàng)建app)
scrapy genspider 爬蟲名 爬蟲地址
3 運(yùn)行爬蟲
scrapy crawl 爬蟲名
4 使用IDE打開爬蟲項(xiàng)目(項(xiàng)目根目錄)
5 目錄結(jié)構(gòu)
myfirst_crawl # 項(xiàng)目名
myfirst_crawl # 文件夾
-spiders # 文件夾(所有的爬蟲都放在下面,一個(gè)py文件就是一個(gè)爬蟲)
-cnblogs.py # 爬蟲
middlewares.py # 以后中間件寫在這里
pipelines.py # 以后存數(shù)據(jù)
settings.py # 配置文件
scrapy.cfg # 項(xiàng)目部署相關(guān)
items.py # 相當(dāng)于models.py
二、scrapy框架原理
五大組件
spider:爬蟲,我們寫代碼的地方,爬取網(wǎng)址和解析數(shù)據(jù)
engine:引擎,大總管,掌管數(shù)據(jù)的流向(我們不管)
scheduler:調(diào)度器,負(fù)責(zé)調(diào)度哪個(gè)地址先爬取,哪個(gè)后爬?。ㄉ疃葍?yōu)先,廣度優(yōu)先)
downloader:下載器,負(fù)責(zé)真正的下載(twisted,異步)
pipeline:管道,存儲(chǔ)數(shù)據(jù)的地方(文件,mysql、redis…)
兩大中間件
爬蟲中間件:爬蟲和引擎之間
下載中間件:引擎和下載器之間(用的多)
三、scrapy解析數(shù)據(jù)
1.執(zhí)行命令
直接敲這個(gè)名令:scrapy gensipder 名字 地址
,等同于新建一個(gè)py文件
執(zhí)行爬蟲,不打印日志:scrapy crawl cnblogs --nolog
使用腳本運(yùn)行爬蟲:
項(xiàng)目根目錄下創(chuàng)建main.py
# 腳本執(zhí)行爬蟲,不用使用命令了
from scrapy.cmdline import execute
execute(['scrapy','crawl','cnblogs','--nolog'])
2.response對(duì)象的css方法和xpath方法
css中寫css選擇器,xpath中寫xpath選擇器
語法:
-xpath取文本內(nèi)容
'.//a[contains(@class,"link-title")]/text()'
-xpath取屬性
'.//a[contains(@class,"link-title")]/@href'
-css取文本
'a.link-title::text'
-css取屬性
'img.image-scale::attr(src)'
兩個(gè)方法:
.extract_first() # 取一個(gè)
.extract() # 取所有
spiders目錄下爬蟲文件參數(shù):文章來源:http://www.zghlxwxcb.cn/news/detail-553413.html
import scrapy
class CnblogsSpider(scrapy.Spider):
name = 'cnblogs' # 爬蟲名
allowed_domains = ['www.cnblogs.com'] # 允許爬取的域
start_urls = ['http://www.cnblogs.com/'] # 爬取的起始地址
def parse(self, response): # 響應(yīng)對(duì)象response,從中解析出想要的數(shù)據(jù)
print('--------------',response)
print(response.text) # bs4解析
示例:
spiders目錄下的cnblogs.py文件文章來源地址http://www.zghlxwxcb.cn/news/detail-553413.html
import scrapy
class CnblogsSpider(scrapy.Spider):
name = 'cnblogs' # 爬蟲名
allowed_domains = ['www.cnblogs.com'] # 允許爬取的域
start_urls = ['http://www.cnblogs.com/'] # 爬取的起始地址
def parse(self, response): # 響應(yīng)對(duì)象response,從中解析出想要的數(shù)據(jù)
# print('--------------',response)
# print(response.text) # bs4解析
############ css ###########
# article_list=response.css('.post-item') # 查出所有類名為post-item的標(biāo)簽,取多條
# # print(len(article_list))
# for article in article_list:
# title=article.css('div.post-item-text>a::text').extract_first() # 取一條
# # article.css('section>div>a::text')
# href=article.css('div.post-item-text>a::attr(href)').extract_first()
# author=article.css('a.post-item-author>span::text').extract_first()
# desc=article.css('p.post-item-summary::text').extract_first()
#
# # 取不到就是None
# photo=article.css('p.post-item-summary>a>img::attr(src)').extract_first()
#
#
# print(title)
# print(href)
# print(author)
# print(desc)
# print(photo)
# print('----------')
########### xpath ###########
article_list = response.xpath('//*[@class="post-item"]') # 查出所有類名為post-item的標(biāo)簽,取多條
# print(len(article_list))
for article in article_list:
# 注意:使用 . 從當(dāng)前標(biāo)簽下找
title = article
到了這里,關(guān)于爬蟲框架scrapy基本原理的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!