bing官方api搜索引擎

bing官方api搜索引擎

1. bing API说明

微软 Bing 的搜索 API 使得开发者能够将 Bing 的搜索能力集成到自己的应用中,包括对网页、图片、新闻、视频的搜索,以及提供了实体搜索和视觉搜索的功能。这些 API 支持安全、无广告且能够根据地理位置提供相关信息的搜索结果。Bing Web Search API v7 允许用户从全球范围内的数十亿个网页文档中检索信息。

2. 秘钥申请

参考Langchain-Chatchat3.1------搜索引擎bing与DuckDuckGo

3. 官方文档地址

官方文档

4. coding案例

下面是一个bing的新闻搜索按理,采用fastapi进行快速部署的服务

复制代码
# -*- coding: utf-8 -*-
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
import json
from datetime import datetime
import requests
import re



app = FastAPI()  # 创建API实例
app.add_middleware(
    CORSMiddleware,
    # 允许跨域的源列表,例如 ["http://www.example.org"] 等等,["*"] 表示允许任何源
    allow_origins=["*"],
    # 跨域请求是否支持 cookie,默认是 False,如果为 True,allow_origins 必须为具体的源,不可以是 ["*"]
    allow_credentials=False,
    # 允许跨域请求的 HTTP 方法列表,默认是 ["GET"]
    allow_methods=["*"],
    # 允许跨域请求的 HTTP 请求头列表,默认是 [],可以使用 ["*"] 表示允许所有的请求头
    # 当然 Accept、Accept-Language、Content-Language 以及 Content-Type 总之被允许的
    allow_headers=["*"],
    # 可以被浏览器访问的响应头, 默认是 [],一般很少指定
    # expose_headers=["*"]
    # 设定浏览器缓存 CORS 响应的最长时间,单位是秒。默认为 600,一般也很少指定
    # max_age=1000
)

def bing_news_search(search_term):

    subscription_key = "bing秘钥"

    search_url = "https://api.bing.microsoft.com/v7.0/news/search"
    headers = {"Ocp-Apim-Subscription-Key" : subscription_key}
    params  = {"q": search_term, "textDecorations": True, "textFormat": "HTML"}

    response = requests.get(search_url, headers=headers, params=params)
    status_code = response.status_code

    response.raise_for_status()
    search_results = response.json()
    return status_code,search_results["value"][0]


@app.post("/news_search")
async def create_item(request: Request):
    json_post_raw = await request.json()
    json_post = json.dumps(json_post_raw)
    json_post_list = json.loads(json_post)
    prompt = json_post_list.get('search')
    status_code,res = bing_news_search(prompt)

    now = datetime.now()
    time = now.strftime("%Y-%m-%d %H:%M:%S")

    answer = {
        "data": res,
        "status": status_code,
        "time": time
    }
    return answer


if __name__ == '__main__':
    uvicorn.run(app, host='0.0.0.0', port=9009, workers=1)
相关推荐
Elastic 中国社区官方博客6 小时前
JavaScript 中的 ES|QL:利用 Apache Arrow 工具
大数据·开发语言·javascript·elasticsearch·搜索引擎·全文检索·apache
云徙科技9 小时前
人工智能时代汽车营销如何创新突破?云徙科技汽车营销智能体助力车企立足数智化转型
搜索引擎
G皮T1 天前
【Elasticsearch】正排索引、倒排索引(含实战案例)
大数据·elasticsearch·搜索引擎·kibana·倒排索引·搜索·正排索引
G皮T1 天前
【Elasticsearch】Elasticsearch 近实时高速查询原理
大数据·elasticsearch·搜索引擎·全文检索·倒排索引·搜索·nrt
1296004522 天前
神经网络压缩
搜索引擎·百度
Liudef062 天前
2025年中国人工智能发展研究报告:技术突破、行业变革与全球竞争新格局
搜索引擎
摇滚侠2 天前
elasticSearch是什么,如何使用,有什么用
大数据·elasticsearch·搜索引擎
代码搬运媛3 天前
ES Modules 与 CommonJS 的核心区别详解
大数据·elasticsearch·搜索引擎
数据智能老司机3 天前
Elastic 向量搜索实战指南——Elastic中的模型管理与向量相关考量
elasticsearch·搜索引擎·llm
Elastic 中国社区官方博客3 天前
使用 OpenTelemetry 和 Elastic 简化公共部门的可观察性
大数据·elasticsearch·搜索引擎·全文检索·可用性测试·opentelemetry