scrapy-splash简单使用详解
作者:zhu6201976-朱华龙 时间:2023-06-02 15:22:37
1.scrapy_splash是scrapy的一个组件
scrapy_splash加载js数据基于Splash来实现的
Splash是一个Javascrapy渲染服务,它是一个实现HTTP API的轻量级浏览器,Splash是用Python和Lua语言实现的,基于Twisted和QT等模块构建
使用scrapy-splash最终拿到的response相当于是在浏览器全部渲染完成以后的网页源代码
2.scrapy_splash的作用
scrpay_splash能够模拟浏览器加载js,并返回js运行后的数据
3.scrapy_splash的环境安装
3.1 使用splash的docker镜像
docker info 查看docker信息
docker images 查看所有镜像
docker pull scrapinghub/splash 安装scrapinghub/splash
docker run -p 8050:8050 scrapinghub/splash & 指定8050端口运行
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.测试代码:
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接收到js请求:
来源:https://blog.csdn.net/zhu6201976/article/details/106518029
标签:scrapy-splash,使用
0
投稿
猜你喜欢
vue实现简单的登录弹出框
2024-05-21 10:30:12
python_opencv用线段画封闭矩形的实例
2022-05-04 19:07:59
网站设计趋势: iPhones[译]
2009-09-21 12:40:00
Python光学仿真教程实现光线追踪
2023-08-12 00:27:39
Python3.10 Generator生成器Coroutine原生协程详解
2023-10-25 15:31:36
图解Python中浅拷贝copy()和深拷贝deepcopy()的区别
2021-12-09 21:09:39
mvn 打包报错:no compiler is provided in this environment
2023-10-31 00:47:17
解决Python下json.loads()中文字符出错的问题
2022-06-17 21:16:52
python3+PyQt5+Qt Designer实现堆叠窗口部件
2023-12-30 01:21:55
python安装pil库方法及代码
2021-10-22 16:55:24
深入理解Django的信号机制
2023-01-20 10:19:58
Python实现字典去除重复的方法示例
2021-02-24 21:52:14
快速入门python学习笔记
2023-08-25 14:26:29
python中的GUI实现计算器
2022-04-18 02:29:05
基于RSA算法在asp中加密与解密对应的函数
2007-11-07 21:39:00
python回调函数的使用方法
2023-05-28 02:50:55
MySQL基于GTID主从搭建
2024-01-23 18:17:56
Python的Flask框架中使用Flask-SQLAlchemy管理数据库的教程
2024-01-21 22:23:32
Django框架教程之正则表达式URL误区详解
2021-03-18 13:46:13
利用不同样式改变相同xhtml结构的布局
2008-08-20 18:17:00