scrapy-splash簡(jiǎn)單使用詳解
1.scrapy_splash是scrapy的一個(gè)組件
scrapy_splash加載js數(shù)據(jù)基于Splash來(lái)實(shí)現(xiàn)的
Splash是一個(gè)Javascrapy渲染服務(wù),它是一個(gè)實(shí)現(xiàn)HTTP API的輕量級(jí)瀏覽器,Splash是用Python和Lua語(yǔ)言實(shí)現(xiàn)的,基于Twisted和QT等模塊構(gòu)建
使用scrapy-splash最終拿到的response相當(dāng)于是在瀏覽器全部渲染完成以后的網(wǎng)頁(yè)源代碼
2.scrapy_splash的作用
scrpay_splash能夠模擬瀏覽器加載js,并返回js運(yùn)行后的數(shù)據(jù)
3.scrapy_splash的環(huán)境安裝
3.1 使用splash的docker鏡像
docker info 查看docker信息
docker images 查看所有鏡像
docker pull scrapinghub/splash 安裝scrapinghub/splash
docker run -p 8050:8050 scrapinghub/splash & 指定8050端口運(yùn)行
3.2.pip install scrapy-splash
3.3.scrapy 配置:
SPLASH_URL = 'http://localhost:8050' DOWNLOADER_MIDDLEWARES = { 'scrapy_splash.SplashCookiesMiddleware': 723, 'scrapy_splash.SplashMiddleware': 725, 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware': 810, } SPIDER_MIDDLEWARES = { 'scrapy_splash.SplashDeduplicateArgsMiddleware': 100, } DUPEFILTER_CLASS = 'scrapy_splash.SplashAwareDupeFilter' HTTPCACHE_STORAGE = 'scrapy_splash.SplashAwareFSCacheStorage'
3.4.scrapy 使用
from scrapy_splash import SplashRequest yield SplashRequest(self.start_urls[0], callback=self.parse, args={'wait': 0.5})
4.測(cè)試代碼:
import datetime import os import scrapy from scrapy_splash import SplashRequest from ..settings import LOG_DIR class SplashSpider(scrapy.Spider): name = 'splash' allowed_domains = ['biqugedu.com'] start_urls = ['http://www.biqugedu.com/0_25/'] custom_settings = { 'LOG_FILE': os.path.join(LOG_DIR, '%s_%s.log' % (name, datetime.date.today().strftime('%Y-%m-%d'))), 'LOG_LEVEL': 'INFO', 'CONCURRENT_REQUESTS': 8, 'AUTOTHROTTLE_ENABLED': True, 'AUTOTHROTTLE_TARGET_CONCURRENCY': 8, 'SPLASH_URL': 'http://localhost:8050', 'DOWNLOADER_MIDDLEWARES': { 'scrapy_splash.SplashCookiesMiddleware': 723, 'scrapy_splash.SplashMiddleware': 725, 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware': 810, }, 'SPIDER_MIDDLEWARES': { 'scrapy_splash.SplashDeduplicateArgsMiddleware': 100, }, 'DUPEFILTER_CLASS': 'scrapy_splash.SplashAwareDupeFilter', 'HTTPCACHE_STORAGE': 'scrapy_splash.SplashAwareFSCacheStorage', } def start_requests(self): yield SplashRequest(self.start_urls[0], callback=self.parse, args={'wait': 0.5}) def parse(self, response): """ :param response: :return: """ response_str = response.body.decode('utf-8', 'ignore') self.logger.info(response_str) self.logger.info(response_str.find('http://www.biqugedu.com/files/article/image/0/25/25s.jpg'))
scrapy-splash接收到j(luò)s請(qǐng)求:
到此這篇關(guān)于scrapy-splash簡(jiǎn)單使用詳解的文章就介紹到這了,更多相關(guān)scrapy-splash 使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python機(jī)器學(xué)習(xí)之隨機(jī)梯度下降法的實(shí)現(xiàn)
如果當(dāng)我們數(shù)據(jù)量和樣本量非常大時(shí),每一項(xiàng)都要參與到梯度下降,那么它的計(jì)算量時(shí)非常大的,所以我們需要采用隨機(jī)梯度下降法。本文介紹了Python實(shí)現(xiàn)隨機(jī)梯度下降法的方法,希望對(duì)大家有所幫助2023-02-02Idea安裝python顯示無(wú)SDK問(wèn)題解決方案
這篇文章主要介紹了Idea安裝python顯示無(wú)SDK問(wèn)題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08通過(guò)mod_python配置運(yùn)行在Apache上的Django框架
這篇文章主要介紹了通過(guò)mod_python配置運(yùn)行在Apache上的Django框架,Django是最具人氣的Python web開(kāi)發(fā)框架,需要的朋友可以參考下2015-07-07Python 實(shí)現(xiàn)遙感影像波段組合的示例代碼
這篇文章主要介紹了Python 實(shí)現(xiàn)遙感影像波段組合的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08python實(shí)現(xiàn)爬蟲(chóng)統(tǒng)計(jì)學(xué)校BBS男女比例(一)
這篇文章主要介紹了python實(shí)現(xiàn)爬蟲(chóng)統(tǒng)計(jì)學(xué)校BBS男女比例,,需要的朋友可以參考下2015-12-12python爬蟲(chóng) urllib模塊反爬蟲(chóng)機(jī)制UA詳解
這篇文章主要介紹了python爬蟲(chóng) urllib模塊反爬蟲(chóng)機(jī)制UA詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08PyTorch加載數(shù)據(jù)集梯度下降優(yōu)化
這篇文章主要介紹了PyTorch加載數(shù)據(jù)集梯度下降優(yōu)化,使用DataLoader方法,并繼承DataSet抽象類,可實(shí)現(xiàn)對(duì)數(shù)據(jù)集進(jìn)行mini_batch梯度下降優(yōu)化,需要的小伙伴可以參考一下2022-03-03詳解利用裝飾器擴(kuò)展Python計(jì)時(shí)器
在本文中,云朵君將和大家一起了解裝飾器的工作原理,如何將我們之前定義的定時(shí)器類?Timer?擴(kuò)展為裝飾器,以及如何簡(jiǎn)化計(jì)時(shí)功能,感興趣的可以了解一下2022-06-06