編集

次の方法で共有


Quickstart: Use the Bing Video Search Python client library

Use this quickstart to begin searching for news with the Bing Video Search client library for Python. While Bing Video Search has a REST API compatible with most programming languages, the client library provides an easy way to integrate the service into your applications. The source code for this sample can be found on GitHub with additional annotations and features.

Prerequisites

  • Python 2.x or 3.x.
  • The Bing Video Search client library for Python.

It is recommended that you use a Python virtual environment. You can install and initialize a virtual environment with the venv module. Install virtualenv for Python 2.7 with:

python -m venv mytestenv

Install the Bing Video Search client library with:

cd mytestenv
python -m pip install azure-cognitiveservices-search-videosearch

Create and initialize the application

  1. Create a new Python file in your favorite IDE or editor, and add the following import statements:

    from azure.cognitiveservices.search.videosearch import VideoSearchClient
    from azure.cognitiveservices.search.videosearch.models import VideoPricing, VideoLength, VideoResolution, VideoInsightModule
    from msrest.authentication import CognitiveServicesCredentials
    
  2. Create a variable for your subscription key.

    subscription_key = "YOUR-SUBSCRIPTION-KEY"
    endpoint = "YOUR-ENDPOINT"
    

Create the search client

Create an instance of the CognitiveServicesCredentials, and instantiate the client:

client = VideoSearchAPI(endpoint, CognitiveServicesCredentials(subscription_key))

Send a search request and get a response

  1. Use client.videos.search() with your search query to send a request to the Bing Video Search API, and then get a response.

    video_result = client.videos.search(query="SwiftKey")
    
  2. If the response contains search results, get the first one, and print its ID, name, and URL.

    if video_result.value:
        first_video_result = video_result.value[0]
        print("Video result count: {}".format(len(video_result.value)))
        print("First video id: {}".format(first_video_result.video_id))
        print("First video name: {}".format(first_video_result.name))
        print("First video url: {}".format(first_video_result.content_url))
    else:
        print("Didn't see any video result data..")
    

Next steps

See also