这篇文章主要介绍了如何使用scrapy-splash,编程网小编觉得不错,现在分享给大家,也给大家做个参考,一起跟随编程网小编来看看吧!1.scrapy_splash是scrapy的一个组件scrapy_splash加载js数据基于Spla
这篇文章主要介绍了如何使用scrapy-splash,编程网小编觉得不错,现在分享给大家,也给大家做个参考,一起跟随编程网小编来看看吧!
scrapy_splash加载js数据基于Splash来实现的
Splash是一个Javascrapy渲染服务,它是一个实现Http api的轻量级浏览器,Splash是用python和lua语言实现的,基于Twisted和Qt等模块构建
使用scrapy-splash最终拿到的response相当于是在浏览器全部渲染完成以后的网页源代码
scrpay_splash能够模拟浏览器加载js,并返回js运行后的数据
1 使用splash的Docker镜像
docker info 查看docker信息
docker images 查看所有镜像
docker pull scrapinghub/splash 安装scrapinghub/splash
docker run -p 8050:8050 scrapinghub/splash & 指定8050端口运行
2.pip install scrapy-splash
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'
scrapy 使用
from scrapy_splash import SplashRequestyield SplashRequest(self.start_urls[0], callback=self.parse, args={'wait': 0.5})
测试代码:
import datetimeimport os import scrapyfrom 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接收到js请求:
以上就是编程网小编为大家收集整理的如何使用scrapy-splash,如何觉得编程网网站的内容还不错,欢迎将编程网网站推荐给身边好友。
--结束END--
本文标题: 如何使用scrapy-splash
本文链接: https://lsjlt.com/news/246641.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0