test_playwright.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # File : test_playwright.py
  4. # Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
  5. # Author's Blog: https://blog.csdn.net/qq_32394351
  6. # Date : 2024/3/24
  7. # https://github.com/microsoft/playwright-python
  8. _description = r"""
  9. Downloading Chromium 123.0.6312.4 (playwright build v1105) from https://playwright.azureedge.net/builds/chromium/1105/chromium-win64.zip
  10. 122.2 MiB [====================] 100% 0.0s
  11. Chromium 123.0.6312.4 (playwright build v1105) downloaded to C:\Users\hjd\AppData\Local\ms-playwright\chromium-1105
  12. Downloading FFMPEG playwright build v1009 from https://playwright.azureedge.net/builds/ffmpeg/1009/ffmpeg-win64.zip
  13. 1.4 MiB [====================] 100% 0.0s
  14. FFMPEG playwright build v1009 downloaded to C:\Users\hjd\AppData\Local\ms-playwright\ffmpeg-1009
  15. Downloading Firefox 123.0 (playwright build v1440) from https://playwright.azureedge.net/builds/firefox/1440/firefox-win64.zip
  16. 83.4 MiB [====================] 100% 0.0s
  17. Firefox 123.0 (playwright build v1440) downloaded to C:\Users\hjd\AppData\Local\ms-playwright\firefox-1440
  18. Downloading Webkit 17.4 (playwright build v1983) from https://playwright.azureedge.net/builds/webkit/1983/webkit-win64.zip
  19. 47.2 MiB [====================] 100% 0s
  20. Webkit 17.4 (playwright build v1983) downloaded to C:\Users\hjd\AppData\Local\ms-playwright\webkit-1983
  21. webkit-1983
  22. 只装这个浏览器,体积小|这个浏览器很多毛病。感觉和ie内核似的。访问个解析页面竟然崩了
  23. pip install playwright
  24. playwright install webkit
  25. playwright install firefox
  26. https://playwright.dev/python/docs/intro
  27. https://playwright.dev/python/docs/api/class-playwright
  28. """
  29. import time
  30. from playwright.sync_api import sync_playwright
  31. t1 = time.time()
  32. with sync_playwright() as p:
  33. # for browser_type in [p.chromium, p.firefox, p.webkit]:
  34. for browser_type in [p.webkit]:
  35. browser = browser_type.launch(headless=False)
  36. context = browser.new_context()
  37. # 类似于requests库,但是有点区别,比如取text和content,这个是函数需要加()执行。requests库是属性
  38. # context = browser.new_context(base_url="https://api.github.com")
  39. # _requests = context.request
  40. # r = _requests.get('https://www.baidu.com')
  41. # # print(r.text())
  42. # webview正常通过cloudfare5秒认证
  43. # page = browser.new_page()
  44. # page.goto('https://www.freeok.pro')
  45. # page.screenshot(path=f'screenshot-{browser_type.name}.png')
  46. # print(page.title())
  47. # print(page.content())
  48. # page.close()
  49. page = context.new_page()
  50. page.goto('https://jx.jsonplayer.com/player/?url=https://m.iqiyi.com/v_1pj3ayb1n70.html')
  51. # page.goto('https://jx.yangtu.top/?url=https://m.iqiyi.com/v_1pj3ayb1n70.html') # 这个会崩
  52. page.wait_for_selector('video')
  53. videoUrl = page.get_attribute('video', 'src')
  54. print('videoUrl:', videoUrl)
  55. # page.screenshot(path=f'screenshot3-{browser_type.name}.png')
  56. print(page.title())
  57. # print(page.content())
  58. page.close()
  59. # 下面代码卡死。csdn有检测
  60. # page.goto('https://blog.csdn.net/qq_47993287/article/details/123170108')
  61. # page.screenshot(path=f'screenshot2-{browser_type.name}.png')
  62. # print(page.title())
  63. browser.close()
  64. t2 = time.time()
  65. print(f'共计耗时{round((t2 - t1) * 1000, 2)}毫秒')