Skip to content

Commit d93702e

Browse files
committed
chapter08
1 parent 5812521 commit d93702e

File tree

15 files changed

+228
-0
lines changed

15 files changed

+228
-0
lines changed

crawler/4_css.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from bs4 import BeautifulSoup
2+
from urllib.request import urlopen
3+
4+
html = urlopen(
5+
"https://morvanzhou.github.io/static/scraping/list.html").read().decode('utf-8')
6+
print(html)
7+
8+
print("\n\n找所有 class=month 的信息. 并打印出它们的 tag 内文字.")
9+
soup = BeautifulSoup(html, features="lxml")
10+
month = soup.find_all('li', {"class": "month"})
11+
for m in month:
12+
print(m.get_text())
13+
14+
print("\n\n找到 class=jan 的信息. 然后在 <ul> 下面继续找 <ul> 内部的 <li> 信息.")
15+
jan = soup.find('ul', {"class": "jan"})
16+
jan_s = jan.find_all('li')
17+
for j in jan_s:
18+
print(j.get_text())
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def display_message():
2+
'''这个函数的功能是打印一个句子指出本章的学习内容'''
3+
print("this chapter is about the function of python.")
4+
5+
6+
display_message()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def favorite_book(title):
2+
print("One of my favorite books is "+title.title()+".")
3+
4+
5+
favorite_book("Sophies World")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def make_shirt(size, word):
2+
print("The size of the cloth is "+size +
3+
", the word on shirt is "+word.title()+".")
4+
5+
6+
make_shirt("S","Study")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def make_shirt(size="L", word="I love Python"):
2+
print("The size of the cloth is "+size +
3+
", the words on shirt are "+word+".")
4+
5+
6+
make_shirt()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def describe_city():
2+
pass
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'''
2+
8-6 城市名:编写一个名为 city_country() 的函数,它接受城市的名称及其所属的
3+
国家。这个函数应返回一个格式类似于下面这样的字符串:
4+
"Santiago, Chile"
5+
至少使用三个城市-国家对调用这个函数,并打印它返回的值。
6+
'''
7+
8+
9+
def city_country(city, country):
10+
print(city+","+country)
11+
12+
13+
city_country("NewYork", "USA")
14+
city_country("Paris", "France")
15+
city_country("Shanghai", "China")
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'''
2+
8-7 专辑:编写一个名为 make_album() 的函数,它创建一个描述音乐专辑的字典。
3+
这个函数应接受歌手的名字和专辑名,并返回一个包含这两项信息的字典。使用这个函
4+
数创建三个表示不同专辑的字典,并打印每个返回的值,以核实字典正确地存储了专辑
5+
的信息。
6+
给函数 make_album() 添加一个可选形参,以便能够存储专辑包含的歌曲数。如果调
7+
用这个函数时指定了歌曲数,就将这个值添加到表示专辑的字典中。调用这个函数,并
8+
至少在一次调用中指定专辑包含的歌曲数。
9+
'''
10+
11+
12+
# def make_album(singer, album):
13+
# return {'singer': singer, 'album': album}
14+
15+
16+
# albumA = make_album("singerA", "albumA")
17+
# albumB = make_album("singerB", "albumB")
18+
# albumC = make_album("singerC", "albumC")
19+
# print(albumA,albumB,albumC)
20+
21+
def make_album(singer, album,songs_number=""):
22+
if songs_number:
23+
return {'singer': singer, 'album': album,'songs_number':songs_number}
24+
else:
25+
return {'singer': singer, 'album': album}
26+
27+
albumA = make_album("singerA", "albumA")
28+
albumB = make_album("singerB", "albumB",10)
29+
print(albumA,albumB)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'''
2+
8-8 用户的专辑:在为完成练习 8-7 编写的程序中,编写一个 while 循环,让用户
3+
输入一个专辑的歌手和名称。获取这些信息后,使用它们来调用函数 make_album() ,并
4+
将创建的字典打印出来。在这个 while 循环中,务必要提供退出途径。
5+
'''
6+
7+
8+
def make_album(singer, album, songs_number=""):
9+
if songs_number:
10+
return {'singer': singer, 'album': album, 'songs_number': songs_number}
11+
else:
12+
return {'singer': singer, 'album': album}
13+
14+
15+
while True:
16+
print(60*"#", end="\n")
17+
print("Please enter singer name and album name:(songs number is optional)")
18+
print("You can quit anytime by enter 'q'")
19+
singer = input("singer name:")
20+
if singer == "q":
21+
break
22+
album = input("album name:")
23+
if album == "q":
24+
break
25+
songs_number = input("songs_number:")
26+
if songs_number == "q":
27+
break
28+
print(make_album(singer, album, songs_number))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'''
2+
8-9 魔术师:创建一个包含魔术师名字的列表,并将其传递给一个名为
3+
show_magicians() 的函数,这个函数打印列表中每个魔术师的名字。
4+
'''
5+
6+
7+
def show_magicians(list_of_magicians):
8+
'''show magicians in list'''
9+
for magician in list_of_magicians:
10+
print(magician)
11+
12+
list_of_magicians = ['magicianA','magicianB','magicianC',]
13+
show_magicians(list_of_magicians)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'''
2+
8-10 了不起的魔术师:在你为完成练习 8-9 而编写的程序中,编写一个名为
3+
make_great() 的函数,对魔术师列表进行修改,在每个魔术师的名字中都加入字样“the
4+
Great”。调用函数 show_magicians() ,确认魔术师列表确实变了。
5+
'''
6+
7+
8+
def show_magicians(list_of_magicians):
9+
'''show magicians in list'''
10+
for magician in list_of_magicians:
11+
print(magician)
12+
13+
14+
def make_great(list_of_magicians):
15+
'''make magicians great'''
16+
for n in range(len(list_of_magicians)):
17+
list_of_magicians[n] = "the great " + list_of_magicians[n]
18+
19+
20+
list_of_magicians = ['magicianA', 'magicianB', 'magicianC', ]
21+
make_great(list_of_magicians)
22+
show_magicians(list_of_magicians)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'''
2+
8-11 不变的魔术师:修改你为完成练习 8-10 而编写的程序,在调用函数
3+
make_great() 时,向它传递魔术师列表的副本。由于不想修改原始列表,请返回修改后
4+
的列表,并将其存储到另一个列表中。分别使用这两个列表来调用 show_magicians() ,
5+
确认一个列表包含的是原来的魔术师名字,而另一个列表包含的是添加了字样“the
6+
Great”的魔术师名字
7+
'''
8+
def show_magicians(list_of_magicians):
9+
'''show magicians in list'''
10+
for magician in list_of_magicians:
11+
print(magician)
12+
13+
14+
def make_great(list_of_magicians):
15+
'''make magicians great'''
16+
great_magicians = list_of_magicians
17+
for n in range(len(great_magicians)):
18+
list_of_magicians[n] = "the great " + great_magicians[n]
19+
return great_magicians
20+
21+
22+
list_of_magicians = ['magicianA', 'magicianB', 'magicianC', ]
23+
show_magicians(make_great(list_of_magicians[:]))
24+
show_magicians(list_of_magicians)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'''
2+
8-12 三明治:编写一个函数,它接受顾客要在三明治中添加的一系列食材。这个
3+
函数只有一个形参(它收集函数调用中提供的所有食材),并打印一条消息,对顾客点
4+
的三明治进行概述。调用这个函数三次,每次都提供不同数量的实参。
5+
'''
6+
def make_sanswitch(*toppings):
7+
"""概述要制作的三明治"""
8+
print("Making a sandswitch with the following toppings:")
9+
for topping in toppings:
10+
print("- " + topping)
11+
12+
make_sanswitch("A","B","C","D")
13+
make_sanswitch("a","b","c")
14+
make_sanswitch("1","2")
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
8-13 用户简介:复制前面的程序 user_profile.py ,在其中调用 build_profile() 来
3+
创建有关你的简介;调用这个函数时,指定你的名和姓,以及三个描述你的键-值对。
4+
'''
5+
6+
7+
def build_profile(first, last, **user_info):
8+
"""创建一个字典,其中包含我们知道的有关用户的一切"""
9+
profile = {}
10+
profile['first_name'] = first
11+
profile['last_name'] = last
12+
for key, value in user_info.items():
13+
profile[key] = value
14+
return profile
15+
16+
17+
user_profile = build_profile(
18+
'albert', 'einstein', location='princeton', field='physics')
19+
print(user_profile)
20+
pass
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
8-14 汽车:编写一个函数,将一辆汽车的信息存储在一个字典中。这个函数总是接
3+
受制造商和型号,还接受任意数量的关键字实参。这样调用这个函数:提供必不可少的
4+
信息,以及两个名称 — 值对,如颜色和选装配件。这个函数必须能够像下面这样进行调用:
5+
car = make_car('subaru', 'outback', color='blue', tow_package=True)
6+
打印返回的字典,确认正确地处理了所有的信息。
7+
'''
8+
9+
10+
def make_car(brand, model, **other):
11+
profile = {}
12+
profile['brand'] = brand
13+
profile['model'] = model
14+
for key, value in other.items():
15+
profile[key] = value
16+
return profile
17+
18+
19+
car_info = make_car('subaru', 'outback', color='blue', tow_package=True)
20+
print(car_info)

0 commit comments

Comments
 (0)