Bing Search API "&#39" appending to response

Daniel Troyer 96 Reputation points
2021-04-04T05:25:22.933+00:00

I am using the Bing Search API and this string of characters "&#39" is being appended to the name and description, is this simply because i am only using a Free Trial version or is the json not being parsed properly?

`search_url = "https://api.bing.microsoft.com/v7.0/news/search"
headers = {"Ocp-Apim-Subscription-Key" : subscription_key}
params  = {"q": search_term, "textDecorations": False, "textFormat": "HTML"}
response = requests.get(search_url, headers=headers, params=params)
response.raise_for_status()
response_dict = response.json()`

'name': 'What Is Filecoin and Why Is the Cryptocurrency's Price Going Up?',

Bing News Search
Bing News Search
A Bing service that supports searching for news and get comprehensive results.
43 questions
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Troyer 96 Reputation points
    2021-04-06T17:14:27.93+00:00
    1. List item

    Thank you for you response to my question, I am a beginner, I found my problem was not with the API or with Json parsing, but It was Django rendering the HTML, I fixed the problem by adding

    {% autoescape on %}  
    { name | safe }}  
    { description | safe }}  
    {% endautoescape %}  
    

1 additional answer

Sort by: Most helpful
  1. YutongTie-MSFT 46,566 Reputation points
    2021-04-06T00:41:47.06+00:00

    Hello,

    I just tried in my end and I can receive the right result with/without the "hypertext". But from my testing, "&#39" can not be translated correct to ('). You can use (') directly. Two things may help:

    1. The result shows only descriptions in the sample code
    2. You could set up the language with mkt = "en-US" in params

    I am sharing my JSON result for your reference:

    84672-image.png

    And also my code:

    import json  
    import os  
    import requests  
      
    subscription_key = "****************************"  
    assert subscription_key  
      
    search_url = "https://api.bing.microsoft.com/v7.0/news/search"  
    search_term = "What Is Filecoin and Why Is the Cryptocurrency's Price Going Up?"  
      
    mkt = 'en-US'  
    headers = {"Ocp-Apim-Subscription-Key" : subscription_key}  
    params  = {"q": search_term, "textDecorations": True, "textFormat": "HTML", 'mkt': mkt}  
    response = requests.get(search_url, headers=headers, params=params)  
    response.raise_for_status()  
    search_results = response.json()  
      
    descriptions = [article["description"] for article in search_results["value"]]  
    print(response.json())  
      
    from IPython.display import HTML  
    rows = "\n".join(["<tr><td>{0}</td></tr>".format(desc) for desc in descriptions])  
    HTML("<table>"+rows+"</table>")  
    

    Please let me know if you have any question.

    Regards,
    Yutong

    0 comments No comments