之前已经实现了:
【已解决】Mac中用Selenium自动操作浏览器实现百度搜索
和:
【已解决】Mac中用puppeteer自动操作浏览器实现百度搜索
现在接着去实现:试试playwright的效果。
先去简单研究看看
playwright
- Node.js library to automate Chromium, Firefox and WebKit with a single API
微软开发的
node.js的库
用于自动化操作浏览器:Chromium,Firefox,WebKit
最大特点:绿色环保ever-green, 能力强capable, 可靠性高reliable,速度快fast

支持无头Headless模式
对应有人实现了python版本
Python version of the Playwright testing and automation library.
原来就是微软自己
那就去按照教程去:
【已解决】Mac中安装Python版Playwright和初始化开发环境
然后接着想办法实现自己要的功能:
模拟百度搜索自动化
其中先去:
【已解决】用Python的Playwright定位并点击百度搜索输入框
再去:
【已解决】用Python的Playwright给百度搜索输入框中输入文字
再去:
【已解决】用Python的Playwright触发百度首页的搜索
接着去:
【已解决】Python的Playwright去解析提取百度搜索的结果
【总结】
最后代码:
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | # Function: Playwright demo baidu search # Author: Crifan Li # Update: 20210331 from playwright.sync_api import sync_playwright # here use sync mode with sync_playwright() as p: chromiumBrowserType = p.chromium print ( "chromiumBrowserType=%s" % chromiumBrowserType) browser = chromiumBrowserType.launch(headless = False ) # chromiumBrowserType=<BrowserType name=chromium executable_path=/Users/xxx/Library/Caches/ms-playwright/chromium-857950/chrome-mac/Chromium.app/Contents/MacOS/Chromium> print ( "browser=%s" % browser) # browser=<Browser type=<BrowserType name=chromium executable_path=/Users/xxx/Library/Caches/ms-playwright/chromium-857950/chrome-mac/Chromium.app/Contents/MacOS/Chromium> version=90.0.4430.0> page = browser.new_page() print ( "page=%s" % page) # page=<Page url='about:blank'> ################################################################################ # Open url ################################################################################ print ( "page=%s" % page) # page=<Page url='https://www.baidu.com/'> ################################################################################ # Input text ################################################################################ searchStr = "crifan" SearchInputSelector = "input#kw.s_ipt" # page.click(SearchInputSelector) page.fill(SearchInputSelector, searchStr) ################################################################################ # Trigger search ################################################################################ EnterKey = "Enter" # Method 1: press Enter key # page.keyboard.press(EnterKey) # Method 2: locate element then click SearchButtonSelector = "input#su" page.press(SearchButtonSelector, EnterKey) # wait -> makesure element visible SearchFoundWordsSelector = 'span.nums_text' # SearchFoundWordsXpath = "//span[@class='nums_text']" page.wait_for_selector(SearchFoundWordsSelector, state = "visible" ) ################################################################################ # Extract content ################################################################################ resultASelector = "h3[class^='t'] a" searchResultAList = page.query_selector_all(resultASelector) print ( "searchResultAList=%s" % searchResultAList) # searchResultAList=[<JSHandle preview=JSHandle@<a target="_blank" href="http://www.baidu.com/link?…>在路上on the way - 走别人没走过的路,让别人有路可走</a>>, <JSHandle preview=JSHandle@node>, 。。。, <JSHandle preview=JSHandle@node>] searchResultANum = len (searchResultAList) print ( "Found %s search result:" % searchResultANum) for curIdx, aElem in enumerate (searchResultAList): curNum = curIdx + 1 print ( "%s [%d] %s" % ( "-" * 20 , curNum, "-" * 20 )) title = aElem.text_content() print ( "title=%s" % title) # title=在路上on the way - 走别人没走过的路,让别人有路可走 baiduLinkUrl = aElem.get_attribute( "href" ) print ( "baiduLinkUrl=%s" % baiduLinkUrl) # baiduLinkUrl=http://www.baidu.com/link?url=fB3F0xZmwig9r2M_1pK4BJG00xFPLjJ85X39GheP_fzEA_zJIjX-IleEH_ZL8pfo # do sceeenshot screenshotFilename = 'baidu_search_%s_result.png' % searchStr page.screenshot(path = screenshotFilename) browser.close() |
输出:
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 35 36 | chromiumBrowserType = <BrowserType name = chromium executable_path = / Users / xxx / Library / Caches / ms - playwright / chromium - 857950 / chrome - mac / Chromium.app / Contents / MacOS / Chromium> browser = <Browser type = <BrowserType name = chromium executable_path = / Users / xxx / Library / Caches / ms - playwright / chromium - 857950 / chrome - mac / Chromium.app / Contents / MacOS / Chromium> version = 90.0 . 4430.0 > page = <Page url = 'about:blank' > searchResultAList = [<JSHandle preview = JSHandle@<a target = "_blank" href = "http: / / www.baidu.com / link?…>在路上on the way - 走别人没走过的路,让别人有路可走< / a>>, <JSHandle preview = JSHandle@node>, <JSHandle preview = JSHandle@node>, <JSHandle preview = JSHandle@node>, <JSHandle preview = JSHandle@node>, <JSHandle preview = JSHandle@node>, <JSHandle preview = JSHandle@node>, <JSHandle preview = JSHandle@node>, <JSHandle preview = JSHandle@node>, <JSHandle preview = JSHandle@node>] Found 10 search result: - - - - - - - - - - - - - - - - - - - - [ 1 ] - - - - - - - - - - - - - - - - - - - - title = 在路上on the way - 走别人没走过的路,让别人有路可走 baiduLinkUrl = http: / / www.baidu.com / link?url = fB3F0xZmwig9r2M_1pK4BJG00xFPLjJ85X39GheP_fzEA_zJIjX - IleEH_ZL8pfo - - - - - - - - - - - - - - - - - - - - [ 2 ] - - - - - - - - - - - - - - - - - - - - title = crifan – 在路上 baiduLinkUrl = http: / / www.baidu.com / link?url = kmvgD1PraoULnnjUvNPQmwHFQ9uUKkXg_HWy0NI3xI11cV7evpdxyA_4FkVf3zLH - - - - - - - - - - - - - - - - - - - - [ 3 ] - - - - - - - - - - - - - - - - - - - - title = crifan简介_crifan的专栏 - CSDN博客_crifan baiduLinkUrl = http: / / www.baidu.com / link?url = CHLWAQKOMgb23GmzVCZRIVze9kBNu6DIVoSWQqe21bWq_qZk2zDu_V3pDC1o1i5WC8qXAbUhaBIN8UO9Sjzxfa - - - - - - - - - - - - - - - - - - - - [ 4 ] - - - - - - - - - - - - - - - - - - - - title = crifan的微博_微博 baiduLinkUrl = http: / / www.baidu.com / link?url = - QwlZ5SEmZD1R2QqdsK7ByUhxmIdX_hiFCX79hg9RTbQ11j5wXaBaYXegXU9WDk3 - - - - - - - - - - - - - - - - - - - - [ 5 ] - - - - - - - - - - - - - - - - - - - - title = Crifan的电子书大全 | crifan.github.io baiduLinkUrl = http: / / www.baidu.com / link?url = Sgrbyd_pBsm - BTANKwSmyveSWvWj2_IqOOZzYw - SkG8tQ_C6Ccz88zZxHf3Eh1JA - - - - - - - - - - - - - - - - - - - - [ 6 ] - - - - - - - - - - - - - - - - - - - - title = GitHub - crifan / crifanLib: crifan's library baiduLinkUrl = http: / / www.baidu.com / link?url = NSZ5IzQ2Qag3CpGLMAbJer3QaAqI7qZOp2Ythiw6o8inoDX - 5LqlzOKWTrMzQK5G - - - - - - - - - - - - - - - - - - - - [ 7 ] - - - - - - - - - - - - - - - - - - - - title = 在路上www.crifan.com - 网站排行榜 baiduLinkUrl = http: / / www.baidu.com / link?url = Tc4cbETNKpQXj - kX1pwSOcPG8l9ijRRPqokRSMHgB59rSn6GoWSBzCPu3ky3dN6Cu1pb - 4HBZ2_YaVyS7qdDS_ - - - - - - - - - - - - - - - - - - - - [ 8 ] - - - - - - - - - - - - - - - - - - - - title = crifan的专栏_crifan_CSDN博客 - crifan领域博主 baiduLinkUrl = http: / / www.baidu.com / link?url = OLkrWu8q9SRZuBN - KzEMO56f82IpIfvbOp - sU3jdjbVBPP3GXBw_8StJgYG - _QrK - - - - - - - - - - - - - - - - - - - - [ 9 ] - - - - - - - - - - - - - - - - - - - - title = User crifan - Stack Overflow baiduLinkUrl = http: / / www.baidu.com / link?url = t1rc0EGg33A - uJUiZHKkUWA8ETf6B5P8pBKo0yNCH - VTWluW3xqUlYRHjMz8bQdiN2mJROMhfkX6bY0db_bB_a - - - - - - - - - - - - - - - - - - - - [ 10 ] - - - - - - - - - - - - - - - - - - - - title = crifan - Bing 词典 baiduLinkUrl = http: / / www.baidu.com / link?url = 8z - 3hYeLAQ8T4efOf4848LtAdpGdR1Ect9au4JIUB32bm2z412RDsMelFW1R2aIk |
效果:

附录:
相关资料:
- Node.JS版
- GitHub
- 官网
- Fast and reliable end-to-end testing for modern web apps | Playwright
- 英文
- 中文
- Getting Started | Playwright 中文文档 | Playwright 中文网 (bootcss.com)
- Element selectors | Playwright 中文文档 | Playwright 中文网
- Python版
转载请注明:在路上 » 【已解决】Mac中用playwright自动操作浏览器实现百度搜索