PageSpeed Insights API V5

时间:2018-12-05 16:31:56

标签: pagespeed-insights

您好,编码员,            我想将Google移动友好型和桌面友好型api与PageSpeed Insights API V5集成在一起。但是我无法区分“审计”部分。我也尝试过区分场景,但是我做不到。

如何区分PageSpeed Insights API V5中的通过审核诊断机会

非常感谢

2 个答案:

答案 0 :(得分:0)

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

// 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

谢谢

答案 1 :(得分:0)

**机会:**响应中没有可用的经过编译的机会集,但是我们可以遍历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