本文通过实例练习Python基础语法, python版本2.7
# -*- coding: utf-8 -*- import randomimport re import requests from bs4 import BeautifulSoup
# 爬取糗事百科的首页内容 def qiushibaike():content = requests.get('http://www.qiushibaike.com/').contentsoup = BeautifulSoup(content, 'html.parser')# print(soup.get_text())for div in soup.find_all('div', {'class': 'foot'}):print(div.text.split())# for div in soup.find_all('div', {'class': 'content'}):
# 字符串常见处理方法 def demo_string():stra = 'hello world'print(stra.capitalize())print(stra.replace('world', 'victor'))strb = ' \n\rhello world \r\n'print(0, strb)print(1, strb.lstrip())print(2, strb.rstrip(), 'xx')strc = 'hello w'print(3, strc.startswith('hel'))print(4, strc.endswith('w'))print(5, stra + strb + strc)print(6, len(stra), len(strb), len(strc))print(7, '-'.join(['a', 'b', 'c'])) # a-b-cprint(8, strc.split(' '))print(9, strc.find('llo'))
# 运算符操作 def demo_operation():print(1, 1 + 2, 5 / 2, 5 * 2, 5 - 2)print(2, 1 + 2, 5.0 / 2, 5 * 2, 5 - 2)print(3, True, not True, False, not False)print(4, 1 << 2, 88 >> 2)print(5, 1 < 2, 5 > 3)print(6, 5 & 3, 5 ^ 3, 5 | 3)x = 3y = 5.0print(7, x, y, type(x), type(y))
# 函数 def demo_buildinfunction():print(1, max(1, 2), min(5, 3))print(2, len('xxx'), len([3, 4, 5]))print(3, abs(-2), abs(7))print(4, range(1, 10, 2))# print(5, '\n'.join(dir(list)))x = 2print(6, eval('x+3'))print(7, chr(65), ord('a'))print(8, divmod(11, 3))
# 控制流 def demo_controlflow():score = 65;if score > 99:print(1, 'A')elif score > 60 & score <= 99:print(2, 'B')else:print(3, 'C')while score < 100:print(4, score)score += 10if score > 80:breakfor i in range(0, 10):if i == 0:passif i == 3:continueif i < 5:print(5, i*i)if i == 7:break
# 列表 def demo_list():lista = [1, 2, 3]print(1, lista)# print(dir(list))listb = ['a', 1, 1.0, 4, 2]print(2, listb)lista.extend(listb)print(3, lista)print(4, len(lista))print(5, 'a' in lista, 'b' in listb)lista += listbprint(lista)listb.insert(0, 'www')print(listb)listb.pop(1)print(listb)# listb.sort()print(listb)print(6, listb[0], listb[1], listb[2])print(7, [0] * 10)print(8, listb * 2)listb.reverse()print(9, listb)t = (1, 2, 3,1)print(10, t)# print(11, t.count())print(11, t.count(1), len(t))
# 字典 def demo_dict():dicta = {4 : 16, 1 : 1, 2 : 4, 3 : 9, 'a' : 'b'}print(1, dicta)print(2, dicta.keys(), dicta.values())for key, value, in dicta.items():print(key, value)for key in dicta.keys():print(key)# 2.0版本是has_keys()方法print(3, dicta.__contains__(1), dicta.__contains__(11))dictb = {'+': add, '-': sub}print(4, dictb.get('-')(5, 3))print(4, dictb['+'](1, 2))print(5, dictb.__contains__('+'))del dictb['+']print(5, dictb.__contains__('+'))print(5, dictb)dictb.pop('-')print(6, dictb)dictb['x'] = 'y'print(7, dictb)def add(a, b):return a + bdef sub(a, b):return a - b# 集合 def demo_set():lista = [1, 2, 3]lista1 = (1, 2, 3)seta = set(lista)seta1 = set(lista1)print(1, seta)print(1, seta1)setb = set((2, 3, 4))print(2, seta.intersection(setb))print(3, seta & setb)print(4, seta | setb, seta.union(setb))print(5, seta - setb, setb - seta)seta.add('xxx')print(6, seta)print(7, len(seta))print(8, seta.isdisjoint(set((1, 2))))print(9, 1 in seta)
# 类 class User:type = 'USER'def __init__(self, name, uid):self.name = nameself.uid = uiddef __repr__(self):# toStirng()return 'i am ' + self.name + ' ' + str(self.uid) + ' ' + self.type# 继承 class Guest(User):type = 'GUEST'def __repr__(self):return "I am guest: " + self.name + ' ' + str(self.uid) + ' ' + self.typeclass Admin(User):type = 'ADMIN'def __init__(self, name, uid, group):User.__init__(self,name, uid)self.group = groupdef __repr__(self):return 'I am admin: ' + self.name + ' ' + str(self.uid) + ' ' + self.group + ' ' + self.typedef create_user(type):if type == 'USER':return User('u1', 1)elif type == 'ADMIN':return Admin('a1', 1, 'shu')else:return Guest('g1', 1)
# 异常 def demo_exception():try:print(2/1)# print(2/0)raise Exception('Raise Error', 'xxx')except Exception as e:print('error', e)finally:print('clean up')def demo_obj():user1 = User('victor', 1)print(user1.__repr__())print(user1)guest = Guest('xunuo', 2)print(guest)admin = Admin('Qingde', 2, 'shanghai university')print(admin)print(create_user('ADMIN'))def demo_random():random.seed(11)for i in range(0, 5):print(1, random.randint(0,100))print(2, int(random.random()*100))print(3, random.choice(range(0, 100, 5))) # 抽奖print(4, random.sample(range(0, 100, 10), 4)) # 抽几个 lista = [1, 2, 3, 4, 5]random.shuffle(lista)print(lista)def demo_regex():str = 'abc123def12gh15'p1 = re.compile('[\d]+')p2 = re.compile('\d')print(1, p1.findall(str))print(2, p2.findall(str))str1 = 'axxx@163.com, bsad@google.com, cdd@qq.com, dasd@qq.com, eda@163.com'p3 = re.compile('[\w]+@[163|qq]+\.com') # 不能有空格print(3, p3.findall(str1))str = '<html><h>title</h><body>content</body></html>'p4 = re.compile('<h>[^<]+</h>')print(4, p4.findall(str))p4 = re.compile('<h>[^<]+</h><body>[^<]+</body>')print(5, p4.findall(str))str = 'xx2017-06-06zzz'p5 = re.compile('\d{4}-\d{2}-\d{2}')print(6, p5.findall(str))if __name__ == '__main__':# print('hello world') qiushibaike()# demo_string()# demo_operation()# demo_buildinfunction()# demo_controlflow()# demo_list()# demo_dict()# demo_set()# demo_obj()# demo_exception()# demo_random()# demo_regex()