我使用必应新闻搜索api来积累给定关键字的新闻,但是,我经常超时

2024-10-01 19:34:07 发布

您现在位置:Python中文网/ 问答频道 /正文

以下是相关代码:

@app.route('/getAcadNews')
def getAadNews():
    headers = {
        'accept': "application/json",
        'cache-control': "no-cache",
        'Ocp-Apim-Subscription-Key': "<API KEY HERE>"
    }

    news_items = {}

    query = removeBadOrdinal(request.args.get('kws'))
    url = "https://api.cognitive.microsoft.com/bing/v5.0/news/search?"

    querystring = {"q":"\""+query+"\" and \"papers published\"","count":"10000","freshness":"Month"}
    try:
        papernews = json.loads(requests.request("GET", url, headers=headers, params=querystring).text)
    except requests.exceptions.ConnectionError:
        papernews = {}

    if "totalEstimatedMatches" in papernews:
        news_items['papers_published_news'] = papernews["totalEstimatedMatches"]
        news_items['papers_published_news_item'] = getTopNews(papernews)
    else:
        news_items['papers_published_news'] = 0
        news_items['papers_published_news_item'] = {}

    querystring = {"q":"\""+query+"\" and \"patent\"","count":"10000","freshness":"Month"}
    try:
        patentnews = json.loads(requests.request("GET", url, headers=headers, params=querystring).text)
    except requests.exceptions.ConnectionError:
        patentnews = {}
    if "totalEstimatedMatches" in patentnews:
        news_items['patent_news'] = patentnews["totalEstimatedMatches"]
        news_items['patent_news_item'] = getTopNews(patentnews)
    else:
        news_items['patent_news'] = 0
        news_items['patent_news_item'] = {}

    resp1 = (json.dumps(news_items, indent=4))
    resp = Response(response=resp1,
        status=200, \
        mimetype="application/json")
    return(resp)

这有时是可行的,但有时,我会遇到以下错误:

^{pr2}$

如果有人能帮我解决这个问题,那会很有帮助的。我是付费的S2级。此问题主要发生在连续调用此函数3次以上时。在


Tags: jsonrequestitemsitemqueryrequestsheadersnews
1条回答
网友
1楼 · 发布于 2024-10-01 19:34:07

不幸的是你我无能为力,这是微软的错。尽管您可能不喜欢这个答案,但它们限制了请求和带宽。如果你的网速慢,这可能是一个因素。在

相关问题 更多 >

    热门问题