Python爬虫:splash+requests简单示例

简介: Python爬虫:splash+requests简单示例

说明:

render是get方式

execute是post方式


render

import requests
def splash_render(url):
    splash_url = "http://localhost:8050/render.html"
    args = {
        "url": url,
        "timeout": 5,
        "image": 0,
        "proxy": "http://222.95.21.28:8888"
    }
    response = requests.get(splash_url, params=args)
    return response.text
if __name__ == '__main__':
    url = "http://quotes.toscrape.com/js/"
    html = splash_render(url)

args参数说明:

url: 需要渲染的页面地址

timeout: 超时时间

proxy:代理

wait:等待渲染时间

images: 是否下载,默认1(下载)

js_source: 渲染页面前执行的js代码


execute


import json
import requests
def splash_execute(url):
    splash_url = "http://localhost:8050/execute"
    script = """
    function main(splash)
        local url="{url}"
        splash:set_user_agent("Mozilla/5.0  Chrome/69.0.3497.100 Safari/537.36")
        splash:go(url)
        splash:wait(2)
        splash:go(url)
        return {
            html = splash:html()
        }
    end
    """
    script = script.replace("{url}", url)
    data = {
        "timeout": 5,
        "lua_source": script
    }
    response = requests.post(splash_url, json=data)
    return response.json().get("html")
if __name__ == '__main__':
    url = "http://quotes.toscrape.com/js/"
    html = splash_execute(url)

参数说明:

timeout 超时

lua_source lua脚本

proxy 代理


模拟登录

以下是lua脚本

splash提供的select选择器,使用方法和jQuery类似

function main(splash, args)
  -- jquery加载比较慢
  splash:autoload("https://code.jquery.com/jquery-3.3.1.min.js")
  splash:set_viewport_size(1366, 768)
  splash:set_user_agent("Mozilla/5.0  Chrome/69.0.3497.100 Safari/537.36")
  -- 从首页点击登录按钮
  splash:go(splash.args.url)
  splash:wait(3)
  splash:runjs("$('#login').click()")
  splash:wait(2)
  -- 登录页输入账号密码,并提交
  splash:select("#username"):send_text("username")
  splash:select("#password"):send_text("password")
  splash:wait(5)
  -- 可以使用splash自带的鼠标点击,并指定点击位置
  local button = splash:select("#button")
    local bounds = button:bounds()
    button:mouse_click{x=bounds.width/3, y=bounds.height/3}
  splash:wait(2)
  -- 返回
  return {
  html=splash:html(),
  png = splash:png(),
  cookie=splash:get_cookies()
  }
end
相关文章
|
2月前
|
数据采集 Web App开发 数据安全/隐私保护
实战:Python爬虫如何模拟登录与维持会话状态
实战:Python爬虫如何模拟登录与维持会话状态
|
3月前
|
数据采集 Web App开发 自然语言处理
新闻热点一目了然:Python爬虫数据可视化
新闻热点一目了然:Python爬虫数据可视化
|
2月前
|
数据采集 监控 数据库
Python异步编程实战:爬虫案例
🌟 蒋星熠Jaxonic,代码为舟的星际旅人。从回调地狱到async/await协程天堂,亲历Python异步编程演进。分享高性能爬虫、数据库异步操作、限流监控等实战经验,助你驾驭并发,在二进制星河中谱写极客诗篇。
Python异步编程实战:爬虫案例
|
3月前
|
数据采集 存储 XML
Python爬虫技术:从基础到实战的完整教程
最后强调: 父母法律法规限制下进行网络抓取活动; 不得侵犯他人版权隐私利益; 同时也要注意个人安全防止泄露敏感信息.
745 19
|
2月前
|
数据采集 存储 JSON
Python爬虫常见陷阱:Ajax动态生成内容的URL去重与数据拼接
Python爬虫常见陷阱:Ajax动态生成内容的URL去重与数据拼接
|
2月前
|
数据采集 存储 JavaScript
解析Python爬虫中的Cookies和Session管理
Cookies与Session是Python爬虫中实现状态保持的核心。Cookies由服务器发送、客户端存储,用于标识用户;Session则通过唯一ID在服务端记录会话信息。二者协同实现登录模拟与数据持久化。
|
3月前
|
数据采集 Web App开发 前端开发
处理动态Token:Python爬虫应对AJAX授权请求的策略
处理动态Token:Python爬虫应对AJAX授权请求的策略
|
8月前
|
数据采集 测试技术 C++
无headers爬虫 vs 带headers爬虫:Python性能对比
无headers爬虫 vs 带headers爬虫:Python性能对比
|
数据采集 存储 JSON
Python网络爬虫:Scrapy框架的实战应用与技巧分享
【10月更文挑战第27天】本文介绍了Python网络爬虫Scrapy框架的实战应用与技巧。首先讲解了如何创建Scrapy项目、定义爬虫、处理JSON响应、设置User-Agent和代理,以及存储爬取的数据。通过具体示例,帮助读者掌握Scrapy的核心功能和使用方法,提升数据采集效率。
558 6
|
8月前
|
数据采集 存储 监控
Python 原生爬虫教程:网络爬虫的基本概念和认知
网络爬虫是一种自动抓取互联网信息的程序,广泛应用于搜索引擎、数据采集、新闻聚合和价格监控等领域。其工作流程包括 URL 调度、HTTP 请求、页面下载、解析、数据存储及新 URL 发现。Python 因其丰富的库(如 requests、BeautifulSoup、Scrapy)和简洁语法成为爬虫开发的首选语言。然而,在使用爬虫时需注意法律与道德问题,例如遵守 robots.txt 规则、控制请求频率以及合法使用数据,以确保爬虫技术健康有序发展。
1170 31

推荐镜像

更多