API Requests and Web Scraping Quiz
10 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What does the get_question function do?

  • It searches for questions on Quora related to a given query.
  • It returns the top Quora posts based on the number of votes and views.
  • It sends a request to the Google Custom Search API to retrieve search results.
  • It retrieves the title of a question from the Brainly API based on a given query. (correct)
  • What is the purpose of the search function?

  • It retrieves the top Quora posts based on votes and views for a given term.
  • It searches for questions on Quora related to a given term.
  • It constructs a URL with the given query and sends a request to the Brainly API.
  • It sends a request to the Google Custom Search API to retrieve search results for a given term. (correct)
  • What is the purpose of the search_quora function?

  • It returns the top Quora posts based on the number of votes and views.
  • It retrieves the title of a question from the Brainly API based on a given query.
  • It sends a request to the Google Custom Search API to retrieve search results.
  • It searches for questions on Quora related to a given query and returns the items. (correct)
  • What is the purpose of the re.sub(r'\s+', "+", q) expression in the get_question function?

    <p>It removes all whitespace characters from the query string and replaces them with '+' characters.</p> Signup and view all the answers

    What does the top_quora_posts function do?

    <p>It returns the top Quora posts based on the number of votes and views, up to a specified limit.</p> Signup and view all the answers

    What is the purpose of the startTime variable in the search function?

    <p>It represents the time when the search request is sent to the Google Custom Search API.</p> Signup and view all the answers

    What is the purpose of the headers dictionary in the get_question and search_quora functions?

    <p>It contains the headers to be included in the HTTP request, such as the User-Agent.</p> Signup and view all the answers

    What is the purpose of the try-except blocks in the functions?

    <p>They are used to handle exceptions that may occur during the API requests.</p> Signup and view all the answers

    What is the purpose of the params dictionary in the search_quora function?

    <p>It contains the parameters to be included in the API request, such as the page number and limit.</p> Signup and view all the answers

    What is the purpose of the if 'data' in response condition in the search_quora function?

    <p>It checks if the API response contains valid data before processing it.</p> Signup and view all the answers

    Study Notes

    import re
    from typing import List
    from datetime import datetime
    from collections import Counter
    import math
    ## Import requests library
    import requests
    
    def get_question(q):
        # Create URL with q parameter q=<question>
        url = 'http://api.brainly.co/api/questions/v2/explore'
        params = {
            'page': 1,
            'limit': 1,
            'q': re.sub(r'\s+', "+", q)
        }
        headers = {'User-Agent': 'Mozilla/5.0'}
    
        try:
            r = requests.get(url, params=params, headers=headers)
            response = r.json()
            if len(response['data']) > 0:
                return response['data']['title']
        except Exception as e:
            print("Error:", str(e))
        return ""
    
    def search(term, count=1):
        query = term + "+site:" + "edu"
        url = "https://www.googleapis.com/customsearch/v1"
        service_key = "YOUR_API_KEY"  # Provide your API key here
        startTime = datetime.now()  # Get current timestamp
        payload = {
            'key': service_key,
            'cx': '',
            'q': query,
            'num': count,
            'starttime': int(startTime.timestamp())
        }
        headers = {
            "Accept": "application/json; utf-8",
            "Accept-Charset": "ISO-8859-1",
            "Accept-Language": "en",
        }
        try:
            r = requests.get(url, params=payload, headers=headers)
            response = r.json()
            if 'items' in response:
                return response['items']
            else:
                raise AttributeError("Items not found")
        except Exception as e:
            print("Search error:", str(e))
        return []
    
    def search_quora(q):
        url = 'http://api.brainly.co/questions/search/v3'
        query = q + "+site:*+edu"
        params = {
            'page': 1,
            'limit': 10,
            'query': query
        }
        headers = {'User-Agent': 'Mozilla/5.0', 'Content-Type': 'application/x-www-form-urlencoded'}
    
        try:
            r = requests.post(url, params=params, headers=headers)
            response = r.json()
            if 'data' in response:
                return response['data']['items']
        except Exception as e:
            print("Quora error:", str(e))
        return []
    
    def top_quora_posts(posts, limit=10):
        result_dict = {}
        for post in posts:
            title = post['name']
            author = post['author']
            votes = post['stats']['votes']
            views = post['stats']['views']
            last_activity = post['last_activity_date']
            total_information = views + votes
            result_dict[title] = [author, votes, total_information]
    
        sorted_result_dict = dict(sorted(result_dict.items(), key=lambda item: item, reverse=True))
        top_keys = list(sorted_result_dict.keys())[:limit]
        top_values = [sorted_result_dict[key] for key in top_keys]
        return top_values
    
    def search(term, count=1):
        query = term + "+site:*+edu"
        url = "https://www.googleapis.com/customsearch/v1"
        service_key = "YOUR_API_KEY"  # Provide your API key here
        startTime = datetime.now()  # Get current timestamp
        payload = {
            'key': service_key,
            'cx': '',
            'q': query,
            'num': count,
            'starttime': int(startTime.timestamp())
        }
        headers = {
            "Accept": "application/json; utf-8",
            "Accept-Charset": "ISO-8859-1",
            "Accept-Language": "en",
        }
        try:
            r = requests.get(url, params=payload, headers=headers)
            response = r.json()
            if 'items' in response:
                return response['items']
            else:
                raise AttributeError("Items not found")
        except Exception as e:
            print("Search error:", str(e))
        return []
    
    def search_quora(q):
        url = 'http://api.brainly.co/questions/search/v3'
        query = q + "+site:*+edu"
        params = {
            'page': 1,
            'limit': 10,
            'query': query
        }
        headers = {'User-Agent': 'Mozilla/5.0', 'Content-Type': 'application/x-www-form-urlencoded'}
    
        try:
            r = requests.post(url, params=params, headers=headers)
            response = r.json()
            if 'data' in response:
                return response['data']['items']
        except Exception as e:
            print("Quora error:", str(e))
        return []
    

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Description

    Test your knowledge on making API requests and web scraping using Python. The quiz covers topics such as utilizing the 'requests' library, handling JSON responses, and scraping data from websites. Challenge yourself with questions on fetching online content programmatically!

    More Like This

    Python API Requests
    10 questions

    Python API Requests

    RadiantCatSEye6630 avatar
    RadiantCatSEye6630
    What is RESTful API?
    14 questions

    What is RESTful API?

    SelectiveMilkyWay avatar
    SelectiveMilkyWay
    Weather Data Retrieval and Conversion
    16 questions
    Use Quizgecko on...
    Browser
    Browser