PageSpeed Insights API V5

PageSpeed Insights API V5

我想将 Google 移动友好和桌面友好 api 与 PageSpeed Insights API V5 集成。但我无法区分审计部分。我也尝试了不同的场景,但我做不到。

如何在 PageSpeed Insights [=22] 中区分通过审核诊断机会 =] V5?

以下是 GoogleChrome 灯塔用来区分机会、诊断和通过审核的代码,您可以在下面找到 github link。

// Opportunities
const opportunityAudits = category.auditRefs
        .filter(audit => audit.group === 'load-opportunities' && !Util.showAsPassed(audit.result))
        .sort((auditA, auditB) => this._getWastedMs(auditB) - this._getWastedMs(auditA));

// Diagnostics
const diagnosticAudits = category.auditRefs
        .filter(audit => audit.group === 'diagnostics' && !Util.showAsPassed(audit.result))
        .sort((a, b) => {
          const scoreA = a.result.scoreDisplayMode === 'informative' ? 100 : Number(a.result.score);
          const scoreB = b.result.scoreDisplayMode === 'informative' ? 100 : Number(b.result.score);
          return scoreA - scoreB;
        });

// Passed audits
const passedAudits = category.auditRefs
        .filter(audit => (audit.group === 'load-opportunities' || audit.group === 'diagnostics') &&
            Util.showAsPassed(audit.result));

参考:https://github.com/GoogleChrome/lighthouse/blob/master/lighthouse-core/report/html/renderer/performance-category-renderer.js

在上面的代码中,Util.showAsPassed() 方法指定如下。

const PASS_THRESHOLD = 0.9;
const RATINGS = {
  PASS: {label: 'pass', minScore: PASS_THRESHOLD},
  AVERAGE: {label: 'average', minScore: 0.5},
  FAIL: {label: 'fail'},
  ERROR: {label: 'error'},
};

static showAsPassed(audit) {
    switch (audit.scoreDisplayMode) {
      case 'manual':
      case 'notApplicable':
        return true;
      case 'error':
      case 'informative':
        return false;
      case 'numeric':
      case 'binary':
      default:
        return Number(audit.score) >= RATINGS.PASS.minScore;
    }
  }

参考:https://github.com/GoogleChrome/lighthouse/blob/master/lighthouse-core/report/html/renderer/util.js

谢谢

** 机会:** 响应中没有可用的编译机会集,但我们可以遍历 lighthouseResult 并针对 lighthouseReslt 中的每个 json,取出 type=opportunity 的结果并将其也应该有详细资料。

response = requests.get('https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url='+<url>+'&strategy='+<desktop or mobile>+'&key='+<api_key>)
js = response.json()

print ("Score: ",js['lighthouseResult']['categories']['performance']['score']*100)

k = []

for m in js['lighthouseResult'] :

    try :
        for i in js['lighthouseResult'][m] :
            k.append([js['lighthouseResult'][m][i]['details'],js['lighthouseResult'][m][i]['title']])
    except :
        pass

final_opportunities = []

print (len(k))
for i in k :
    if 'overallSavingsMs' in list(i[0].keys()) :

        print (i[1],i[0]['overallSavingsMs'])
        final_opportunities.append([i[1] , i[0]['overallSavingsMs']])

审计结果可以在: lighthouseResult.audits

综合表现得分:response.lighthouseResult.categories.performance.score

对于 python 实现,您可以参考以下 github 存储库: https://github.com/amartya-dev/PageSpeedAPI