文章目录
- Python for Everybody
- 课程简介
- Quiz 13: Network Programming
- 单选题(1-11)
- 操作题
- Autograder 1: Request-Response Cycle
- Autograder 2: Scraping HTML Data with BeautifulSoup
- Autograder 3: Following Links with BeautifulSoup
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 13: Network Programming
We take a quick look at how data moves across the network using the HyperText Transport Protocol (HTTP) and how we write programs to read data across the network.
单选题(1-11)
- What do we call it when a browser uses the HTTP protocol to load a file or page from a server and display it in the browser?
- SMTP
- The Request/Response Cycle
- DECNET
- Internet Protocol (IP)
- IMAP
- What separates the HTTP headers from the body of the HTTP document?
- Four dashes
- X-End-Header: true
- A blank line
- A less-than sign indicating the start of an HTML tag
- Which of the following is most similar to a TCP port number?
- A telephone number
- A street number in an address
- A telephone extension
- The distance between two locations
- The GPS coordinates of a building
- What must you do in Python before opening a socket?
- import tcp-socket
- _socket = true
- import socket
- import tcp
- open socket
- In a client-server application on the web using sockets, which must come up first?
- it does not matter
- client
- server
- Which of the following TCP sockets is most commonly used for the web protocol (HTTP)?
- 80
- 22
- 119
- 25
- 23
- Which of the following is most like an open socket in an application?
- The ringer on a telephone
- An “in-progress” phone conversation
- Fiber optic cables
- The chain on a bicycle
- The wheels on an automobile
- What does the “H” of HTTP stand for?
- Hyperspeed
- wHolsitic
- Manual
- HyperText
- Simple
- What is an important aspect of an Application Layer protocol like HTTP?
- How long do we wait before packets are retransmitted?
- What is the IP address for a domain like www.dr-chuck.com?
- How much memory does the server need to serve requests?
- Which application talks first? The client or server?
- What are the three parts of this URL (Uniform Resource Locator)?
http://www.dr-chuck.com/page1.htm
- Host, offset, and page
- Page, offset, and count
- Protocol, host, and document
- Protocol, document, and offset
- Document, page, and protocol
- When you click on an anchor tag in a web page like below, what HTTP request is sent to the server?
<p>Please click <a href="page1.htm">here</a>.</p>
- PUT
- INFO
- POST
- GET
- DELETE
操作题
Autograder 1: Request-Response Cycle
Exploring the HyperText Transport Protocol
You are to retrieve the following document using the HTTP protocol in a way that you can examine the HTTP Response headers.
- http://data.pr4e.org/intro-short.txt
There are three ways that you might retrieve this web page and look at the response headers:
- Preferred: Modify the socket1.py program to retrieve the above URL and print out the headers and data. Make sure to change the code to retrieve the above URL - the values are different for each URL.
- Open the URL in a web browser with a developer console or FireBug and manually examine the headers that are returned.
Autograder 2: Scraping HTML Data with BeautifulSoup
Scraping Numbers from HTML using BeautifulSoup In this assignment you will write a Python program similar to http://www.py4e.com/code3/urllink2.py. The program will use urllib to read the HTML from the data files below, and parse the data, extracting numbers and compute the sum of the numbers in the file.
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.
- Sample data: http://py4e-data.dr-chuck.net/comments_42.html
(Sum=2553) - Actual data: http://py4e-data.dr-chuck.net/comments_1577743.html
(Sum ends with 26)
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 file is a table of names and comment counts. You can ignore most of the data in the file except for lines like the following:
<tr><td>Modu</td><td><span class="comments">90</span></td></tr>
<tr><td>Kenzie</td><td><span class="comments">88</span></td></tr>
<tr><td>Hubert</td><td><span class="comments">87</span></td></tr>
You are to find all the <span>
tags in the file and pull out the numbers from the tag and sum the numbers.
Look at the sample code provided. It shows how to find all of a certain kind of tag, loop through the tags and extract the various aspects of the tags.
...
# Retrieve all of the anchor tags
tags = soup('a')
for tag in tags:
# Look at the parts of a tag
print 'TAG:',tag
print 'URL:',tag.get('href', None)
print 'Contents:',tag.contents[0]
print 'Attrs:',tag.attrs
You need to adjust this code to look for span tags and pull out the text content of the span tag, convert them to integers and add them up to complete the assignment.
Sample Execution
$ python3 solution.py
Enter - http://py4e-data.dr-chuck.net/comments_42.html
Count 50
Sum 2...
Autograder 3: Following Links with BeautifulSoup
Following Links in Python
In this assignment you will write a Python program that expands on http://www.py4e.com/code3/urllinks.py. The program will use urllib to read the HTML from the data files below, extract the href= vaues from the anchor tags, scan for a tag that is in a particular position relative to the first name in the list, follow that link and repeat the process a number of times and report the last name you find.
We provide two files for this assignment. One is a sample file where we give you the name for your testing and the other is the actual data you need to process for the assignment
- Sample problem: Start at http://py4e-data.dr-chuck.net/known_by_Fikret.html
Find the link at position 3 (the first name is 1). Follow that link. Repeat this process 4 times. The answer is the last name that you retrieve.
Sequence of names: Fikret Montgomery Mhairade Butchi Anayah
Last name in sequence: Anayah - Actual problem: Start at: http://py4e-data.dr-chuck.net/known_by_Dyllan.html
Find the link at position 18 (the first name is 1). Follow that link. Repeat this process 7 times. The answer is the last name that you retrieve.
Hint: The first character of the name of the last page that you will load is: R
Strategy
The web pages tweak the height between the links and hide the page after a few seconds to make it difficult for you to do the assignment without writing a Python program. But frankly with a little effort and patience you can overcome these attempts to make it a little harder to complete the assignment without writing a Python program. But that is not the point. The point is to write a clever Python program to solve the program.
Sample execution
Here is a sample execution of a solution:
$ python3 solution.py
Enter URL: http://py4e-data.dr-chuck.net/known_by_Fikret.html
Enter count: 4
Enter position: 3
Retrieving: http://py4e-data.dr-chuck.net/known_by_Fikret.html
Retrieving: http://py4e-data.dr-chuck.net/known_by_Montgomery.html
Retrieving: http://py4e-data.dr-chuck.net/known_by_Mhairade.html
Retrieving: http://py4e-data.dr-chuck.net/known_by_Butchi.html
Retrieving: http://py4e-data.dr-chuck.net/known_by_Anayah.html
The answer to the assignment for this execution is “Anayah”.