Splash的爬虫应用(二)

简介: Splash的爬虫应用(二)

接上文 Splash的爬虫应用(一)https://developer.aliyun.com/article/1617947

Splash中的HTTP API
Splash提供了API接口,可以实现Python与Splash之间的交互。Splash比较常用的API接口及使用方法如下:

1、render.html
通过该接口可以实现获取JavaScript渲染后的HTML代码,接口的请求地址如下:

http://localhost:8050/render.html

代码如下:

#_*_coding:utf-8_*_
# 作者      :liuxiaowei
# 创建时间   :2/10/22 9:57 PM
# 文件      :使用render.html接口获取百度首页图片链接.py
# IDE      :PyCharm

# 导入网路请求模块
import requests
# 导入HTML解析模块
from bs4 import BeautifulSoup
# Splash 的 render.html接口地址
splash_url = 'http://localhost:8050/render.html'

# 需要爬取的页面地址
args = {
   'url': 'https://www.baidu.com'}

# 使用render.html接口对百度首页发送网络请求
resp = requests.get(splash_url, args)
# 不使用render.html接口对百度首页发送网络请求
#resp = requests.get('https://www.baidu.com/')
# 设置编码方式
resp.encoding = 'utf-8'
# 创建解析HTML代码BeautifulSoup对象
soup = BeautifulSoup(resp.text, 'html.parser')

# 获取百度首页Logo图片的链接
img_url = 'https:' + soup.select('div[class="s-p-top"]')[0].select('img')[0].attrs['src']

# 打印链接地址
print(img_url)

程序运行结果如下:

https://www.baidu.com/img/PC_880906d2a4ad95f5fafb2e540c5cdad7.png

Process finished with exit code 0

如果不使用render.html接口直接访问百度首页,将出现报错信息,因为百度首页中Logo图片的链接地址是渲染后的结果,所以在没经过Splash渲染的情况下是不能直接从HTML代码中提取出来的。错误信息如下:

Traceback (most recent call last):
File "/Users/liuxiaowei/PycharmProjects/爬虫练习/明日科技/爬取动态渲染的信息/搭建和运行Splash环境/使用render.html接口获取百度首页图片链接.py", line 26, in

img_url = 'https:' + soup.select('div[class="s-p-top"]')[0].select('img')[0].attrs['src']
IndexError: list index out of range

Process finished with exit code 1

在使用render.html接口时,除了可以使用简单的url参数以外,还有多种参数可以应用,比较常用的参数及含义如表所示:

render.html 接口常用参数含义及描述

参 数 名

描 述

timeout

设置渲染页面超时的时间

proxy

设置代理服务的地址

wait

设置页面加载后等待更新的时间

images

设置是否下载图片,默认值为1表示下载图片,值为0时表示不下载图片

js_source

设置用户自定义的JavaScript代码,在页面渲染前执行

说 明

关于Splash API接口中的其他参数可以参考官方文档,地址如下:

https://splash.readthedocs.io/en/stable/api.html

2、render.png
通过该接口可以实现获取目标网页的截图,接口的请求地址如下:

http://localhost:8050/render.png

render.png接口比render.html接口多了两个比较重要的参数,分别为“width“与”height“, 使用这两个参数即可指定目标网页截图的宽度与高度,以获取百度首页截图为例。

示例代码如下:

#_*_coding:utf-8_*_
# 作者      :liuxiaowei
# 创建时间   :2/10/22 10:51 PM
# 文件      :使用render.png接口获取百度首页截图.py
# IDE      :PyCharm

# 导入网络请求模块
import requests

# Splash的render.png接口地址
splash_url = 'http://localhost:8050/render.png'
# 需要爬取的网页地址
args = {
   'url':'https://www.baidu.com/', 'width':1280, 'height':800}

# 发送网络请求
resp = requests.get(splash_url, args)
# 调用open()函数
with open('baidu.png', 'wb') as f:
    # 将返回的二进制数据保存成图片
    f.write(resp.content)

程序运行结果在当前目录下将自动生成名为"baidu.png"的图片文件,打开文件的效果如下:

image.png

说 明

Splash还提供了一个render.jpeg接口,该接口与render.png类似,只不过返回的是JPEG格式的二进制数据。

3、render.json
通过该接口可以实现获取JavaScript渲染网页信息的JSON,根据传递的参数,它可以包含HTML、PNG和其他信息。接口的请求地址如下:

http://localhost:8050/render.json

在默认情况下使用render.json接口,将返回请求地址、页面标题、页面尺寸的JSON信息。代码如下:

#_*_coding:utf-8_*_
# 作者      :liuxiaowei
# 创建时间   :2/10/22 11:11 PM
# 文件      :获取请求页面的JSON信息.py
# IDE      :PyCharm

# 导入网络请求模块
import requests
# Splash的render.json接口地址
splash_url = 'http://localhost:8050/render.json'

# 需要爬取的网页地址
args = {
   'url':'https://www.baidu.com/'}
#发送网络请求
resp = requests.get(splash_url, args)

# 打印返回的JSON信息
print(resp.json())

程序运行结果:

{
   'url': 'https://www.baidu.com/', 'requestedUrl': 'https://www.baidu.com/', 'geometry': [0, 0, 1024, 768], 'title': '百度一下,你就知道'}

Process finished with exit code 0

4、执行Lua自定义脚本
Splash还提供了一个非常强大的execute接口,该接口可以实现在Python代码中执行Lua脚本。使用该接口就必须指定lua_source参数,该参数表示需要执行的Lua脚本,Splash执行完成以后将结果返回Python。以获取百度首页渲染后的HMTL代码为例,示例代码如下:

#_*_coding:utf-8_*_
# 作者      :liuxiaowei
# 创建时间   :2/10/22 11:22 PM
# 文件      :获取百度渲染后的HTML代码.py
# IDE      :PyCharm

# 导入网络请求模块
import requests
# 导入quote()方法
from urllib.parse import quote

# 自定义的Lua脚本
lua_script = '''
function main(splash)
    splash:go("https://www.baidu.com/")
    splash:wait(0.5)
    return splash:html()
end
'''
# Splash的execute接口地址
splash_url = 'http://localhost:8050/execute?lua_source='+quote(lua_script)

# 定义headers信息
headers = {
   
  "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36"
}
# 发送网络请求
resp = requests.get(splash_url, headers=headers)

# 打印渲染后的HMTL代码
print(resp.text)

程序运行结果如下:

<!DOCTYPE html><!--STATUS OK--><html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><meta content="always" name="referrer"><meta name="theme-color" content="#ffffff"><meta name="description" content="全球领先的中文搜索引擎、致力于让网民更便捷地获取信息,找到所求。百度超过千亿的中文网页数据库,可以瞬间找到相关的搜索结果。"><link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"><link rel="search" type="application/opensearchdescription+xml" href="/content-search.xml" title="百度搜索"><link rel="icon" sizes="any" mask="" href="//www.baidu.com/img/baidu_85beaf5496f291521eb75ba38eacbd87.svg"><link rel="dns-prefetch" href="//dss0.bdstatic.com"><link rel="dns-prefetch" href="//dss1.bdstatic.com"><link rel="dns-prefetch" href="//ss1.bdstatic.com"><link rel="dns-prefetch" href="//sp0.baidu.com"><link rel="dns-prefetch" href="//sp1.baidu.com"><link rel="dns-prefetch" href="//sp2.baidu.com"><title>百度一下,你就知道</title><style index="newi" type="text/css">#form .bdsug{
   top:39px}.bdsug{
   display:none;position:absolute;width:535px;background:#fff;border:1px solid #ccc!important;_overflow:hidden;box-shadow:1px 1px 3px #ededed;-webkit-box-shadow:1px 1px 3px #ededed;-moz-box-shadow:1px 1px 3px #ededed;-o-box-shadow:1px 1px 3px #ededed}.bdsug li{
   width:519px;color:#000;font:14px arial;line-height:25px;padding:0 8px;position:relative;cursor:default}.bdsug li.bdsug-s{
   background:#f0f0f0}.bdsug-store span,.bdsug-store b{
   color:#7A77C8}.bdsug-store-del{
   font-size:12px;color:#666;text-decoration:underline;position:absolute;right:8px;top:0;cursor:pointer;display:none}.bdsug-s .bdsug-store-del{
   display:inline-block}.bdsug-ala{
   display:inline-block;border-bottom:1px solid #e6e6e6}.bdsug-ala h3{
   line-height:14px;background:url(//www.baidu.com/img/sug_bd.png?v=09816787.png)

在Splash中使用Lua脚本可以执行一系列的渲染操作,这样便可以通过Splash模拟浏览器实现网页数据的提取工作。Lua脚本中的语法是比较简单的,可以通过“splash:“的方式调用其内部的方法和属性,“function main(splash)“表示脚本入口;“splash:go(“https://www.baidu.com/”)表示调用go()方法访问百度首页(网址);代码“splash:wait(0.5)“表示等待0.5秒;“return splash:html()“表示返回渲染后的HTML代码;”end“表示脚本结束。

Lua脚本的常用属性与方法:

Lua脚本常用的属性与方法含义
image.png
image.png
说 明

由于Lua脚本中的属性与方法较多,如果感兴趣的朋友需要了解更多相关资料可以参考官网API文档,网址:

https://splash.readthedocs.io/en/stable/scripting-ref.html

总 结

Splash的爬虫应用

搭建Splash环境
Splash中的HTTP API
§ 获取JavaScript渲染后的HTML代码
§ 获取目标网页的截图
§ 获取javascript渲染网页信息的JSON
执行Lua自定义脚本

相关文章
|
8月前
|
数据采集 Java API
深度解析:爬虫技术获取淘宝商品详情并封装为API的全流程应用
本文探讨了如何利用爬虫技术获取淘宝商品详情并封装为API。首先介绍了爬虫的核心原理与工具,包括Python的Requests、BeautifulSoup和Scrapy等库。接着通过实战案例展示了如何分析淘宝商品页面结构、编写爬虫代码以及突破反爬虫策略。随后讲解了如何使用Flask框架将数据封装为API,并部署到服务器供外部访问。最后强调了在开发过程中需遵守法律与道德规范,确保数据使用的合法性和正当性。
|
6月前
|
数据采集 存储 数据可视化
Python网络爬虫在环境保护中的应用:污染源监测数据抓取与分析
在环保领域,数据是决策基础,但分散在多个平台,获取困难。Python网络爬虫技术灵活高效,可自动化抓取空气质量、水质、污染源等数据,实现多平台整合、实时更新、结构化存储与异常预警。本文详解爬虫实战应用,涵盖技术选型、代码实现、反爬策略与数据分析,助力环保数据高效利用。
370 0
|
7月前
|
数据采集 API 调度
Python爬虫框架对比:Scrapy vs Requests在API调用中的应用
本文对比了 Python 中 Scrapy 与 Requests 两大爬虫框架在 API 调用中的差异,涵盖架构设计、调用模式、性能优化及适用场景,并提供实战建议,助力开发者根据项目需求选择合适工具。
|
数据采集 存储 JSON
Python网络爬虫:Scrapy框架的实战应用与技巧分享
【10月更文挑战第27天】本文介绍了Python网络爬虫Scrapy框架的实战应用与技巧。首先讲解了如何创建Scrapy项目、定义爬虫、处理JSON响应、设置User-Agent和代理,以及存储爬取的数据。通过具体示例,帮助读者掌握Scrapy的核心功能和使用方法,提升数据采集效率。
607 6
|
10月前
|
数据采集 XML 存储
Headers池技术在Python爬虫反反爬中的应用
Headers池技术在Python爬虫反反爬中的应用
|
数据采集 JavaScript 前端开发
异步请求在TypeScript网络爬虫中的应用
异步请求在TypeScript网络爬虫中的应用
|
数据采集 前端开发 中间件
Python网络爬虫:Scrapy框架的实战应用与技巧分享
【10月更文挑战第26天】Python是一种强大的编程语言,在数据抓取和网络爬虫领域应用广泛。Scrapy作为高效灵活的爬虫框架,为开发者提供了强大的工具集。本文通过实战案例,详细解析Scrapy框架的应用与技巧,并附上示例代码。文章介绍了Scrapy的基本概念、创建项目、编写简单爬虫、高级特性和技巧等内容。
638 4
|
数据采集 中间件 API
在Scrapy爬虫中应用Crawlera进行反爬虫策略
在Scrapy爬虫中应用Crawlera进行反爬虫策略
|
数据采集 JavaScript API
Splash的爬虫应用(一)
Splash的爬虫应用(一)
329 0
|
10月前
|
数据采集 测试技术 C++
无headers爬虫 vs 带headers爬虫:Python性能对比
无headers爬虫 vs 带headers爬虫:Python性能对比