发布于 2026-01-06 2 阅读
0

将您的开发组织统计数据导入电子表格

将您的开发组织统计数据导入电子表格

如果您想为 DEV 的个人帐户提取统计数据,这很容易做到,因为 DEV 有一个 API,并且已经有很棒的教程教您如何操作,但是如果您是 DEV 组织,该如何做同样的事情呢?

组织层面没有 API,但我们仍然可以通过我编写的简单爬虫程序获取数据。您只需导航到组织的仪表盘即可。

您的控制面板与您的公开页面不同,请确保您访问的是如下所示的页面:

https://dev.to/dashboard/organization/ORG_ID>
Enter fullscreen mode Exit fullscreen mode
  1. 打开 Chrome 浏览器
  2. 前往您所在组织的仪表盘
  3. 打开 Chrome 开发者工具
  4. 粘贴以下代码,它将自动下载包含所有可见统计数据的 CSV 文件。
  5. 尽情查看您的数据吧!
function articles(){
    const results = []
    const articles = document.querySelectorAll('.single-article')
    let data;
    for(let i = 0; i < articles.length; i++){
        data = article(articles[i])
        if (data) {
            results.push(data)
        }
    }
    return results
}

function article(article){
    // if there is no time selector that means this article 
    // is not published and will have no useful stats
    if (article.querySelector('time')){
        const name = article.querySelector('h2').innerText
        const tags = article.querySelectorAll('.tag')
        const author = article.querySelector('option[selected=selected]').innerText
        const date = article.querySelector('time').innerText
        const page_view_count = article.querySelector('.page-views-count').innerText
        const reactions_count = article.querySelector('.reactions-count').innerText
        const comments_count = article.querySelector('.comments-count').innerText
        const tags_string = []  
        for(let t = 0; t < tags.length; t++){
            tags_string.push(tags[t].innerText)
        }   
        return [
            name,
            tags_string.join(' '),
            author,
            date,
            page_view_count,
            reactions_count,
            comments_count
        ]
    } else {
        return false        
    }
}

function save_data(){
    const results = articles()
    results.unshift(['Name','Tags','Author','Date','Page Views Count','Reactions Count','Comments Count'])

    const csv_string = []
    for(let i = 0; i < results.length; i++){
        csv_string.push(results[i].join(','))
    }

  var blob = new Blob([csv_string.join("\n")], {type: 'text/csv'})
  let e    = document.createEvent('MouseEvents')
  let a    = document.createElement('a')
  const date = new Date().getTime()
  const epoch = Math.round(date / 1000)

  a.download = `export-${epoch}.csv`
  a.href = window.URL.createObjectURL(blob)
  a.dataset.downloadurl =  ['text/csv', a.download, a.href].join(':')
  e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
  a.dispatchEvent(e)
}

save_data()
Enter fullscreen mode Exit fullscreen mode

唯一的缺点是,如果 HTML 标记发生变化,可能会导致此抓取程序失效,需要进行一些小的修改才能修复此脚本。

我可以想象这个脚本被打包成一个 Chrome 扩展程序,它可以每天提取数据,将其推送到 Google 电子表格,并显示一段时间内的增长情况。

文章来源:https://dev.to/andrewbrown/pulling-your-dev-organization-stats-into-a-spreadsheet-2i1