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
相关文章
|
1天前
|
网络协议 安全 Python
我们将使用Python的内置库`http.server`来创建一个简单的Web服务器。虽然这个示例相对简单,但我们可以围绕它展开许多讨论,包括HTTP协议、网络编程、异常处理、多线程等。
我们将使用Python的内置库`http.server`来创建一个简单的Web服务器。虽然这个示例相对简单,但我们可以围绕它展开许多讨论,包括HTTP协议、网络编程、异常处理、多线程等。
5 0
|
1天前
|
算法 Python
我们需要一个简单的Python脚本来作为示例。假设我们有一个名为`hello_world.py`的脚本,
我们需要一个简单的Python脚本来作为示例。假设我们有一个名为`hello_world.py`的脚本,
7 0
|
1天前
|
Unix Shell Python
Python代码示例标准输出与标准错误输出
Python代码示例标准输出与标准错误输出
5 0
|
1天前
|
SQL Java C++
Python代码示例简单的print()函数使用
Python代码示例简单的print()函数使用
4 0
|
1天前
|
Unix Python
Python代码示例:使用`syslog`模块进行日志记录
Python代码示例:使用`syslog`模块进行日志记录
7 0
|
1天前
|
数据库 Python
我们来看一个简单的Python协程示例,它使用了`async`和`await`关键字。
我们来看一个简单的Python协程示例,它使用了`async`和`await`关键字。
5 0
|
1天前
|
数据采集 存储 移动开发
BeautifulSoup Python代码示例
BeautifulSoup Python代码示例
6 0
|
1天前
|
机器学习/深度学习 缓存 分布式计算
我们来看一个简单的Python代码示例,它使用`joblib`模块来并行执行一个函数:
我们来看一个简单的Python代码示例,它使用`joblib`模块来并行执行一个函数:
7 0
|
1天前
|
存储 算法 安全
我们来看一个简单的Python代码示例,它使用`hashlib`模块中的`md5()`和`sha256()`函数来计算字符串的哈希值。
我们来看一个简单的Python代码示例,它使用`hashlib`模块中的`md5()`和`sha256()`函数来计算字符串的哈希值。
9 0
|
1天前
|
存储 算法 安全
cryptography Python代码示例
cryptography Python代码示例
6 0