Python爬虫splash+requests简单示例

网友投稿 734 2022-05-30

说明:

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)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

args参数说明:

url: 需要渲染的页面地址

timeout: 超时时间

proxy:代理

wait:等待渲染时间

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

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

execute

Python爬虫:splash+requests简单示例

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)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

参数说明:

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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

参考:

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

Python

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:部署ZbxTable
下一篇:关于Spring Boot你不可不知道的实情
相关文章