Bing Web API v7如何获取仅英文结果?已尝试setLang等方法无效
I’ve run into this exact issue before—setLang and Accept-Language alone don’t always cut it for strict English-only content filtering. Here’s what actually works, prioritizing server-side solutions first:
1. Use the mkt Parameter (Server-Side, Most Effective)
The setLang parameter mainly controls the language of the API’s response metadata (like error messages), not the content language of the search results. Instead, use the mkt (market) parameter to target a specific English-speaking region. This tells Bing to prioritize content from that market, which is nearly always English.
For example, use:
mkt=en-US(English, United States)mkt=en-GB(English, United Kingdom)mkt=en-AU(English, Australia)
Combine this with setLang=en and the Accept-Language header to reinforce your request:
curl -H "Ocp-Apim-Subscription-Key: YOUR_API_KEY" \ -H "Accept-Language: en-US" \ "https://api.bing.microsoft.com/v7.0/websearch?q=your-search-query&mkt=en-US&setLang=en&count=10"
2. Add the cc Parameter (Optional, Extra Narrowing)
If you still see occasional non-English results, pair mkt with the cc (country code) parameter to restrict results to content hosted in an English-speaking country. For example:cc=US (United States) or cc=GB (United Kingdom)
This adds another layer of server-side filtering to ensure content is tied to an English-speaking region.
3. Fallback to Client-Side Filtering (Preserve Result Count)
If server-side filtering still returns a few non-English entries (rare, but possible for broad queries), adjust the count parameter to request more results than you need. For example, if you need 10 English results, request 15. Then, filter the response’s webPages.value array client-side to select only entries where language equals "en" until you have your desired number of results.
Here’s a quick JavaScript example of this fallback:
const desiredCount = 10; const rawResults = response.webPages.value; const englishResults = rawResults.filter(item => item.language === "en").slice(0, desiredCount);
4. Double-Check Your Endpoint
Ensure you’re using the correct v7 web search endpoint: https://api.bing.microsoft.com/v7.0/websearch. Using a different endpoint (like the general search endpoint) might not respect all filtering parameters as expected.
内容的提问来源于stack exchange,提问作者Brian Pace

