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)
相关推荐
weixin_457885825 小时前
DeepSeek与搜索引擎:AI生成内容如何突破“语义天花板”
人工智能·搜索引擎·ai·deepseek
老友@6 小时前
Elasticsearch 全面解析
大数据·elasticsearch·搜索引擎
weixin_4578858212 小时前
DeepSeek:AI如何重构搜索引擎时代的原创内容生态
人工智能·搜索引擎·ai·重构·deepseek
小尹呀17 小时前
LangChain-提示模板 (Prompt Templates)
搜索引擎·langchain·prompt
qq_5470261791 天前
Elasticsearch 集群搭建
大数据·elasticsearch·搜索引擎
三天不学习1 天前
Lucene.Net全文搜索引擎:架构解析与全流程实战指南
搜索引擎·.net·lucene
Elastic 中国社区官方博客2 天前
将 CrewAI 与 Elasticsearch 结合使用
大数据·人工智能·elasticsearch·机器学习·搜索引擎·ai·全文检索
铭毅天下2 天前
Elasticsearch 8.X 如何利用嵌入向量提升搜索能力?
大数据·elasticsearch·搜索引擎·全文检索
CXH7282 天前
Git 的进阶功能和技巧
大数据·elasticsearch·搜索引擎
旧故新长2 天前
IDEA 中遇到 Git Log 界面不显示问题的解决方案
大数据·elasticsearch·搜索引擎