自动化脚本

25 年 5 月 19 日 星期一
444 字
3 分钟

爬虫

​ 好久没有写过博客了,最近有感觉,很多时候去钻一个点,会漏掉很多有趣的其他方面的东西,我相信这大概不是我愿意丢弃的,所以适当的脱离一下还是有好处的

​ 爬虫,作为一项很有趣的,满足自己各种xxx的技术,当然不能放过,刚好在知乎刷到了相关的内容,这周就来盘一盘(希望能顺利写完 5.11 19:43

puppeteer

​ 首先这个库和我这个乡巴佬所认知的爬虫就不太一样,这是个JavaScript库(啊已经不是python了吗),因此需要用到node.js,官方可以安装Node.js — 在任何地方运行 JavaScript

shell
node -v

应该输出版本号,验证node.js正确安装

  1. 选择一个文件夹,输入命令
shell
npm install puppeteer

然后就会自动生成node_modules,注意要在你的工作目录中做这步操作

  1. 然后你就可以开始玩puppeteer啦

    js
    const puppeteer = require('puppeteer')
    const browser = await puppeteer.launch()
    const page = await browser.newPage()
    
    // Navigate the page to a URL.
    await page.goto('https://developer.chrome.com/')
    
    // Set screen size.
    await page.setViewport({ width: 1080, height: 1024 })
    
    // Type into search box using accessible input name.
    await page.locator('aria/Search').fill('automate beyond recorder')
    
    // Wait and click on first result.
    await page.locator('.devsite-result-item-link').click()
    
    // Locate the full title with a unique string.
    const textSelector = await page.locator('text/Customize and automate').waitHandle()
    const fullTitle = await textSelector?.evaluate((el) => el.textContent)
    
    // Print the full title.
    console.log('The title of this blog post is "%s".', fullTitle)
    
    await browser.close()
    

    就按着语法去玩就好了

  2. 于是就可以浅显的实战了

    javascript
    const puppeteer = require('puppeteer')
    
    ;(async () => {
      const browser = await puppeteer.launch()
      const page = await browser.newPage()
      page.setViewport({ width: 1920, height: 1080 })
    
      // networkidle0 代表所有网络请求都完成了
      await page.goto('https:xxx', { waitUntil: 'networkidle0' })
    
      const category = await page.$('#category-0')
      const title = await page.evaluate((el) => {
        if (el) {
          return el.innerHTML
        }
      }, category)
    
      console.log(title)
    
      await page.screenshot({ path: 'xxx.png' })
      await browser.close()
    })()
    

    代码来自https://kirigaya.cn

    playwright

文章标题:自动化脚本

文章作者:io-wy

文章链接:https://io-wy.github.io/posts/%E7%88%AC%E8%99%AB[复制]

最后修改时间:


商业转载请联系站长获得授权,非商业转载请注明本文出处及文章链接,您可以自由地在任何媒体以任何形式复制和分发作品,也可以修改和创作,但是分发衍生作品时必须采用相同的许可协议。
本文采用CC BY-NC-SA 4.0进行许可。