scrapy-splash的介紹
在
前面的部落格中,我們已經見識到了Scrapy的強大之處。但是,Scrapy也有其不足之處,即Scrapy沒有JS engine, 是以它無法爬取JavaScript生成的動态網頁,隻能爬取靜态網頁,而在現代的網絡世界中,大部分網頁都會采用JavaScript來豐富網頁的功能。是以,這無疑Scrapy的遺憾之處。
那麼,我們還能愉快地使用Scrapy來爬取動态網頁嗎?有沒有什麼補充的辦法呢?答案依然是yes!答案就是,使用
scrapy-splash子產品!
scrapy-splash子產品主要使用了
Splash. 所謂的Splash, 就是一個Javascript渲染服務。它是一個實作了HTTP API的輕量級浏覽器,Splash是用Python實作的,同時使用Twisted和QT。Twisted(QT)用來讓服務具有異步處理能力,以發揮webkit的并發能力。Splash的特點如下:
- 并行處理多個網頁
- 得到HTML結果以及(或者)渲染成圖檔
- 關掉加載圖檔或使用 Adblock Plus規則使得渲染速度更快
- 使用JavaScript處理網頁内容
- 使用Lua腳本
- 能在Splash-Jupyter Notebooks中開發Splash Lua scripts
- 能夠獲得具體的HAR格式的渲染資訊
scrapy-splash的安裝
由于Splash的上述特點,使得Splash和Scrapy兩者的相容性較好,抓取效率較高。
聽了上面的介紹,有沒有對scrapy-splash很心動呢?下面就介紹如何安裝scrapy-splash,步驟如下:
1. 安裝
pip3 install scrapy-splash
2. scrapy-splash使用的是Splash HTTP API, 是以需要一個splash instance,一般采用
docker運作splash,是以需要安裝docker。不同系統的安裝指令會不同,如筆者的CentOS7系統的安裝方式為:
sudo yum install docker
安裝完docker後,可以輸入指令‘docker -v’來驗證docker是否安裝成功。
3. 開啟docker服務,拉取splash鏡像(pull the image):
sudo service docker start
sudo dock pull scrapinghub/splash
運作結果如下:
4. 開啟容器(start the container):
sudo docker run -p 8050:8050 scrapinghub/splash
此時Splash以運作在本地伺服器的端口8050(http).在浏覽器中輸入’localhost:8050’, 頁面如下:
在這個網頁中我們能夠運作Lua scripts,這對我們在scrapy-splash中使用Lua scripts是非常有幫助的。以上就是我們安裝scrapy-splash的全部。
scrapy-splash的執行個體
在安裝完scrapy-splash之後,不趁機介紹一個執行個體,實在是說不過去的,我們将在此介紹一個簡單的執行個體,那就是利用百度查詢手機号碼資訊。比如,我們在百度輸入框中輸入手機号碼‘159********’,然後查詢,得到如下資訊:
我們将利用scrapy-splash模拟以上操作并擷取手機号碼資訊。
1. 建立scrapy項目phone
2. 配置settings.py檔案,配置的内容如下:
ROBOTSTXT_OBEY = False
SPIDER_MIDDLEWARES = {
'scrapy_splash.SplashDeduplicateArgsMiddleware': 100,
}
DOWNLOADER_MIDDLEWARES = {
'scrapy_splash.SplashCookiesMiddleware': 723,
'scrapy_splash.SplashMiddleware': 725,
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware': 810
}
SPLASH_URL = 'http://localhost:8050'
DUPEFILTER_CLASS = 'scrapy_splash.SplashAwareDupeFilter'
HTTPCACHE_STORAGE = 'scrapy_splash.SplashAwareFSCacheStorage'
具體的配置說明可以參考:
https://pypi.python.org/pypi/scrapy-splash.
3. 建立爬蟲檔案phoneSpider.py, 代碼如下:
# -*- coding: utf-8 -*-
from scrapy import Spider, Request
from scrapy_splash import SplashRequest
# splash lua script
script = """
function main(splash, args)
assert(splash:go(args.url))
assert(splash:wait(args.wait))
js = string.format("document.querySelector('#kw').value=%s;document.querySelector('#su').click()", args.phone)
splash:evaljs(js)
assert(splash:wait(args.wait))
return splash:html()
end
"""
class phoneSpider(Spider):
name = 'phone'
allowed_domains = ['www.baidu.com']
url = 'https://www.baidu.com'
# start request
def start_requests(self):
yield SplashRequest(self.url, callback=self.parse, endpoint='execute', args={'lua_source': script, 'phone':'159*******', 'wait': 5})
# parse the html content
def parse(self, response):
info = response.css('div.op_mobilephone_r.c-gap-bottom-small').xpath('span/text()').extract()
print('='*40)
print(''.join(info))
print('='*40)
4. 運作爬蟲,scrapy crawl phone, 結果如下:
執行個體展示到此結束,歡迎大家通路這個項目的Github位址:
https://github.com/percent4/phoneSpider.當然,有什麼問題,也可以載下面留言評論哦~~