跳到正文
DEV Community· ByteWright·· 3 小時前AI 評分56

如何在 Google Trends 比較超過 5 個關鍵字,以及「swift」為何高於「python」

How to compare more than 5 keywords in Google Trends (and why "swift" beat "python")

AI 導讀

ByteWright 提出以每批共用的錨點關鍵字校準 Google Trends 數據,將超過 5 個搜尋詞放到同一尺度比較。示例以「java」作錨點,分 3 批比較 12 個詞;「swift」的搜尋量亦包括 Taylor Swift 和 Suzuki Swift,不能直接視為 Swift 程式語言的熱度。作者提醒各批必須使用相同國家、時間範圍和類別,並提到快速連續請求可能觸發 HTTP 429。

正文

The cover chart shows 12 search terms on one Google Trends scale, US, past 12 months. You can't get that chart from Google directly. The compare box stops at 5 terms, and each comparison is rescaled so its own top value is 100.

Before the method, the surprise in the data: "swift" almost ties "python". That's not the programming language pulling ahead. The plain word "swift" also counts searches for Taylor Swift and the Suzuki Swift car. I'll come back to that, because it's the mistake that ruins most keyword comparisons.

Why you can't just run more comparisons

I asked Google Trends about "rust" twice, same day, same country, same 12 months. Next to "python" it averaged 30.1. Next to "dart" it averaged 41.6.

Average interest in

Same searches, different numbers, because each request sets its own top value to 100. So you can't put the results of two separate comparisons in one spreadsheet column and sort them.

The fix: an anchor keyword

Put the same keyword, the anchor, in every batch:

  • batch 1: anchor + 4 keywords
  • batch 2: anchor + the next 4
  • and so on

The anchor's real search volume is the same in every batch; only its scale changes. That gives you one conversion factor per batch:

factor = sum of anchor values in batch 1 / sum of anchor values in this batch

Multiply every keyword in a batch by its factor, and everything is on batch 1's scale. Then divide by the overall maximum and multiply by 100, so the top value is 100 again.

For the chart above I used "java" as the anchor: 11 other terms, 3 batches.

Picking the anchor

Google returns whole numbers. If the anchor is tiny next to the biggest keyword in its batch, its values round to 0, 1 or 2 and the factor gets noisy. Pick an anchor that's clearly visible in every batch. "java" averaged 14.9 on the final scale here, well above the rounding floor. Use the same country, time range and category in every batch, or the numbers aren't comparable.

The code

This uses pytrends, an unofficial Google Trends client:

import random
import time

import pandas as pd
from pytrends.request import TrendReq


def fetch_batch(keywords, timeframe='today 12-m', geo='US'):
    pytrends = TrendReq(hl='en-US', tz=0)
    pytrends.build_payload(keywords, timeframe=timeframe, geo=geo)
    return pytrends.interest_over_time().drop(columns='isPartial', errors='ignore')


def stitch(batches: list[pd.DataFrame], anchor: str) -> pd.DataFrame:
    """Put every batch on batch 1's scale using the anchor, then rescale to a peak of 100."""
    ref_total = batches[0][anchor].sum()
    merged = batches[0].copy()
    for batch in batches[1:]:
        factor = ref_total / batch[anchor].sum()
        for column in batch.columns:
            if column != anchor:
                merged[column] = batch[column] * factor
    return (merged * 100 / merged.max().max()).round(2)


anchor = 'java'
others = ['python', 'javascript', 'typescript', 'golang', 'rust', 'kotlin',
          'swift', 'ruby', 'php', 'scala', 'dart']

batches = []
for i in range(0, len(others), 4):
    batches.append(fetch_batch([anchor, *others[i:i + 4]]))
    time.sleep(random.uniform(4, 7))  # Google rate-limits fast; see below

result = stitch(batches, anchor)
print(result.mean().sort_values(ascending=False).round(1))

To check it, compare any 4 or 5 of your keywords in one direct request and line up the averages with the stitched ones. When I did that with "java", "python", "rust" and "golang", the stitched averages matched within 0.04. The largest gap was on "golang", the smallest term.

Three things that bite

Search terms aren't topics. "swift" counts searches for Taylor Swift, "rust" includes the video game, "python" the snake, "dart" the pub game. Google's topic IDs fix this: pytrends.suggestions('Taylor Swift') returns entries with an mid (/m/0dl567 for Taylor Swift) that you can pass to build_payload instead of a word. In one request, the Taylor Swift topic averaged 24.2 next to 28.9 for the plain word "swift". Topic lookup is hit-and-miss for programming languages, though: searching suggestions for the Swift language mostly returned books. Often it's easier to qualify the term ("swift programming") and check what you're actually measuring.

Rate limits. In my testing Google started answering HTTP 429 after 3 or 4 quick requests from one IP. The 5 requests behind this article went through with 4–7 second pauses. For 50 keywords (13 batches) you'll want retries with growing waits, and probably more than one IP.

Tiny values. "kotlin" ends at 0.8 on this scale. That's correct relative to the others, but at that size the values are coarse. If small terms matter most to you, put them in a separate comparison with a smaller anchor.

If you'd rather not run it yourself

Disclosure: I built a hosted version of this method, Google Trends Scraper on Apify. It takes any number of keywords, does the batching, anchoring and retries, flags rows where the anchor was too weak, and exports JSON, CSV or Excel. It costs $3 per 1,000 results.

For a few dozen keywords and a bit of patience, the script above does the same job for free.


I wrote this with help from an AI assistant and checked every claim and code sample myself. The data was fetched on 2026-10-23.

來源:DEV Community · dev.to