You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python结合Google图片修正职业词汇拼写并获取关联标签?

Fixing Misspelled Multilingual Professions with Google Images API

Hey there! This is totally feasible—and we can do it legally using Google's official API (no sketchy scraping that could get you blocked). Google Images has built-in spelling correction and image labeling, so we can leverage that to fix your profession terms. Here's how to pull it off:

Prerequisites First

Before writing code, you'll need two things from Google Cloud:

  • A Custom Search API key: Enable the Custom Search API in your Google Cloud Console to get this.
  • A Search Engine ID (CX): Create a custom search engine configured to allow image searches (you can set it to search the entire web for images).

Once you have those, we can jump into the code.

Full Script

First, install the required packages:

pip install pandas requests

Now the Python script:

import pandas as pd
import requests

# Replace these with your own credentials
API_KEY = "YOUR_GOOGLE_API_KEY"
CX = "YOUR_SEARCH_ENGINE_ID"

def correct_profession(term):
    # Google Custom Search API endpoint for image searches
    url = "https://www.googleapis.com/customsearch/v1"
    
    params = {
        "key": API_KEY,
        "cx": CX,
        "q": term,
        "searchType": "image",
        "num": 1  # Only fetch the first result
    }
    
    try:
        response = requests.get(url)
        response.raise_for_status()
        data = response.json()
        
        # Prioritize Google's built-in spelling correction
        if "spelling" in data:
            return data["spelling"]["correctedQuery"]
        
        # If no spelling fix is needed, extract the standard profession name from the first image's title
        if "items" in data and len(data["items"]) > 0:
            first_image_title = data["items"][0]["title"]
            # Clean up the title to isolate the profession term
            cleaned_title = first_image_title.split(" - ")[0].split(" | ")[0].strip()
            return cleaned_title
        
        # Fallback to original term if no results are found
        return term
    
    except Exception as e:
        print(f"Error processing '{term}': {str(e)}")
        return term

# Your original DataFrame
df = pd.DataFrame(data=['teacer','leraar','insegniaante','designer'], columns=['professions'])

# Apply correction to each profession entry
df['corrected_professions'] = df['professions'].apply(correct_profession)

print(df)

How It Works

  • Spelling Correction First: Google's API will return a correctedQuery if it detects a misspelling (like fixing "teacer" to "teacher").
  • Multilingual Support: The API automatically handles non-English terms—for example, it'll recognize "leraar" (Dutch for teacher) and return the correct standardized term, or fix "insegniaante" to the Italian "insegnante".
  • Fallback Handling: If the API call fails or no results are found, it returns the original term to avoid breaking your workflow.

Important Notes

  • API Limits: The free tier gives you 100 queries per day. For higher volumes, set up billing (it's very affordable for most use cases).
  • Credential Safety: Never hardcode your API key or CX in public code—use environment variables instead for production.

内容的提问来源于stack exchange,提问作者gabboshow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:57:54