Python爬虫之scrapy_splash组件的使用

12 篇文章 0 订阅
1 篇文章 1 订阅

1.什么是scrapy_splash?

scrapy_splash是scrapy的一个组件

  • scrapy-splash加载js数据是基于Splash来实现的。
  • Splash是一个Javascript渲染服务。它是一个实现了HTTP API的轻量级浏览器,Splash是用Python和Lua语言实现的,基于Twisted和QT等模块构建。
  • 使用scrapy-splash最终拿到的response相当于是在浏览器全部渲染完成以后的网页源代码。

splash官方文档
splash中文文档

2. scrapy_splash的作用

scrapy-splash能够模拟浏览器加载js,并返回js运行后的数据

3. scrapy_splash的环境安装

3.1 使用splash的docker镜像

注意 需要自行安装docker

3.1.1 获取splash镜像

在正确安装docker的基础上pull取splash的镜像

sudo docker pull scrapinghub/splash
3.1.2 启动splash
## 前台运行 
sudo docker run -p 8050:8050 scrapinghub/splash
## 后台运行 
sudo docker run -d -p 8050:8050 scrapinghub/splash
3.1.3 访问
http://127.0.0.1:8050 
3.2 在python虚拟环境中安装scrapy-splash包
pip install scrapy-splash

4. 在scrapy中使用splash

4.1 创建项目创建爬虫
scrapy startproject test_splash
cd test_splash
scrapy genspider no_splash baidu.com
scrapy genspider with_splash baidu.com
4.2 完善settings.py配置文件
# 在settings.py文件中添加splash的配置以及修改robots协议

# 渲染服务的url
SPLASH_URL = 'http://127.0.0.1:8050'
# 下载器中间件
DOWNLOADER_MIDDLEWARES = {
    'scrapy_splash.SplashCookiesMiddleware': 723,
    'scrapy_splash.SplashMiddleware': 725,
    'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware': 810,
}
# 去重过滤器
DUPEFILTER_CLASS = 'scrapy_splash.SplashAwareDupeFilter'
# 使用Splash的Http缓存
HTTPCACHE_STORAGE = 'scrapy_splash.SplashAwareFSCacheStorage'

# Obey robots.txt rules
ROBOTSTXT_OBEY = False
4.3 不使用splash
# spiders/no_splash.py
mport scrapy


class NoSplashSpider(scrapy.Spider):
    name = 'no_splash'
    allowed_domains = ['baidu.com']
    start_urls = ['https://www.baidu.com/s?wd=13161933309']

    def parse(self, response):
        with open('no_splash.html', 'w') as f:
            f.write(response.body.decode())

4.4 使用splash

# spiders/with_splash.py
import scrapy
from scrapy_splash import SplashRequest # 使用scrapy_splash包提供的request对象

class WithSplashSpider(scrapy.Spider):
    name = 'with_splash'
    allowed_domains = ['baidu.com']
    start_urls = ['https://www.baidu.com/s?wd=13161933309']

    def start_requests(self):
        yield SplashRequest(self.start_urls[0],
                            callback=self.parse_splash,
                            args={'wait': 10}, # 最大超时时间,单位:秒
                            endpoint='render.html') # 使用splash服务的固定参数

    def parse_splash(self, response):
        with open('with_splash.html', 'w') as f:
            f.write(response.body.decode())

5. python脚本

# 截取整个页面并以图片的形式返回

import requests

def splash_render(url):
    splash_url = "http://localhost:8050/render.png"

    args = {
        "url": url,
        "wait": 10,
        'render_all' : 1,
        # "proxy": "http://host:port"
    }

    response = requests.get(splash_url, params=args)
    response = requests.get(url)
    with open('92222.png', 'wb') as f:
        f.write(response.content)


if __name__ == '__main__':
    url = "https://blog.csdn.net/mouday/article/details/82843401"

    splash_render(url)
# 运行lua脚本 保存图片
import requests

splash = """
function main(splash, args)
  assert(splash:go('https://blog.csdn.net/weixin_45171937/article/details/129178729'))
  return splash:png{
    render_all=true
  }
  end
"""
url = 'http://localhost:8050/execute?lua_source=' + urllib.parse.quote(splash)
response = requests.get(url)
with open('123.png', 'wb') as f:
    f.write(response.content)

6. splash请求类型

6.1 render.html

返回一个经过Javascript渲染之后的页面的HTML代码

6.2 render.jpeg

将页面渲染的结果以图片的方式返回(格式为jpeg)

6.3 render.har

以HAR格式返回Splash与目标站点的交互信息,里面包含了请求信息、响应信息、时间信息和头信息等等

6.4 render.json

将经过JavaScript渲染的页面信息以json格式返回,它可以返回HTML,PNG等其他信息。返回何种信息由相关参数指定

6.5 execute

执行自定义的渲染脚本并返回对应的结果

6.6 run

这个端点与execute具有相同的功能,但是它会自动将 lua_source 包装在 function main(splash, args) … end 结构中

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

༒࿈十三༙྇࿈༒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值