Example #1
0
def grab_nasdaq100_tickers():
    load_dotenv()
    base_url = "https://msft-azure-hacks-image.cognitiveservices.azure.com/bing/v7.0"
    api_key = os.getenv("AZURE_API_KEY_IMAGE")
    client = WebSearchAPI(CognitiveServicesCredentials(api_key),
                          base_url=base_url)
    web_data = client.web.search(query="NASDAQ-100")
    for i in range(len(web_data.web_pages.value)):
        if 'wikipedia' in web_data.web_pages.value[i].url:
            query = web_data.web_pages.value[i].url
            break

    res = requests.get(query).text
    soup = BeautifulSoup(res, 'lxml')
    ticker_table = soup.find('table', {'class': 'wikitable sortable'})

    links = ticker_table.findAll('a')
    company_names = []
    for link in links:
        company_names.append(link.get('title'))

    ticker_links = ticker_table.findAll('td')
    tickers = []
    for i in range(len(ticker_links)):
        if i % 2 == 1:
            ticker = re.sub(r"<.?table[^>]*>|<.?t[rd]>|<font[^>]+>|<.?b>", "",
                            str(ticker_links[i]))
            tickers.append(re.sub("(\\r|)\\n$", "", ticker))

    df = pd.DataFrame()
    df['company'] = company_names
    df['ticker'] = tickers

    return df
Example #2
0
def add_event():
    check = Playersonly()
    check.name = request.args.get('name')
    check.formatName()
    name = check.name
    new_player = Highscore.query.filter_by(name=name).first()
    if new_player is None:
        try:
            client = WebSearchAPI(
                CognitiveServicesCredentials(subscription_key))
            web_data = client.web.search(query=name)
            x = web_data.additional_properties['entities']['value'][0][
                'description']
            if ("nba" in x.lower()) or ("national basketball association"
                                        in x.lower()) or ("retired"
                                                          in x.lower()):
                quote_page = f'https://www.basketball-reference.com/players/{check.url[0]}/{check.url}01.html'
                page = urllib2.urlopen(quote_page)
                soup = BeautifulSoup(page, 'html.parser')
                career_stats = soup.find('div',
                                         attrs={'class': 'stats_pullout'})
                if career_stats is None:
                    payload = {
                        'error':
                        'There is no basketball reference page for that player. Is it possible that you made a spelling mistake or are using a nickname? For instance, if you want Metta World Peace, search for Ron Artest'
                    }
                    return jsonify(payload)
                else:
                    check.ppg = career_stats.find_all('p')[5].text
                    check.rebounds = career_stats.find_all('p')[7].text
                    check.assists = career_stats.find_all('p')[9].text
                    check.per = career_stats.find_all('p')[19].text
                    highscore = Highscore(name=name,
                                          ppg=float(check.ppg),
                                          rebounds=float(check.rebounds),
                                          assists=float(check.assists),
                                          per=float(check.per),
                                          picture_url="",
                                          rating=15)
                    db.session.add(highscore)
                    db.session.commit()
                    payload = {
                        "name": highscore.name,
                        "ppg": highscore.ppg,
                        "rebounds": highscore.rebounds,
                        "assists": highscore.assists,
                        "per": highscore.per
                    }
            else:
                payload = {
                    'error': 'Hmmm. Are you sure that is an NBA player?'
                }

            return jsonify(payload)
        except Exception as e:
            print(str(e))
            return jsonify(str(e))
    else:
        error = {'error': 'that player is already registered'}
        return jsonify(error)
Example #3
0
def web_search_with_response_filter(subscription_key):
    """WebSearchWithResponseFilter.

    This will search (Microsoft) with response filters to news and print details of news.
    """

    client = WebSearchAPI(CognitiveServicesCredentials(subscription_key))

    try:
        web_data = client.web.search(query="Microsoft",
                                     response_filter=["News"])
        print(
            "Searched for Query# \" Microsoft \" with response filters \"News\""
        )

        # News attribute since I filtered "News"
        if web_data.news.value:

            print("Webpage Results#{}".format(len(web_data.news.value)))

            first_web_page = web_data.news.value[0]
            print("First web page name: {} ".format(first_web_page.name))
            print("First web page URL: {} ".format(first_web_page.url))

        else:
            print("Didn't see any Web data..")

    except Exception as err:
        print("Encountered exception. {}".format(err))
Example #4
0
def web_search_with_answer_count_promote_and_safe_search(subscription_key):

    client = WebSearchAPI(CognitiveServicesCredentials(subscription_key))

    try:
        '''
        Set the query, answer_count, promote, and safe_search parameters using the SDK's search method. See:
        https://docs.microsoft.com/python/api/azure-cognitiveservices-search-websearch/azure.cognitiveservices.search.websearch.operations.weboperations?view=azure-python.
        '''
        web_data = client.web.search(
            query="Niagara Falls",
            answer_count=2,
            promote=["videos"],
            safe_search=SafeSearch.strict  # or directly "Strict"
        )
        print("\r\nSearching for \"Niagara Falls\"")

        '''
        If results are available, print the # of responses, and the first result returned.
        '''
        if web_data.web_pages.value:

            print("Webpage Results#{}".format(len(web_data.web_pages.value)))

            first_web_page = web_data.web_pages.value[0]
            print("First web page name: {} ".format(first_web_page.name))
            print("First web page URL: {} ".format(first_web_page.url))

        else:
            print("Didn't see any Web data..")

    except Exception as err:
        print("Encountered exception. {}".format(err))
Example #5
0
def web_search_with_answer_count_promote_and_safe_search(subscription_key):
    """WebSearchWithAnswerCountPromoteAndSafeSearch.

    This will search (Lady Gaga) with answerCount and promote parameters and print details of answers.
    """

    client = WebSearchAPI(CognitiveServicesCredentials(subscription_key))

    try:
        web_data = client.web.search(
            query="Lady Gaga",
            answer_count=2,
            promote=["videos"],
            safe_search=SafeSearch.strict  # or directly "Strict"
        )
        print("Searched for Query# \" Lady Gaga\"")

        if web_data.web_pages.value:

            print("Webpage Results#{}".format(len(web_data.web_pages.value)))

            first_web_page = web_data.web_pages.value[0]
            print("First web page name: {} ".format(first_web_page.name))
            print("First web page URL: {} ".format(first_web_page.url))

        else:
            print("Didn't see any Web data..")

    except Exception as err:
        print("Encountered exception. {}".format(err))
Example #6
0
def web_results_with_count_and_offset(subscription_key):
    """WebResultsWithCountAndOffset.

    This will search (Best restaurants in Seattle), verify number of results and print out name and url of first result.
    """

    client = WebSearchAPI(CognitiveServicesCredentials(subscription_key))

    try:
        web_data = client.web.search(query="Best restaurants in Seattle",
                                     offset=10,
                                     count=20)
        print("Searched for Query# \" Best restaurants in Seattle \"")

        if web_data.web_pages.value:

            print("Webpage Results#{}".format(len(web_data.web_pages.value)))

            first_web_page = web_data.web_pages.value[0]
            print("First web page name: {} ".format(first_web_page.name))
            print("First web page URL: {} ".format(first_web_page.url))

        else:
            print("Didn't see any Web data..")

    except Exception as err:
        print("Encountered exception. {}".format(err))
Example #7
0
def web_search_with_response_filter(subscription_key):
    client = WebSearchAPI(CognitiveServicesCredentials(subscription_key))
    try:
        '''
        Set the query, response_filter, and freshness using the SDK's search method. See:
        https://docs.microsoft.com/python/api/azure-cognitiveservices-search-websearch/azure.cognitiveservices.search.websearch.operations.weboperations?view=azure-python.
        '''
        web_data = client.web.search(query="xbox",
            response_filter=["News"],
            freshness="Day")
        print("\r\nSearching for \"xbox\" with the response filter set to \"News\" and freshness filter set to \"Day\".")

        '''
        If news articles are available, print the # of responses, and the first and second
        articles returned.
        '''
        if web_data.news.value:

            print("# of news results: {}".format(len(web_data.news.value)))

            first_web_page = web_data.news.value[0]
            print("First article name: {} ".format(first_web_page.name))
            print("First article URL: {} ".format(first_web_page.url))

            print("")

            second_web_page = web_data.news.value[1]
            print("\nSecond article name: {} ".format(second_web_page.name))
            print("Second article URL: {} ".format(second_web_page.url))

        else:
            print("Didn't find any news articles...")

    except Exception as err:
        print("Encountered exception. {}".format(err))
Example #8
0
def web_results_with_count_and_offset(subscription_key, city):
    client = WebSearchAPI(CognitiveServicesCredentials(subscription_key))
    global baike
    try:
        '''
         Set the query, offset, and count using the SDK's search method. See:
         https://docs.microsoft.com/python/api/azure-cognitiveservices-search-websearch/azure.cognitiveservices.search.websearch.operations.weboperations?view=azure-python.
         '''
        city = city
        web_data = client.web.search(query=city, offset=0, count=5)

        if web_data.web_pages.value:
            '''
             If web pages are available, print the # of responses, and the first and second
             web pages returned.
             '''
            #print("Webpage Results#{}".format(len(web_data.web_pages.value)))
            print(web_data.web_pages.value)
            for i in web_data.web_pages.value:
                if "百科" in format(i.name):
                    webPage = format(i.name)
                    webUrl = format(i.url)
                    webSnippet = format(i.snippet)

            baike = {"page": webPage, "url": webUrl, "snippet": webSnippet}
            #print("First web page name: {} ".format(first_web_page.name))
            #print("First web page URL: {} ".format(first_web_page.url))

        else:
            print("Didn't find any web pages...")

    except Exception as err:
        print("Encountered exception. {}".format(err))
Example #9
0
def get_passage(question):
    passage_list = []
    try:
        os.system(
            'cd /home/sangzhijie && bash logbuptgw.sh -i 2016110858 6361311')
        subscription_key = "70be76aee0f54666a6498ed07334dabe"
        client = WebSearchAPI(CognitiveServicesCredentials(subscription_key))
        web_data = client.web.search(query=question, set_lang='EN')
        if hasattr(web_data.web_pages, 'value'):
            data_list = [
                item for item in web_data.web_pages.value
                if item.additional_properties['language'] == 'en'
            ]
            print("Total num: " + str(len(web_data.web_pages.value)) +
                  '\tFiltered num: ' + str(len(data_list)))
            for i in range(len(data_list)):
                web_page = data_list[i]
                name = web_page.name
                url = web_page.url
                snippet = web_page.snippet
                passage_list.append([snippet, url])
        else:
            print("Didn't find any web pages...")
    except:
        os.system(
            'cd /home/sangzhijie && bash logbuptgw.sh -i 2016110858 6361311')
        print("get_passage: ERROR!!!")
    return passage_list
def simple_bing_search(search_word):
    subscription_key = "" # input key from Bing search
    assert subscription_key
    client = WebSearchAPI(CognitiveServicesCredentials(subscription_key))
    web_data_raw = client.web.search(query=search_word, raw = True, count = 50)
    raw_text = web_data_raw.response.text
    raw_dict = json.loads(raw_text)
    return raw_dict
Example #11
0
    def _bing(self, msg):
        from azure.cognitiveservices.search.websearch import WebSearchAPI
        from azure.cognitiveservices.search.websearch.models import SafeSearch
        from msrest.authentication import CognitiveServicesCredentials
        subscription_key = "YOUR_SUBSCRIPTION_KEY"
        client = WebSearchAPI(CognitiveServicesCredentials(subscription_key), base_url = "YOUR_ENDPOINT")
        web_data = client.web.search(query=msg)
        if hasattr(web_data.web_pages, 'value'):
            first_web_page = web_data.web_pages.value[0]
            print("First web page name: {} ".format(first_web_page.name))
            print("First web page URL: {} ".format(first_web_page.url))
        else:
            print("Didn't find any web pages...")
        if hasattr(web_data.images, 'value'):

            print("\r\nImage Results#{}".format(len(web_data.images.value)))

            first_image = web_data.images.value[0]
            print("First Image name: {} ".format(first_image.name))
            print("First Image URL: {} ".format(first_image.url))

        else:
            print("Didn't find any images...")

        '''
        News
        If the search response contains news, the first result's name and url
        are printed.
        '''
        if hasattr(web_data.news, 'value'):

            print("\r\nNews Results#{}".format(len(web_data.news.value)))

            first_news = web_data.news.value[0]
            print("First News name: {} ".format(first_news.name))
            print("First News URL: {} ".format(first_news.url))

        else:
            print("Didn't find any news...")

        '''
        If the search response contains videos, the first result's name and url
        are printed.
        '''
        if hasattr(web_data.videos, 'value'):

            print("\r\nVideos Results#{}".format(len(web_data.videos.value)))

            first_video = web_data.videos.value[0]
            print("First Videos name: {} ".format(first_video.name))
            print("First Videos URL: {} ".format(first_video.url))

        else:
            print("Didn't find any videos...")
Example #12
0
def place_details(search_place, restaurant_info):
    api_key = return_bing_image_api_key()
    credentials = WebSearchAPI(CognitiveServicesCredentials(api_key))
    information = credentials.web.search(query=search_place)

    if hasattr(information.web_pages, 'value'):
        web_page = information.web_pages.value[0]
        print(web_page.snippet)
        restaurant_info["snippet"] = replace_characters(web_page.snippet)
    else:
        restaurant_info["snippet"] = "Didn't find any web pages!"
Example #13
0
def searchWeb(text, output, c):
    try:
        text = text.encode('utf-8')
    except:
        text = text
    #query = urllib.quote_plus(text)
    #query = text
    #if len(query)>60:
    #    return output,c
    #using googleapis for searching web
    try:
        subscription_key = "9ea45659ed914562b85ed5b1a1feb1f1"  #FIXME (Update this to my Azure subscription key?)
        client = WebSearchAPI(CognitiveServicesCredentials(subscription_key))
        response = client.web.search(query=text)
        if hasattr(response.web_pages, 'value'):
            for page in response.web_pages.value:
                print "page name: {} ".format(page.name)
                print "page URL: {} ".format(page.url)
                content = page.snippet
                if page.url in output:
                    output[page.url] = output[page.url] + 1
                    c[page.url] = (c[page.url] * (output[page.url] - 1) +
                                   cosineSim(text, strip_tags(content))) / (
                                       output[page.url])
                else:
                    output[page.url] = 1
                    c[page.url] = cosineSim(text, strip_tags(content))
        else:
            print("no match found...")
    #base_url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q='
    #url = base_url + '%22' + query + '%22'
    #request = urllib2.Request(url,None,{'Referer':'Google Chrome'})
    #response = urllib2.urlopen(request)
    #results = simplejson.load(response)

#        if (len(results) and 'responseData' in results and 'results' in results['responseData'] and results['responseData']['results'] != []):
#            for ele in results['responseData']['results']:
#                Match = results['responseData']['results'][0]
#                content = Match['content']
#                if Match['url'] in output:
#                    #print text
#                    #print strip_tags(content)
#                    output[Match['url']] = output[Match['url']] + 1
#                    c[Match['url']] = (c[Match['url']]*(output[Match['url']] - 1) + cosineSim(text,strip_tags(content)))/(output[Match['url']])
#                else:
#                    output[Match['url']] = 1
#                    c[Match['url']] = cosineSim(text,strip_tags(content))
    except:
        return output, c
    return output, c
Example #14
0
def main():
    if len(sys.argv) != 3:
        print("Usage: {} <AGOR input CSV> <Harradine output CSV>".format(sys.argv[0]))
        return

    # Azure creds
    try:
        subscription_key = os.environ.get('AZURE_KEY')
    except:
        print("Must supply AZURE_KEY environment variable")
        return
    client = WebSearchAPI(CognitiveServicesCredentials(subscription_key), base_url="https://api.cognitive.microsoft.com/bing/v7.0")

    agencies = read_agor(sys.argv[1])
    output = find_harradine_reports(agencies, client)
    # TODO: For agencies/websites without their own Harradine list, we probably want to use the portfolio department's list rather than some random result

    with open(sys.argv[2], 'w') as f:
        writer = csv.DictWriter(f, ['Title', 'Portfolio', 'Domain', 'ReportURL', 'ReportPageTitle'])
        writer.writeheader()
        writer.writerows(output)
Example #15
0
def getBing(firstTag):
    #based on very first tag generated, do the tag    
    subkey = os.getenv('BING_SEARCH_KEY') #bing key
    
    #declare client
    dict = {}
    client = WebSearchAPI(CognitiveServicesCredentials(subkey))
    web_data = client.web.search(query=firstTag)
    print("Searched for Query:  " + firstTag)
    if web_data.web_pages.value:
        length = len(web_data.web_pages.value)
        i = 0
        while i < length and i < 6:
            page = web_data.web_pages.value[i]
            title = page.name
            url = page.url
            print(title, url)
            dict[url] = title
            i+=1
    
    return dict
Example #16
0
def main():   
    # Load environmental variables
    SC_ENDPOINT = os.environ['SC_ENDPOINT']
    SC_KEY = os.environ['SC_KEY']

    # Instantiate the client and replace with your endpoint.
    client = WebSearchAPI(CognitiveServicesCredentials(SC_KEY), base_url = SC_ENDPOINT)

    categories = { 'data': [ 'data', 'operating system', 'container', 'dell', 'emc', 'networking', 'virtualization', 'db', 'database', 'relational', 'erp'],
                    'ml' : [ 'machine learning', 'ml', 'intelligence', 'artificial', 'science'],
                    'storage': ['storage', 'nas'],
                    'iot': ['iot', 'thing', 'internet of things','thermostat','bluetooth','modem', 'hardware', 'camera'],
                    'devops': ['devops', 'code', 'pipeline', 'api', 'kubernetes', 'k8s', 'gaming'],
                    'securty': ['cissp', 'hacker', 'security','defense', 'keys', 'privacy', 'virus', 'anti-virus'],
                    'health': ['hipaa'],
                    'media': ['streaming', 'video']
                    }

    # Load worksheet
    wb = load_workbook(filename = 'kusto_output.xlsx')
    ws = wb.active
    colA = ws['A']

    # Add Column for category in the sheet
    ws.insert_cols(2,2)
    ws['B1'] = 'Top_Category'
    ws['C1'] = 'Categories'

    # Search the tem and add it to the column starting at row B2
    for index, row in enumerate(colA[1:14176], start = 2):
        categories_found = bingSearch(client, categories, row.value)
        category_csv = ''
        for category in sorted(categories_found, key=categories_found.get):
            category_csv = category_csv + ',' + category
        ws['C' + str(index)] = category_csv
        ws['B' + str(index)] = category
        print("MAX: " + category)
        index += 1
    
    wb.save(filename = 'output.xlsx')
Example #17
0
 def __init__(self):
     subscription_key = "fea767db97b44733bc86bfe710f80be6"
     self.client = WebSearchAPI(
         CognitiveServicesCredentials(subscription_key))
from msrest.authentication import CognitiveServicesCredentials

import os
import sys

# Add your Bing Search V7 subscription key to your environment variables.
if 'BING_SEARCH_V7_KEY' in os.environ:
    SUBSCRIPTION_KEY = os.environ['BING_SEARCH_V7_KEY']
else:
    print("Error: No Bing Search V7 API key set in environment variables")
    # TODO: will want to print out instructions for setting the env variable
    sys.exit()

# Instantiate the client and replace with your endpoint.
client = WebSearchAPI(
    CognitiveServicesCredentials(SUBSCRIPTION_KEY),
    base_url="https://cs3300-bing-api-01.cognitiveservices.azure.com/bing/v7.0"
)

web_data = client.web.search(query="Python Jobs")
print("Searched")
'''
Web pages
If the search response contains web pages, the results' name and url
are printed.
'''
if hasattr(web_data.web_pages, 'value'):

    print("\r\nWebpage Results#{}".format(len(web_data.web_pages.value)))

    for i in range(len(web_data.web_pages.value)):
        web_page = web_data.web_pages.value[i]
Example #19
0
parser.add_argument(dest='results_number',
                    nargs='?',
                    type=int,
                    default=20,
                    help="Number of results returned (max=50)")
parser.add_argument(
    dest='subscription_key_env_name',
    nargs='?',
    default="AZURE_KEY",
    help="Environment variable with Bing Service Subscription Key")

args = parser.parse_args()

subscription_key = os.environ[args.subscription_key_env_name]

client = WebSearchAPI(CognitiveServicesCredentials(subscription_key))

with open(args.queries_list_file, mode='r') as input_file:
    for line in input_file:
        query = line.rstrip()
        print("Querying for: {}".format(query))
        try:
            web_data = client.web.search(query=query,
                                         count=args.results_number)

            if web_data.web_pages.value:
                print("Webpage Results #{}".format(
                    len(web_data.web_pages.value)))

                with open(args.results_file, mode='a') as output_file:
                    for i, result in enumerate(web_data.web_pages.value):
Example #20
0
'''
	REF:	https://docs.microsoft.com/en-us/azure/cognitive-services/bing-web-search/web-sdk-python-quickstart
'''

# Import required modules.
from azure.cognitiveservices.search.websearch import WebSearchAPI
from azure.cognitiveservices.search.websearch.models import SafeSearch
from msrest.authentication import CognitiveServicesCredentials

# Replace with your subscription key.
subscription_key = "05acd6c70fe449d98cfdf8b5866fe055"

# Instantiate the client and replace with your endpoint.
client = WebSearchAPI(CognitiveServicesCredentials(subscription_key), base_url = "https://api.cognitive.microsoft.com/bing/v7.0")

# Make a request. Replace Yosemite if you'd like.
#web_data = client.web.search(query="Yosemite")
web_data = client.web.search(query="RBC Royal Bank")
#print("\r\nSearched for Query# \" Yosemite \"")
print("\r\nSearched for Query# \" RBC Royal Bank \"")

'''
Web pages
If the search response contains web pages, the first result's name and url
are printed.
'''
if hasattr(web_data.web_pages, 'value'):

    print("\r\nWebpage Results#{}".format(len(web_data.web_pages.value)))

    first_web_page = web_data.web_pages.value[0]
Example #21
0
# Import required modules.
from azure.cognitiveservices.search.websearch import WebSearchAPI
from azure.cognitiveservices.search.websearch.models import SafeSearch
from msrest.authentication import CognitiveServicesCredentials

# Replace with your subscription key.
subscription_key = "228f4b3d8e4640e59064122302266549"

# Instantiate the client and replace with your endpoint.
client = WebSearchAPI(
    CognitiveServicesCredentials(subscription_key),
    base_url="https://kardel4.cognitiveservices.azure.com/bing/v7.0")

# Make a request. Replace Yosemite if you'd like.
web_data = client.web.search(
    query=
    "Congrats to Kirstjen Nielsen, our next Secretary of Homeland Security! Well deserved"
)
print("\r\nSearched for Query# \" @Breaking911 Build that wall!!👍 \"")
'''
Web pages
If the search response contains web pages, the first result's name and url
are printed.
'''
if hasattr(web_data.web_pages, 'value'):

    print("\r\nWebpage Results#{}".format(len(web_data.web_pages.value)))

    for web_page in web_data.web_pages.value:
        print("First web page name: {} ".format(web_page.name))
        print("First web page URL: {} ".format(web_page.url))
Example #22
0
def web_results_with_count_and_offset(subscription_key):
	client = WebSearchAPI(CognitiveServicesCredentials(subscription_key))

	try:
    	'''
        Set the query, offset, and count using the SDK's search method. See:
        https://docs.microsoft.com/python/api/azure-cognitiveservices-search-websearch/azure.cognitiveservices.search.websearch.operations.weboperations?view=azure-python.
        '''
        web_data = client.web.search(query="Best restaurants in Seattle", offset=10, count=20)
        print("\r\nSearching for \"Best restaurants in Seattle\"")

        if web_data.web_pages.value:
            '''
            If web pages are available, print the # of responses, and the first and second
            web pages returned.
            '''
            print("Webpage Results#{}".format(len(web_data.web_pages.value)))

            first_web_page = web_data.web_pages.value[0]
            print("First web page name: {} ".format(first_web_page.name))
            print("First web page URL: {} ".format(first_web_page.url))

        else:
            print("Didn't find any web pages...")

	except Exception as err:
    	print("Encountered exception. {}".format(err))

#####################################################################################################################

# Declare the function.
def web_search_with_response_filter(subscription_key):
    client = WebSearchAPI(CognitiveServicesCredentials(subscription_key))
    try:
        '''
        Set the query, response_filter, and freshness using the SDK's search method. See:
        https://docs.microsoft.com/python/api/azure-cognitiveservices-search-websearch/azure.cognitiveservices.search.websearch.operations.weboperations?view=azure-python.
        '''
        web_data = client.web.search(query="xbox",
            response_filter=["News"],
            freshness="Day")
        print("\r\nSearching for \"xbox\" with the response filter set to \"News\" and freshness filter set to \"Day\".")

        '''
        If news articles are available, print the # of responses, and the first and second
        articles returned.
        '''
        if web_data.news.value:

            print("# of news results: {}".format(len(web_data.news.value)))

            first_web_page = web_data.news.value[0]
            print("First article name: {} ".format(first_web_page.name))
            print("First article URL: {} ".format(first_web_page.url))

            print("")

            second_web_page = web_data.news.value[1]
            print("\nSecond article name: {} ".format(second_web_page.name))
            print("Second article URL: {} ".format(second_web_page.url))

        else:
            print("Didn't find any news articles...")

    except Exception as err:
        print("Encountered exception. {}".format(err))

# Call the function.
web_search_with_response_filter(subscription_key)

#####################################################################################################################

# Declare the function.
def web_search_with_answer_count_promote_and_safe_search(subscription_key):

    client = WebSearchAPI(CognitiveServicesCredentials(subscription_key))

    try:
        '''
        Set the query, answer_count, promote, and safe_search parameters using the SDK's search method. See:
        https://docs.microsoft.com/python/api/azure-cognitiveservices-search-websearch/azure.cognitiveservices.search.websearch.operations.weboperations?view=azure-python.
        '''
        web_data = client.web.search(
            query="Niagara Falls",
            answer_count=2,
            promote=["videos"],
            safe_search=SafeSearch.strict  # or directly "Strict"
        )
        print("\r\nSearching for \"Niagara Falls\"")

        '''
        If results are available, print the # of responses, and the first result returned.
        '''
        if web_data.web_pages.value:

            print("Webpage Results#{}".format(len(web_data.web_pages.value)))

            first_web_page = web_data.web_pages.value[0]
            print("First web page name: {} ".format(first_web_page.name))
            print("First web page URL: {} ".format(first_web_page.url))

        else:
            print("Didn't see any Web data..")

    except Exception as err:
        print("Encountered exception. {}".format(err))
Example #23
0
def result_types_lookup(subscription_key):
    """WebSearchResultTypesLookup.

    This will look up a single query (Xbox) and print out name and url for first web, image, news and videos results.
    """
    client = WebSearchAPI(CognitiveServicesCredentials(subscription_key))

    try:

        web_data = client.web.search(query="xbox")
        print("Searched for Query# \" Xbox \"")

        # WebPages
        if web_data.web_pages.value:

            print("Webpage Results#{}".format(len(web_data.web_pages.value)))

            first_web_page = web_data.web_pages.value[0]
            print("First web page name: {} ".format(first_web_page.name))
            print("First web page URL: {} ".format(first_web_page.url))

        else:
            print("Didn't see any Web data..")

        # Images
        if web_data.images.value:

            print("Image Results#{}".format(len(web_data.images.value)))

            first_image = web_data.images.value[0]
            print("First Image name: {} ".format(first_image.name))
            print("First Image URL: {} ".format(first_image.url))

        else:
            print("Didn't see any Image..")

        # News
        if web_data.news.value:

            print("News Results#{}".format(len(web_data.news.value)))

            first_news = web_data.news.value[0]
            print("First News name: {} ".format(first_news.name))
            print("First News URL: {} ".format(first_news.url))

        else:
            print("Didn't see any News..")

        # Videos
        if web_data.videos.value:

            print("Videos Results#{}".format(len(web_data.videos.value)))

            first_video = web_data.videos.value[0]
            print("First Videos name: {} ".format(first_video.name))
            print("First Videos URL: {} ".format(first_video.url))

        else:
            print("Didn't see any Videos..")

    except Exception as err:
        print("Encountered exception. {}".format(err))
import os
from flask import Flask, render_template, request, jsonify
from msrest.authentication import CognitiveServicesCredentials
from azure.cognitiveservices.search.websearch import WebSearchAPI
from page_reader import PageReader

app = Flask(__name__)

SUBSCRIPTION_KEY = os.environ.get("SUBSCRIPTION_KEY", "")
client = WebSearchAPI(CognitiveServicesCredentials(SUBSCRIPTION_KEY))


@app.route("/", methods=["GET"])
def index():
    return render_template("index.html")


def page_to_dict(page):
    return {
        "title": page.name,
        "url": page.url,
        "description": page.snippet,
        "date": page.date_last_crawled
    }


@app.route("/search", methods=["POST"])
def search():
    content = request.get_json()
    query = content["query"]
Example #25
0
# URL image, used as a reference only
# To detect the faces and find the celebrity in this photo, use the Computer Vision service (optional).
query_image_url = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/faces.jpg"

# The name of the celebrity you want to search for on the web.
celebrity_name = 'Bern Collaco'

# Add your Cognitive Services subscription key and endpoint to your environment variables.
subscription_key = os.environ['COGNITIVE_SERVICES_SUBSCRIPTION_KEY']
endpoint = os.environ['COGNITIVE_SERVICES_ENDPOINT']

'''
Authenticate a client. 
'''
web_search_client = WebSearchAPI(CognitiveServicesCredentials(
    subscription_key), endpoint + 'bing/v7.0')

'''
Bing Web Search
Using the name of a celebrity, search for other images of them and return the source URL and image.
This example uses the API calls:
  search()
'''
print()
print("===== Bing Web Search =====")
print("Searching the web for:", celebrity_name)
print()

web_data = web_search_client.web.search(
    query=celebrity_name,  # query search term
    response_filter=['Images'],  # return only images
Example #26
0
# Import required modules.
from azure.cognitiveservices.search.websearch import WebSearchAPI
from azure.cognitiveservices.search.websearch.models import SafeSearch
# from msrest.authentication import CognitiveServicesCredentials
from azure.cognitiveservices.search.imagesearch import ImageSearchAPI
from msrest.authentication import CognitiveServicesCredentials

# Just variables in the accounts.py files that is gitignored
from accounts import subscription_key, endpoint

# Instantiate the client and replace with your endpoint.
client = WebSearchAPI(CognitiveServicesCredentials(subscription_key), base_url = endpoint)

# Images
image_client = ImageSearchAPI(CognitiveServicesCredentials(subscription_key), base_url=endpoint)

# Not used
def get_web_pages(query):
    web_data = client.web.search(query=query)
    return web_data.web_pages

#using bing search api
from azure.cognitiveservices.search.websearch import WebSearchAPI
from azure.cognitiveservices.search.websearch.models import SafeSearch
from msrest.authentication import CognitiveServicesCredentials



subkey = ''
firstTag = 'neural networks'
#declare client
client = WebSearchAPI(CognitiveServicesCredentials(subkey))
web_data = client.web.search(query=firstTag)
print("Searched for Query:  " + firstTag)
if web_data.web_pages.value:
    length = len(web_data.web_pages.value)
    i = 0
    while i < length and i < 6:
        page = web_data.web_pages.value[i]
        title = page.name
        url = page.url
        print(title, url)
        i+=1
Example #28
0
 def __init__(self):
     self.fld = 'kb'
     bing_v7_key = get_api_key('bing_v7')[0]
     self.bing_web_client = WebSearchAPI(
         CognitiveServicesCredentials(bing_v7_key))