Python爬蟲splash+requests簡單示例

      網友投稿 984 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

      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

      Python爬蟲:splash+requests簡單示例

      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小時內刪除侵權內容。

      上一篇:ModelArts平臺介紹
      下一篇:linux文件屬性的查看
      相關文章
      亚洲娇小性色xxxx| 2022中文字字幕久亚洲| 亚洲精品中文字幕无码蜜桃| 亚洲Av无码乱码在线播放| 亚洲日韩精品无码专区加勒比☆| 亚洲国产av一区二区三区丶| 亚洲白色白色在线播放| 亚洲系列国产精品制服丝袜第| 亚洲影院在线观看| 亚洲成在人天堂一区二区| 午夜影视日本亚洲欧洲精品一区| 亚洲中文字幕无码永久在线| 久久精品亚洲福利| 亚洲熟妇无码另类久久久| 亚洲日韩中文字幕在线播放| 亚洲乱码日产一区三区| 亚洲第一AV网站| 亚洲av无码国产精品夜色午夜| 亚洲av无码av制服另类专区| 亚洲精品免费在线观看| 亚洲宅男天堂a在线| 亚洲欧洲日产国码在线观看| 亚洲av无码片在线观看| 成人亚洲国产va天堂| 亚洲6080yy久久无码产自国产 | 亚洲va无码专区国产乱码| 亚洲AV无码专区国产乱码电影| 亚洲av永久无码精品古装片 | 久久精品国产亚洲AV麻豆不卡 | 蜜芽亚洲av无码精品色午夜| 亚洲高清免费在线观看| 亚洲乱码在线视频| 亚洲GV天堂无码男同在线观看| 国产AV无码专区亚洲AV麻豆丫| 亚洲性久久久影院| 亚洲爆乳无码专区| 亚洲卡一卡2卡三卡4麻豆| 亚洲精品一卡2卡3卡四卡乱码| 亚洲成av人在片观看| 久久精品国产精品亚洲精品| 亚洲人成在线电影|