Json 使用RapidAPI的上下文Web新闻API的python客户端

Json 使用RapidAPI的上下文Web新闻API的python客户端,json,feed,rapidapi,Json,Feed,Rapidapi,我正在尝试使用上下文Web新闻API。此处描述了端点: 以下是Python中的请求片段,如RapidAPI所述: response=unirest.get(“https://contextualwebsearch-websearch-v1.p.rapidapi.com/api/Search/NewsSearchAPI?autoCorrect=true&pageNumber=1&pageSize=10&q=Taylor+Swift&safeSearch=false“, 标题={ “X-Rapid

我正在尝试使用上下文Web新闻API。此处描述了端点:

以下是Python中的请求片段,如RapidAPI所述:

response=unirest.get(“https://contextualwebsearch-websearch-v1.p.rapidapi.com/api/Search/NewsSearchAPI?autoCorrect=true&pageNumber=1&pageSize=10&q=Taylor+Swift&safeSearch=false“,
标题={
“X-RapidAPI-Host”:“contextualwebsearch-websearch-v1.p.RapidAPI.com”,
“X-RapidAPI-Key”:“XXXXXX”
}
)

如何发送请求并解析响应?你能为新闻API提供一个完整的代码示例吗?

下面的代码使用python版本3.X。下面是我传递字符串Taylor Swift并解析响应的完整示例…如果你在任何地方卡住了,请告诉我

import requests  # install from: http://docs.python-requests.org/en/master/

# Replace the following string value with your valid X-RapidAPI-Key.
Your_X_RapidAPI_Key = "XXXXXXXXXXXXXXXXXXX";

# The query parameters: (update according to your search query)
q = "Taylor%20Swift"  # the search query
pageNumber = 1  # the number of requested page
pageSize = 10  # the size of a page
autoCorrect = True  # autoCorrectspelling
safeSearch = False  # filter results for adult content

response = requests.get(
    "https://contextualwebsearch-websearch-v1.p.rapidapi.com/api/Search/NewsSearchAPI?q={}&pageNumber={}&pageSize={}&autocorrect={}&safeSearch={}".format(
        q, pageNumber, pageSize, autoCorrect, safeSearch),
    headers={
        "X-RapidAPI-Key": Your_X_RapidAPI_Key
    }
).json()

# Get the numer of items returned
totalCount = response["totalCount"];

# Get the list of most frequent searches related to the input search query
relatedSearch = response["relatedSearch"]

# Go over each resulting item
for webPage in response["value"]:
    # Get the web page metadata
    url = webPage["url"]
    title = webPage["title"]
    description = webPage["description"]
    keywords = webPage["keywords"]
    provider = webPage["provider"]["name"]
    datePublished = webPage["datePublished"]

    # Get the web page image (if exists)
    imageUrl = webPage["image"]["url"]
    imageHeight = webPage["image"]["height"]
    imageWidth = webPage["image"]["width"]

    thumbnail = webPage["image"]["thumbnail"]
    thumbnailHeight = webPage["image"]["thumbna`enter code here`ilHeight"]

# An example: Output the webpage url, title and published date:
print("Url: %s. Title: %s. Published Date:%s." % (url, title, datePublished))

工作起来很有魅力!谢谢同样的代码是否也适用于ContextualWeb搜索API和图像API?我是否应该更改端点名称?