Quiz 14_2-2: Using Web Services | Python for Everybody 配套练习_解题记录


Python for Everybody


课程简介

Python for Everybody 零基础程序设计(Python 入门)

  • This course aims to teach everyone the basics of programming computers using Python. 本课程旨在向所有人传授使用 Python 进行计算机编程的基础知识。
  • We cover the basics of how one constructs a program from a series of simple instructions in Python. 我们介绍了如何通过 Python 中的一系列简单指令构建程序的基础知识。
  • The course has no pre-requisites and avoids all but the simplest mathematics. Anyone with moderate computer experience should be able to master the materials in this course. 该课程没有任何先决条件,除了最简单的数学之外,避免了所有内容。任何具有中等计算机经验的人都应该能够掌握本课程中的材料。
  • This course will cover Chapters 1-5 of the textbook “Python for Everybody”. Once a student completes this course, they will be ready to take more advanced programming courses. 本课程将涵盖《Python for Everyday》教科书的第 1-5 章。学生完成本课程后,他们将准备好学习更高级的编程课程。
  • This course covers Python 3.

在这里插入图片描述

coursera

Python for Everybody 零基础程序设计(Python 入门)

Charles Russell Severance
Clinical Professor

在这里插入图片描述

个人主页
Twitter

在这里插入图片描述

University of Michigan


课程资源

coursera原版课程视频
coursera原版视频-中英文精校字幕-B站
Dr. Chuck官方翻录版视频-机器翻译字幕-B站

PY4E-课程配套练习
Dr. Chuck Online - 系列课程开源官网



Quiz 14_2-2: Using Web Services

Web services allow a program to access data available in a different server.


单选题(1-15)

  1. Who is credited with getting the JSON movement started?
  • Mitchell Baker
  • Douglas Crockford
  • Bjarne Stroustrup
  • Pooja Sankar
  1. Who is credited with the REST approach to web services?
  • Roy Fielding
  • Daphne Koller
  • Vint Cerf
  • Bjarne Stroustrup
  • Leonard Klienrock
  1. What Python library do you have to import to parse and handle JSON?
  • import json
  • import re
  • ElementTree
  • BeautifulSoup
  1. Which of the following is true about an API?
  • An API defines the header bits in the first 8 bits of all IP packets
  • An API is a contract that defines how to use a software library
  • An API keeps servers running even when the power is off
  • An API defines the pin-outs for the USB connectors
  1. What is the method used to parse a string containing JSON data so that you can work with the data in Python?
  • json.connect()
  • json.parse()
  • json.loads()
  • json.read()
  1. Which of the following is a web services approach used by the Twitter API?
  • CORBA
  • REST
  • SOAP
  • XML-RPC
  1. What kind of variable will you get in Python when the following JSON is parsed:
[ "Glenn", "Sally", "Jen" ]
  • A dictionary with three key / value pairs
  • Three tuples
  • A dictionary with one key / value pair
  • One Tuple
  • A list with three items
  1. What kind of variable will you get in Python when the following JSON is parsed:
{ "id" : "001",
  "x" : "2",
  "name" : "Chuck"
}
  • A dictionary with three key / value pairs
  • A tuple with three items
  • A list with three items
  • A list with six items
  • A list of tuples
  1. Which of the following is not true about the service-oriented approach?
  • An application runs together all in one place
  • An application makes use of the services provided by other applications
  • Standards are developed where many pairs of applications must work together
  • Web services and APIs are used to transfer data between applications
  1. If the following JSON were parsed and put into the variable x,
{
    "users": [
        {
            "status": {
                "text": "@jazzychad I just bought one .__.",
             },
             "location": "San Francisco, California",
             "screen_name": "leahculver",
             "name": "Leah Culver",
         },
   ...

what Python code would extract “Leah Culver” from the JSON?

  • x[0][“name”]
  • x[“users”][0][“name”]
  • x[“users”][“name”]
  • x[“name”]
  1. Which of these two web service approaches is preferred in most modern service-oriented applications?
  • REST - Representational state transfer
  • SOAP - Simple Object Access Protocol
  1. What library call do you make to append properly encoded parameters to the end of a URL like the following:
http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=Ann+Arbor%2C+MI
  • re.encode()
  • re.match()
  • urllib.parse.urlencode()
  • urllib.urlcat()
  1. What happens when you exceed the Google geocoding API rate limit?
  • The API starts to perform very slowly
  • You cannot use the API for 24 hours
  • You canot use the API until you respond to an email that contains a survey question
  • Your application starts to perform very slowly
  1. What protocol does Twitter use to protect its API?
  • SHA1-MD5
  • SOAP
  • Java Web Tokens
  • PKI-HMAC
  • WS*Security
  • OAuth
  1. What header does Twitter use to tell you how many more API requests you can make before you will be rate limited?
  • x-request-count-down
  • content-type
  • x-max-requests
  • x-rate-limit-remaining

操作题

Autograder 1: Extract Data from JSON

In this assignment you will write a Python program somewhat similar to http://www.py4e.com/code3/json2.py. The program will prompt for a URL, read the JSON data from that URL using urllib and then parse and extract the comment counts from the JSON data, compute the sum of the numbers in the file and enter the sum below:

We provide two files for this assignment. One is a sample file where we give you the sum for your testing and the other is the actual data you need to process for the assignment.

You do not need to save these files to your folder since your program will read the data directly from the URL. Note: Each student will have a distinct data url for the assignment - so only use your own data url for analysis.

Data Format
The data consists of a number of names and comment counts in JSON as follows:

{
  comments: [
    {
      name: "Matthias"
      count: 97
    },
    {
      name: "Geomer"
      count: 97
    }
    ...
  ]
}

The closest sample code that shows how to parse JSON and extract a list is json2.py. You might also want to look at geoxml.py to see how to prompt for a URL and retrieve data from a URL.

Sample Execution

$ python3 solution.py
Enter location: http://py4e-data.dr-chuck.net/comments_42.json
Retrieving http://py4e-data.dr-chuck.net/comments_42.json
Retrieved 2733 characters
Count: 50
Sum: 2...

Autograder 2: Calling a JSON API

In this assignment you will write a Python program somewhat similar to http://www.py4e.com/code3/geojson.py. The program will prompt for a location, contact a web service and retrieve JSON for the web service and parse that data, and retrieve the first place_id from the JSON. A place ID is a textual identifier that uniquely identifies a place as within Google Maps.

API End Points

To complete this assignment, you should use this API endpoint that has a static subset of the Google Data:

http://py4e-data.dr-chuck.net/json?

This API uses the same parameter (address) as the Google API. This API also has no rate limit so you can test as often as you like. If you visit the URL with no parameters, you get “No address…” response.

To call the API, you need to include a key= parameter and provide the address that you are requesting as the address= parameter that is properly URL encoded using the urllib.parse.urlencode() function as shown in http://www.py4e.com/code3/geojson.py

Make sure to check that your code is using the API endpoint as shown above. You will get different results from the geojson and json endpoints so make sure you are using the same end point as this autograder is using.

Test Data / Sample Execution

You can test to see if your program is working with a location of “South Federal University” which will have a place_id of “ChIJNeHD4p-540AR2Q0_ZjwmKJ8”.

$ python3 solution.py
Enter location: South Federal University
Retrieving http://...
Retrieved 2453 characters
Place id ChIJNeHD4p-540AR2Q0_ZjwmKJ8

Turn In

Please run your program to find the place_id for this location:

University of Greifswald

Make sure to retreive the data from the URL specified above and not the normal Google API. Your program should work with the Google API - but the place_id may not match for this assignment.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冰.封万里

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值