代码目录

renturn top

renturn down

1、斐波那契数列的实现

2、猜年龄(code)

3、求最大值和最小值

4、打印出一个九九乘法表:

5、求最大公约数的个数

6、一个购物车程序:

7、购物车1.0

8、 输出一个矩形

9、超级low的一个登陆接口

10、for ... else 的实例

11、凯撒秘密逻辑代码

12、给定两个正整数a和b,将该小数点后第x位到第y位的数字当做密码

13、三级菜单

14、在文件中的某一行后面加上一段字符串

15、对一个文件中的内容进行修改

16、若为py数,输出‘yes’,否则输出‘no’

17、将日志写入文件的函数

18、斐波那契简单的实现列表的实现方式

19、装饰器函数实例

20、用装饰器写的一个登录页面程序

21、随机数模块生成随机验证码

22、实现日志写入的函数

22、计算机的最low实现(求改)

23、面向对象编程,实现页面分页(属性)

24、利用字符串调用模块中的函数

25、b站主播间狂刷弹幕

25、socket实现不间断通信

26、socket执行cmd命令

27、爬取英雄联盟的皮肤

28、文件上传

30、ftp实现

31. 对一个需要请求头的网址进行爬取

32. 使用代理服务器实现网页的爬取

33. 爬取网页时实现日志输出

34.字符画

35.微信控制电脑

36.爬取京东上所有的手机的图片

37.获取某个网址中的所有link

38.爬取糗事百科的所有笑话

39.使用代理服务器

40.爬取微信文章

41.随机考察单词(窗口版)

斐波那契数列的实现

第一种方法:

 def fibs(bit):           # fibs numbers functionbefore, after = 0, 1for i in range(bit):print(before)before, after = after, before + afterfibs(10)

第二种方法:

fibs_list = [0, 1]for i in range(10-2):         # 取出前十位斐波那契数fibs_list.append(fibs_list[-1]+fibs_list[-2])
for fibs_num in fibs_list:print(fibs_num)

2、猜年龄(code)

flag = True
age = 30while flag == True:guess =int(input('your guess age:'))if guess == age:print('you right!')flag = Falseelse:print('your is error!')if guess < age:print('maybe is small!')elif guess > age:print('maybe is big!')

3、求最大值和最小值(支持用户输入数字)

def num_list():   #将输入的数字,存入一个列表num_list_a = []flag = Truewhile flag == True:your_choice = input('exit:')if your_choice == 'yes':flag = Falseelse:num = int(input('>>:'))num_list_a.append(num)return num_list_a
def max_min_num(a):   #求出最大值和最小值max = 0min = a[0]for i in a:if i < min:min = iif i > max:max = iprint('the max number :',max)print('the min number :',min)return
max_min_num(num_list())

4、打印出一个九九乘法表:
for i in range(1,10):for j in range(1,i+1):     # 对于九九乘法表要注意他的每行,是第一个数在递增print('%d * %d = %d' % (j, i, i*j), end='    ')     # end的方法是将在本次循环中的输出的所有内容在一行输出print()   # 相当于一个换行符的作用

5、求最大公约数的个数

a = int(input('a = '))
b = int(input('b = '))def times(x,y):  timess = 0  if x > y:  max = x+1else:max = y+1for i in range(1,max):if x % i == 0 and y % i == 0:timess += 1return timess
print(times(a,b))

6、一个购物车程序:

这是一个简单的购物程序

实现的功能有:

1.自定义商品 在shopping_list 中
2.可以实时退出
3.可以输入你有多少钱
4.有 是否确认支付的片段
5.可以循环购买

 
shopping_list = {'apple':10,'banana':20,'book':50}def show_shopping():   #  shopping shown = 0print('----------------shopping store----------------\n''welcome shopping store!!!!\n''this is my store all shopping!\n''----------------------------------------------')for shopping in shopping_list:print('%d  --->  %s,%d' % (n,shopping,shopping_list[shopping]))n+=1
# show_shopping()   the test
# your_all_money -= shopping_list[your_choice]
# print('you have %d yuan now, and you purchase one %d' % (your_all_money,your_choice))# 需要再次购买
def buy_again():global flagbuy_again = input('you want to buy again:')if buy_again == 'yes':show_shopping()main()else:print('wlcome again!')flag = Falsereturn flag
# 主函数
def main():# 调用全局变量global your_all_moneyglobal flagwhile flag == True:exit = input('exit now:')if exit == 'yes':print('welcome again')flag = Falseelse:show_shopping()   # showyour_choice= input('please enter you want to buy commodity:')if your_choice in shopping_list:print('you need pay %d yuan !!' % shopping_list[your_choice])pay_choice = input('make sure to pay:')if pay_choice == 'yes':your_all_money -= shopping_list[your_choice]print('you have %d yuan now, and you purchase one %s' % (your_all_money,your_choice))flag = buy_again()else:flag = buy_again()else:print('please enter again!')main()
flag = True    # 控制直接退出程序
your_all_money = int(input('your all money:'))
main()

7、购物车1.0
shopping_dict = {'apple': 10,          # 商品列表'iphone': 1000,'book': 100}def show_shopping():   #展示商品的函数print('shopping list'.center(50, '-'))for i in shopping_dict:price = shopping_dict[i]print(i, price)print('End'.center(50, '-'))flag = True   #  控制退出的标志位
shopping_car = {}  # 购物车
times = 0    # 控制输入总金额的次数,只需要一次def pay(x):    # 支付函数global salaryshopping_money = 0    # 总价格初始化# is_pay = input('pay now?(yes/no)')# if is_pay == 'yes':for i in x:shopping_money += x[i]print('you have pay %d yuan' % shopping_money)salary -= shopping_moneyprint('you have %d yuan now!!' % salary)          # 打印剩下的钱while flag:show_shopping()if times == 0:salary = int(input('how much money do you have?'))leave = input('Whether or not to exit the program?(yes/no)')if leave == 'yes':print('welcome in again!!!')flag = Falseelse:your_choice = input('What products do you want to buy?')if your_choice in shopping_dict:price = shopping_dict[your_choice]if salary < price:                      # 余额不足有提示,缺多少钱print('you are need %d yuan' % (price-salary))else:print('%s need %d yuan' % (your_choice, price))shopping_car[your_choice] = price     # 将未支付的商品,加在购物车中is_pay = input('pay now?(yes/no)')if is_pay == 'yes':pay(shopping_car)else:print('continue shopping!')else:print('%s maybe no this thing!' % your_choice)times += 1

8、 输出一个矩形
long = int(input('enter long:'))
width = int(input('enter width:'))
def rectangle(x, y):for i in range(y):for j in range(x):print('#', end='')print()
rectangle(long, width)

9、超级low的一个登陆接口
1,实现若一次性输入错误三次密码,将会锁定1,由于水平太low所以在最开始,需要输入两次用户名,而且必须保持一样
import sysu = 0
usename = input('your name is :')
while u < 3:usename = input('your name is :')with open('lock', 'r') as lock:lock_list = lock.read() if usename in lock_list:print('your id is lock!')sys.exit('bye')with open('user_id and password', 'r') as file:info_list = eval(file.read())if usename in info_list:password = input('your password:')if password == info_list[usename]:print('login succeed!')sys.exit('wlcome')else:print('enter error!')else:print('no this name or id !')u += 1
if u == 3:with open('lock', 'w') as lock1:lock1.write(usename)

10、for ... else 的实例
name = 'alex'
password = '234'for i in range(3):username = input('please enter your name is :')user_password = input('please enter your password is :')if username == name and user_password == password:print('login succeed')breakelse:print('login fail')
else:print('its more time')


11、凯撒秘密逻辑代码
a ='xyz'         #凯撒密码的原理就是这样,虽然很简单
b = 3
a = a.lower()
for i in a:ascll = ord(i)ascll += bif ascll >= 122:ascll -= 26print(chr(ascll), end='')else:print(chr(ascll), end='')

12、给定两个正整数a和b,将该小数点后第x位到第y位的数字当做密码
def password(a,b,x,y):all = str(a/b)passwd_list = all.split('.')# print(passwd_list)# print(passwd_list[1])  测试输出passwordd = passwd_list[1][x-1:y]long = y-x+1 - len(passwordd)if long > 0:print(passwordd+'0'*long)else:print(passwordd)password(1,2,1,4)

13、三级菜单
location_dict = {'foot':{'apple':{'red':{},'greed':{}},'orange':{'red':{},'black':{}}},'phone':{'iphone:{}'}
}flag = True              # 用于直接退出的标志位
father_list = []          # 存储上一级的内容
current_layer = location_dict    # 用于循环作用于每个菜单上
def show_buttons():print('Function buttons'.center(60, '-'))      # 展示每个功能键的用法print("1. 'exit' means exit program\n""2. 'back' means back to the next level")print('present ending'.center(60, '-'))
show_buttons()
while flag == True:for key in current_layer:print(key)your_choice = input('please input your choice:').strip()   # 去掉用户输入的多余的空格,换行符之类的if len(your_choice) == 0:continueif your_choice in current_layer:father_list.append(current_layer) # 在进入下一级之前,将上一级的内容存储在一个列表中,用于用户的返回上一级的操作current_layer = current_layer[your_choice]   # 变量重新赋值elif your_choice == 'exit':   # 直接退出的标志位print('welcome again!')flag = Falseelif your_choice == 'back':if father_layyer:current_layer = father_list.pop()else:print('This is first layer now!')else:print('maybe not have this choice!')

14、在文件中的某一行后面加上一段字符串
location_dict = {'foot':{'apple':{'red':{},'greed':{}},'orange':{'red':{},'black':{}}},'phone':{'iphone:{}'}
}flag = True              # 用于直接退出的标志位
father_list = []          # 存储上一级的内容
current_layer = location_dict    # 用于循环作用于每个菜单上
def show_buttons():print('Function buttons'.center(60, '-'))      # 展示每个功能键的用法print("1. 'exit' means exit program\n""2. 'back' means back to the next level")print('present ending'.center(60, '-'))
show_buttons()
while flag == True:for key in current_layer:print(key)your_choice = input('please input your choice:').strip()   # 去掉用户输入的多余的空格,换行符之类的if len(your_choice) == 0:continueif your_choice in current_layer:father_list.append(current_layer) # 在进入下一级之前,将上一级的内容存储在一个列表中,用于用户的返回上一级的操作current_layer = current_layer[your_choice]   # 变量重新赋值elif your_choice == 'exit':   # 直接退出的标志位print('welcome again!')flag = Falseelif your_choice == 'back':if father_layyer:current_layer = father_list.pop()else:print('This is first layer now!')else:print('maybe not have this choice!')

15、对一个文件中的内容进行修改方法:  内存的机制只能支持从一个文件的最后进行写入的操作,但是不支持修改,和插入式的添加  所以如果我们需要修改文件中的内容的话,只能从之前的文件中读出所有的内容,然后在将更改之后的内容,写入另一个文件(这种方法存在很大的弊端)
code:
with open('lock', 'r') as file:     # 只读read_list = file.readlines()
num = 0
with open('lock1', 'w') as file1:   # 只写for i in read_list:num += 1if num == 2:                   # 判断需要修改的行号new_string = ''.join([i, 'kangjunhao is a good man'])file1.write(new_string)else:file1.write(i)

16、2992的十进制数表示,其四位数字之和为2+9+9+2=22,它的十六进制数BB0,其四位数字之和也为22,同时它的十二进制数表示1894,其四位数字之和也为22,这种的十进制的数字被成为py数,若为py数,输出‘yes’,否则输出‘no’
def ten(num):add = 0                      # 将十进制的数字每个位上的数字相加num = str(num)for i in num:add += int(i)# print(add)return adddef twelve(num0):little_list = []            # 将数字转换为十二进制并且将每个数字相加while num0 >= 12:num = num0 % 12num0 = num0 // 12little_list.append(str(num))if num0 <= 12:little_list.append(str(num0))change_after = int(''.join(little_list[::-1]))# print(change_after)# print(type(change_after))
    ten(change_after)return ten(change_after)def sixteen(num):sixteen_dict = {'a': 10, 'b': 11, 'c': 12, 'd': 13, 'e': 14, 'f': 15}add = 0                  # 将数字转换为十六进制并且将字母位的转换为十进制的数字,然后将每个数字相加num = hex(num)num = str(num)for i in num:if i in sixteen_dict:i = sixteen_dict[i]add += ielif i == 'x':continueelse:add += int(i)# print(add)return add# print(sixteen(2992))
def main_function(source):# 整个程序的主函数,主要是判断这十进制,十二进制,十六进制的值是不是一样if ten(source) == sixteen(source) and sixteen(source) == twelve(source):answer = 'yes'else:answer = 'no'return answer

17、将日志写入文件的函数
def logging(pay):time_format = "%Y-%m-%d  %H:%M:%S"         # 时间的设置格式now_time = time.strftime(time_format)#now_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))print('you pay {} yuan at {} \n'.format(pay, now_time))with open('lock', 'a') as file:file.write('you pay {} yuan at {} \n'.format(pay, now_time))

18、斐波那契简单的实现列表的实现方式
def fibs(num):result = [0, 1]   # 将斐波那契的前两个数先存在列表中for times in range(num - 2):      # 控制循环的次数result.append(result[-1] + result[-2])  # 将斐波那契数列的最后的两位数字相加,然后把结果存在列表中return result
print(fibs(10))

19、装饰器函数实例(保证封闭原则的情况下,实现功能的扩展)
import time
def show_time(func):   # 装饰器函数def inner(x, y):      # 闭包函数start_time = time.time()print(func(x, y))  # 如果原函数中的返回值使用return返回的,在装饰器函数中添加print函数end_time = time.time()run_time = end_time - start_timereturn run_time# this is source function  ,need add some new functions nowreturn inner
@show_time      # 对原函数名进行重新赋值 @show_time = (add = show_time(add))
def add(x, y):   # 原函数result = x + ytime.sleep(3)return resultprint(add(1, 3))

20、用装饰器写的一个登录页面程序
def show_pages():         # 展示所有可以进入的页面print('all pages'.center(60, '*'))print('1. home page\n''2. book page\n''3. foot page')print('Ending'.center(60, '*'))
#show_pages()
is_exit = True      # 判断退出的标志位
# is_login = False    # 判断是否已经登录,默认未登录def login_type(is_type):         # 装饰器参数,判断以每个页面以哪种方式进行验证登录def login(func):         # 装饰器函数
with open('user info', 'r') as file1:   # 文件中的内容是验证是否登录的is_login = file1.read()def inner():nonlocal is_login     # 因为当密码输入正确的时候,需要修改登录文件中的内容,所以提前声明if is_login == 'false':if is_type == 'weixin':          # 判断某个页面是以什么方式进行登录的,然后在相应的文件中提取账户密码with open('weixin', 'r') as file:user_info = eval(file.read())      # 将文件中的内容以字典的形式拿出elif is_type == 'jindong':with open('jindong', 'r') as file:user_info = eval(file.read())####
your_id = input('Please enter your ID:').strip()your_passwd = input('Please enter your password:').strip()if your_id in user_info and your_passwd == user_info[your_id]:   # 验证账号密码是否正确func()         # 密码正确执行函数is_login = 'true'with open('user info', 'w') as file2:    # 登录成功,将结果记入验证登录文件
                        file2.write(is_login)else:print('id or password is error!')elif is_login == 'true':   # 如果已经登录过,直接执行相应的页面函数# print('ok')
                func()# is_login = little_is_login   # 将little_is_login的值再次赋给return innerreturn login@login_type('jindong')
def home():  # home page source functionprint('welcome to home page!')@login_type('weixin')
def book():   # book page source functionprint('welcome to book page!')@login_type('jindong')
def foot():   # foot page source functionprint('welcome to foot page!')while is_exit == True:show_pages()       # 展示全部的页面信息user_choice = input('Which page do you want to go to?')if user_choice == '1':home()        # 如果输入相应的序号就执行相应的函数elif user_choice == '2':book()elif user_choice == '3':foot()elif user_choice == 'exit':      # 如果输入exit,退出程序print('welcome again!')is_exit = Falseelif len(user_choice) == 0:continueelse:print('input error!')continue

 21、随机数模块生成随机的验证码

import randomrandom_list = []
for i in range(65, 92):random_list.append(chr(i))
for j in range(97, 123):random_list.append(chr(j))
for h in range(10):random_list.append(h)random_num = random.sample(random_list, 5)
for k in random_num:print(k, end='')

22、实现写入日志的函数

import logging
import osdef write_logger(card_num, message):logger = logging.getLogger()card_num = str(card_num)path = os.path.join(r'C:\Users\Administrator\Desktop\kk', card_num)  # 拼接每个账号专属的文件
file_handle = logging.FileHandler(path)  # 创建打印在文件中的对象stream_handle = logging.StreamHandler()  # 打印在屏幕上的对象# output_format = logging.Formatter('%(asctime)--> %(levelname)s--> %(message)s')  # 日志输出的格式output_format = logging.Formatter('%(asctime)s   %(levelname)s   %(message)s')file_handle.setFormatter(output_format)  # 给对象自定义输出格式
    stream_handle.setFormatter(output_format)logger.addHandler(file_handle)        # 添加输出方式
    logger.addHandler(stream_handle)logger.setLevel(logging.DEBUG)logger.debug(message)  #

money = '10'
thing = 'apple'message = ' '.join(['pay for', thing, 'spend', money, '$'])
print(message)write_logger(123456, message)

22、计算机最low实现
import re# 计算乘法
def mul(source_string):if '*' in source_string:only_mul_string = re.search('\d*\*\d*', source_string)  # 只匹配出一个乘法算式only_mul_string = only_mul_string.group()  # 得到匹配出的乘法算式
result_mul = 1  # 乘法结果变量的初始值mul_num_list = re.split('\*', only_mul_string)for num in mul_num_list:result_mul *= int(num)  # 乘法算式得出结果result_mul = str(result_mul)  # 将整形的结果转化为字符串
source_string = source_string.replace(only_mul_string, result_mul)  # 将原来的算式替换成算式的结果return source_string# 计算除法
def div(source_string):if '/' in source_string:only_div_string = re.search('\d*/\d*', source_string)  # 只匹配出一个除法算式only_div_string = only_div_string.group()  # 得到匹配出的乘法算式
div_num_list = re.split('/', only_div_string)result_div = int(div_num_list[0]) / int(div_num_list[1])  # 计算结果
result_div = str(result_div)  # 将整形的结果转化为字符串
source_string = source_string.replace(only_div_string, result_div)  # 将原来的算式替换成算式的结果return source_string# 计算加法
def add(source_string):if '+' in source_string:only_add_string = re.search('\d*\+\d*', source_string)  # 只匹配出一个加法算式,如果是外层的括号也是要进行替换的only_add_string = only_add_string.group()  # 得到匹配出的加法算式
add_num_list = re.split('\+', only_add_string)result_add = int(add_num_list[0]) + int(add_num_list[1])  # 计算结果
result_add = str(result_add)  # 将整形的结果转化为字符串
source_string = source_string.replace(only_add_string, result_add)  # 将原来的算式替换成算式的结果return source_string# 计算减法
def sub(source_string):if '-' in source_string:only_sub_string = re.search('\d*\-\d*', source_string)  # 只匹配出一个减法算式only_sub_string = only_sub_string.group()  # 得到匹配出的减法算式
sub_num_list = re.split('\-', only_sub_string)result_sub = int(sub_num_list[0]) - int(sub_num_list[1])  # 计算结果
result_sub = str(result_sub)  # 将整形的结果转化为字符串
source_string = source_string.replace(only_sub_string, result_sub)  # 将原来的算式替换成算式的结果return source_string# 整个计算机程序,将会从这里开始执行

test_string = input('please input you need counting equation:')   # 用户输入算式if len(re.findall('\(', test_string)) != len(re.findall('\)', test_string)):print('error')      # 判断括号的数量是否相等# 因为在计算完毕括号中的内容后,会出现两个数字之间有两个符号,这个函数就是处理这个问题的
def format(source_string):format_dic = {'+-': '-', '-+': '-'}for i in format_dic:if i in source_string:source_string = source_string.replace(i, format_dic[i])return source_stringwhile '(' in test_string:         # 如果算式中还有括号,就继续循环计算ret = re.search('\([^\(\)]+\)', test_string).group()  # 取到有括号的式子# print(ret)counting_mul = mul(ret)counting_div = div(counting_mul)counting_add = add(counting_div)counting_sub = sub(counting_add)counting_sub = counting_sub.replace('(', '')  # 如果计算完毕括号中的内容后,将取消已经计算完的括号counting_sub = counting_sub.replace(')', '')test_string = test_string.replace(ret, counting_sub)test_string = format(test_string)      # 检查格式,经过循环的对括号的处理,最后剩下的数据都是没有括号的低级运算for i in range(10):                     #  因为没能找到合适的控制循环的条件,所以就指定十次循环,一般情况下够用if len(re.findall('\b', test_string)) == 2: # 匹配有特殊边界的字符串,所以当只有数字的时候就只有指定的两个特殊边界pass# print(test_string)else:counting_mul = mul(test_string)      # 对之前的低级运算可以进行再计算,最后得到正确的答案counting_div = div(counting_mul)counting_add = add(counting_div)counting_sub = sub(counting_add)test_string = counting_subprint('answer : %s' % test_string)   # 最后输出最终的答案

 23、面向对象编程,实现页面分页(属性)

class bar:def __init__(self, page):try:    # 如果输入不是一个数字,则返回第一页的内容self.page = int(page)except Exception as e:self.page = 1@propertydef start(self):start_page = (self.page-1) * 10return start_page@propertydef end(self):end_page = self.page * 10return end_pagetest_list = []
for i in range(1000):  # 相当于生成很多的页面中的内容test_list.append(i)  #  存储在一个列表中,使用的时候,用列表切片的方法取出while True:your_enter = input('open page:')obj = bar(your_enter.strip())  # 创建对象,将用户输入的页数传给对象,让类中的init方法来执行print(test_list[obj.start:obj.end]) # 将对应的页面的内容利用切片的方式去吃

利用字符串调用模块中的函数

import app while True:your_enter = input('your enter:')try:func = getattr(app, your_enter.strip()) # 利用反射中的方法用字符串对模块进行操作func() # 执行用户输入的对应的函数except Exception as e:print('hello world')

 25、b站主播间狂刷弹幕

复制代码
import requests
import randomimport timeclass get_danmu:def __init__(self, room_id):self.room_id = str(room_id)self.url = 'https://api.live.bilibili.com/ajax/msg'self.dict = {'roomid': self.room_id,'csrf_token': '9093d7c5990c96429fc8f45f8d23047a','visit_id': 'avttexb3o08'  }self.send_url = 'https://api.live.bilibili.com/msg/send'  # 发送弹幕的地址
self.Cookie = {'Cookie':'你的cookie'}# 保存你的账号密码信息,用于登录def message(self):    # 拿到弹幕result = requests.post(self.url, self.dict) # 拿到当前地址的数据,(json文件)进行后续操作
self.danmu_list = []   # 定义一个空的列表用来存储抓到的别人的信息for i in range(10): # 循环每次拿取十个弹幕page = i# format = ''.join([result, "result.json()['data']['room']", "[", page, "]", "['text']"])# print(format)requests_format = "result.json()['data']['room'][%d]['text']" % page  # 格式化输出#print(eval(requests_format))  # 转化字符串格式# map(lambda x : eval(requests_format), range(10))
self.danmu_list.append(str(eval(requests_format)))    # 将最近的十个弹幕放在列表中
self.danmu_message = self.danmu_list[random.randint(1, 9)]   # 随机后去列表中保存的一个弹幕# print(self.danmu_list)print(self.danmu_message)def send_danmu(self):# result_send = requests.post(self.send_url, self.send_dict, self.Cookie)
self.send_dict = {         # 请求发送弹幕的参数'color': '16777215','fontsize': '25','mode': '1','msg': self.danmu_message,# 'msg': '6666','rnd': '1529894665','roomid': self.room_id,'csrf_token': '9093d7c5990c96429fc8f45f8d23047a'}result_send = requests.post(self.send_url, data = self.send_dict, cookies = self.Cookie)room_id = input('room id:')obj = get_danmu(room_id)times = 0
while times < 100:obj.message()obj.send_danmu()times += 1

 25.socket实现不间断连接(有发送消息的时间,输入exit退出通信)

# server端
import socket
import datetime
server = socket.socket()
address = ('192.168.205.1', 8000)
server.bind(address)
server.listen(5)
print('For writing....')
conn, adr = server.accept()
print('连接成功')
while True:your_enter = input('your enter:')if your_enter == 'exit':    # 如果用户输入exit就退出通信breaklast_info = ' '.join([your_enter, str(datetime.datetime.now())])conn.send(bytes(last_info, 'utf8'))resquests = conn.recv(1024)if not resquests:       # 因为对端如果发送的是exit,那么就不发送消息,所以发现对端没有发送消息就退出breakprint(str(resquests, 'utf8'))conn.close()# client端
import socket
import datetime
client = socket.socket()
address = ('192.168.205.1', 8000)
client.connect(address)
print('连接成功')
while True:requests = client.recv(1024) # 因为对端如果发送的是exit,那么就不发送消息,所以发现对端没有发送消息就退出if not requests:breakprint(str(requests, 'utf8'))your_enter = input('your enter:')if your_enter == 'exit':              # 如果用户输入exit就退出通信breaklast_info = ' '.join([your_enter, str(datetime.datetime.now())])      # 实际发送的消息包括时间和消息内容client.send(bytes(last_info, 'utf8'))
client.close()

26、socket执行cmd命令

# server端
import socket
import subprocess
server = socket.socket()
address = ('192.168.205.1', 8000)
server.bind(address)
server.listen(5)  # 服务端允许最多的排队客户端
print('writing....')
conn, adr = server.accept()      # 暂时阻塞等待客户端的连接
print('连接成功')   # 连接成功提示
while True:response = conn.recv(1024)obj = subprocess.Popen(str(response, 'utf8'), shell=True, stdout=subprocess.PIPE) # 创建可以与命令提示符交互的对象return_value = obj.stdout.read()conn.send(bytes(str(len(return_value)), 'utf8'))  # 向客户端发送返回信息的长度,让客户端确定需要几次的接受
conn.sendall(return_value)  # 发送实际返回信息# client端
import socketclient = socket.socket()
address = ('192.168.205.1', 8000)
client.connect(address)   # 确定这个客户端需要连接那个服务端
print('连接成功')while True:your_enter = input('your enter:')client.send(bytes(your_enter, 'utf8'))   # 将用户的输入的请求发送到服务端info = client.recv(1024)#data_len = int(str(client.recv(1024), 'utf8'))# print(data_len)end_info = bytes()  # 初始化一个变量 ,字节类型while len(end_info) != int(str(info, 'utf8')):data = client.recv(1024)end_info += dataprint(str(end_info, 'gbk'))

27、爬取英雄联盟的皮肤

import json
import re
import requestsdef get_lol_images():url_js = 'http://lol.qq.com/biz/hero/champion.js'html_js = requests.get(url_js).text # 拿到js源码# print(html_js)hero_list = re.findall('"keys":(.*?),"data"', html_js)    # 正则表达式匹配带有英雄id的字符串hero_dict = json.loads(hero_list[0]) # 将带有英雄id的列表转换为字典,便于后面使用里面的数据# print(json.loads(hero_list[0]).values())hero_face_url_list = [] # 接下来将拼接好的所有的皮肤网址,添加到列表中for hero_id in hero_dict:# print(hero_id)                 # for 循环得到每个英雄的idfor i in range(20):      # 每个英雄循环20次,代表20个皮肤num = str(i)face_id = ''if len(num) == 1:    # 如果皮肤位的数字是个位数那么就需要在皮肤前面加上两个零,因为皮肤位固定是三位数face_id = '00' + numelif len(num) == 2:face_id = '0' + numhero_face_id = str(hero_id) + face_id  # 将英雄的id和皮肤的id拼接# print(hero_face_id)hero_face_url = 'http://ossweb-img.qq.com/images/lol/web201310/skin/big'+hero_face_id+'.jpg'  # 接下来是对每个皮肤的网址进行拼接hero_face_url_list.append(hero_face_url)  # 循环将每个皮肤网址存入列表# print(hero_face_url)# if requests.get(hero_face_url) == 200:#     hero_face_url_list.append(hero_face_url)    # 判断每个网址,如果可以正常访问的就加入列表中# else:#     pass# print(hero_face_url_list)path_list = [] # 存放皮肤在本地的存放地址for name in hero_dict.values():      # 循环字典中的值# print(name)    # 拿到英雄的名字用于地址的拼接for i in range(20):path_url = 'C:/Users/Administrator/Desktop/hero face/' + name + str(i) + '.jpg' # 拼接皮肤的存放地址path_list.append(path_url) # 将拼接好的存放地址存入列表# print(path_list)# 接下来进行下载n = 0for hero in hero_face_url_list:res = requests.get(hero)n += 1if res.status_code == 200:   # 判断如果皮肤网址可以正确访问就下载# print(hero)print('正在下载 %s' % path_list[n])with open(path_list[n], 'wb') as file:file.write(res.content)
get_lol_images()

 28、文件上传(代码实现图片的上传,可以上传任何类型的文件)

#  server端import socket
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
server = socket.socket()
address = ('192.168.205.1', 8000)
server.bind(address)
server.listen(5)
print('writing....')
conn, adr = server.accept()
print('连接成功')
while True:file_info = conn.recv(1024) # 接受到传输文件所需的所有信息cmd, file_path, file_size = str(file_info, 'utf8').split('|')  # 文件切片file_name = os.path.basename(file_path)  # 拿到文件名,因为下面需要创建一个一样文件名来存放接受到的文件recv_path = os.path.join(BASE_DIR, file_name)# print(cmd)# print(file_path)# print(file_size)
with open(recv_path, 'wb') as file:recv_size = 0while recv_size != int(file_size):data = conn.recv(1024)file.write(data)recv_size += len(data)print('接受成功')# client端import socket
import os
client = socket.socket()
address = ('192.168.205.1', 8000)
client.connect(address)
print('连接成功')BASE_DIR = os.path.dirname(os.path.abspath(__file__))   # 拿到根目录的路径,用于以下文件的绝对路径的拼接while True:your_enter = input('your enter:').strip() # 输入的格式,post|需要传送的文件的相对路径,去除多余的换行符
cmd, filename = your_enter.split('|')    # 拿到上传命令和上传文件的相对路径
file_path = os.path.join(BASE_DIR, filename)      # 需要上传文件的绝对路径的拼接
file_size = os.stat(file_path).st_size  # 拿到需要传送的文件的大小,如果文件很大需要循环上传
file_info = '|'.join([cmd, file_path, str(file_size)])    # 将所有的传输的信息打包发送到server端# print(file_info)
client.send(bytes(file_info, 'utf8'))    # 将打包好的信息传送
with open(file_path, 'rb') as file:    # 打开文件,获取数据
send_size = 0while send_size != file_size: # 循环读取文件,并且发送data = file.read(1024)client.send(data)send_size += len(data)print('上传成功')

 30、ftp实现

实现功能:上传文件,下载文件,可多用户同时连接,实现用户连接密码,实现断点续传

未实现功能:上传文件或者是下载文件时显示进度条

# server端import socketserverimport subprocessBASEDIR = os.path.dirname(os.path.abspath(__file__))class MyServer(socketserver.BaseRequestHandler):def handle(self):       # 主逻辑代码print('连接成功')conn = self.request   # 客户端与服务端的通信对象while True:accept_info = str(conn.recv(1024), 'utf8') #  接受到命令# print(str(accept_info, 'utf8'))func = accept_info.split('|')[0]    # 切片观察,客户端需要做什么操作message = accept_info.split('|')[1]  # 拿到客户端需要做的操作if func == 'cmd': # cmd命令obj = subprocess.Popen(message, shell=True, stdout=subprocess.PIPE)   # 调动命令提示符cmd_value = obj.stdout.read()# print(str(cmd_value, 'gbk'))# print(type(cmd_value))cmd_value_size = len(cmd_value) # 返回值大小conn.send(bytes(str(cmd_value_size), 'utf8'))  # 发送返回值的大小
conn.send(cmd_value)  # 发送返回值的内容elif func == 'post':  # 上传文件# print('ok')end_info = bytes()# print(accept_info.split('|')[2])       # 接受的文件大小信息while len(end_info) != int(accept_info.split('|')[2]):  # accept_info[2]是需要传送文件的sizeaccept_file = conn.recv(1024)end_info += accept_file# print(end_info)   # 查看接受到的文件的信息file_dir = os.path.join(BASEDIR, message) # 接受到的文件的存放地址# with open(file_dir, 'wb') as file:#     file.write(end_info)   # 上传成功#     print('上传成功')with open(file_dir, 'wb') as file:file.write(end_info)print('接受成功')elif func == 'download':  # 下载文件# print('ok')file_path = os.path.join(BASEDIR, message)  # 客户端需要下载的文件的绝对路径file_size = os.stat(file_path).st_size  # 文件大小# print(file_path, file_size)conn.send(bytes(str(file_size), 'utf8'))    # 发送文件大小# print('ok')download_file_size = int(str(conn.recv(1024), 'utf8'))  # 客户端已经下载的文件的大小with open(file_path, 'rb') as file:file.seek(download_file_size)    # 调整光标download_info = file.read() # 文件的字节类型的数据# print(type(download_info))
                conn.sendall(download_info)print('上传成功')address = ('127.0.0.1', 6000)
server = socketserver.ThreadingTCPServer(address, MyServer)
server.serve_forever()   # 调用方法

# client端import osimport socketimport sysBASEDIR = os.path.dirname(os.path.abspath(__file__))  # 父级地址

client = socket.socket()  # 建立client端
address = ('127.0.0.1', 6000)while True:  # 密码验证password = input('your password:')if password == '123':print('服务端验证成功')breakelse:print('服务端验证失败')continue
client.connect(address)
print('连接成功。。。。')while True:     # 接受信息,发送信息,主逻辑代码your_enter = input('your enter:').strip()if your_enter.split('|')[0] == 'post':   # 如果客户端需要上传文件的处理方法file_dir = os.path.join(BASEDIR, your_enter.split('|')[1])  # 拼接出需要传送的文件的绝对路径with open(file_dir, 'rb') as file:file_message = file.read()  # 文件内容,等待发送file_size = os.stat(file_dir).st_size   # 获取文件大小your_enter = '|'.join([your_enter, str(file_size)])  # 打包需要传送的用户输入的命令和文件大小client.send(bytes(your_enter, 'utf8'))client.send(file_message) # 发送内容print('上传成功')# print(type(file_message))continueelif your_enter.split('|')[0] == 'download':   # 如果客户端需要从服务端下载文件的处理方法download_dir = os.path.join(BASEDIR, your_enter.split('|')[1]) # 下载后文件的存放路径client.send(bytes(your_enter, 'utf8'))download_file_size = int(str(client.recv(1024), 'utf8'))   # 需要下载的文件的大小# accept_download_size = os.stat(download_dir).st_size  #with open(download_dir, 'w') as file:accept_download_size = os.stat(download_dir).st_size  # 已经下载下来的文件大小client.send(bytes(str(accept_download_size), 'utf8'))  # 发送已经下载文件的大小download_file = bytes() # 将每段接受到的数据,最后都整合在download_file 中,最后文件中写入的也是这个数据while len(download_file) != download_file_size - accept_download_size:download_file_info = client.recv(1024)  # 小段的文件
download_file += download_file_infowith open(download_dir, 'ab') as file:file.write(download_file)print('下载成功')continueclient.sendall(bytes(your_enter, 'utf8'))accept_size = int(str(client.recv(1024), 'utf8'))  # 接受到将要传输的命令的大小end_info = bytes()       # 初始化一个字符类型的变量while len(end_info) != accept_size:  # 实现连续接受accept_info = client.recv(1024)end_info += accept_infoprint(str(end_info, 'gbk'))

 31.对一个需要请求头的网址进行爬取

# 第一种import urllib.requesturl = 'https://blog.csdn.net/jiangwei0910410003/article/details/79436956'  # 需要爬取的网址

response_head = ('User-Agent', '请求头信息')  # 设置请求头信息

add_head_obj = urllib.request.build_opener()  # 新建一个有添加请求头功能的对象

add_head_obj.addheaders = [response_head]   # 添加请求头

response = add_head_obj.open(url)   # 使用添加了请求头的对象请求一个网址

content = response.read()  # 获取HTML文档print(content)

# 第二种
import urllib.request

url = "http://www.baidu.com"

url_obj = urllib.request.Request(url)

url_obj.add_header("User-Agent", 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36')  # 添加请求头

content = urllib.request.urlopen(url).read()

print(content)

32.使用代理服务器爬取网页
# 实现使用代理服务器来爬取网页import urllib.requestdef use_proxy(url, proxy_ip):    # url是爬取的网址,proxy_ip是代理服务器的地址和端口setting_proxy = urllib.request.ProxyHandler({'http': proxy_ip})opener = urllib.request.build_opener(setting_proxy, urllib.request.HTTPHandler)urllib.request.install_opener(opener)data = urllib.request.urlopen(url).read()return dataurl = 'https://tieba.baidu.com/index.html'
proxy_ip = "123.114.79.109:1080"
content = use_proxy(url, proxy_ip)
with open('thrid.html', 'wb') as file:file.write(content)

 33.爬取网页时实现输出日志

#  实现爬取网页时打印对应日志import urllib.requestdef input_logger(url):httphd = urllib.request.HTTPHandler(debuglevel=1)      # 设置日志输出级别httpshd = urllib.request.HTTPSHandler(debuglevel=1)opener = urllib.request.build_opener(httphd, httpshd)   # 设置自定义opener对象urllib.request.install_opener(opener)   # 将opener对象创建为全局默认的对象data = urllib.request.urlopen(url).read()return dataurl = "https://www.cnblogs.com/kangjunhao/p/9097983.html#div1"
content = input_logger(url)
with open('thrid.html', 'wb') as file:file.write(content)

 34.字符画

from PIL import Image # PIL 是一个 Python 图像处理库ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,^`'. ")
# 是我们的字符画所使用的字符集,一共有 70 个字符,字符的种类与数量可以自己根据字符画的效果反复调试的WIDTH = 100  # 字符画的宽
HEIGHT = 100  # 字符画的高# 将256灰度映射到70个字符上,也就是RGB值转字符的函数:
def get_char(r, g, b, alpha=256):  # alpha透明度if alpha == 0:return ' 'length = len(ascii_char)gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b)  # 计算灰度unit = (256.0 + 1) / lengthreturn ascii_char[int(gray / unit)]  # 不同的灰度对应着不同的字符# 通过灰度来区分色块if __name__ == '__main__':img = '000.png'  # 图片所在位置im = Image.open(img)im = im.resize((WIDTH, HEIGHT), Image.NEAREST)txt = ""for i in range(HEIGHT):for j in range(WIDTH):txt += get_char(*im.getpixel((j, i))) # 获得相应的字符txt += '\n'print(txt)  # 打印出字符画# 将字符画 写入文件中# with open("C:/Users/lec/Desktop/output.txt", 'w') as f:#     f.write(txt)

 35.微信控制电脑

import itchat
import os
import time
import cv2sendMsg = u"{消息助手}:暂时无法回复"
usageMsg = u"使用方法:\n1.运行CMD命令:cmd xxx (xxx为命令)\n" \u"-例如关机命令:\ncmd shutdown -s -t 0 \n" \u"2.获取当前电脑用户:cap\n3.启用消息助手(默认关闭):ast\n" \u"4.关闭消息助手:astc"
flag = 0 #消息助手开关
nowTime = time.localtime()
filename = str(nowTime.tm_mday)+str(nowTime.tm_hour)+str(nowTime.tm_min)+str(nowTime.tm_sec)+".txt"
myfile = open(filename, 'w')@itchat.msg_register('Text')
def text_reply(msg):global flagmessage = msg['Text']fromName = msg['FromUserName']toName = msg['ToUserName']if toName == "filehelper":if message == "cap":cap = cv2.VideoCapture(0)ret, img = cap.read()cv2.imwrite("weixinTemp.jpg", img)itchat.send('@img@%s'%u'weixinTemp.jpg', 'filehelper')cap.release()if message[0:3] == "cmd":os.system(message.strip(message[0:4]))if message == "ast":flag = 1itchat.send("消息助手已开启", 'filehelper')if message == 'astc':flag = 0itchat.send('消息助手已关闭', 'filehelper')elif flag == 1:itchat.send(sendMsg, fromName)myfile.write(message)myfile.write('\n')myfile.flush()
if __name__ == '__main__':itchat.auto_login()itchat.send(usageMsg, 'filehepler')itchat.run()

35.by myslef

import itchat
import cv2
import os
# itchat.auto_login()
@itchat.msg_register("Text")   # 如果接受到文本消息(msg),就调用下面的方法
def send_msg(msg):send_content = msg['Text']    # 接受到的文本消息Tousename = msg['ToUserName']  # 接受消息的手机端try:          # 错误捕捉itchat.send(send_content, toUserName=Tousename)# print(msg)except Exception as e:print(e)if send_content == 'cap':         # 如果接受到手机端的文本消息为cap,就调用cv库cap = cv2.VideoCapture(0)ret, img = cap.read()cv2.imwrite("weixinTemp.jpg", img)itchat.send('@img@%s'%u'weixinTemp.jpg', 'filehelper')cap.release()if send_content[0:3] == "cmd":       # 判断是否是cmd命令os.system(send_content.strip(send_content[0:4]))    # 获取到手机端发送的真正的cmd命令# itchat.send("kangjunhao", toUserName="filehelper")
run_hlep = "自拍请输入cap"
itchat.auto_login()
itchat.send(run_hlep, toUserName="filehelper")
itchat.run()

36.爬取京东上所有手机的图片

import urllib.request
import re
# 定义一个函数,参数为总共网页数
def get_pic(page):pic_sum = 0   # 计算一共爬取了多少张图片page = int(page)for page_num in range(page):   # 循环所有的页数page_num = str(page_num)url = "https://list.jd.com/list.html?cat=9987,653,655&page=%s&sort=sort_rank_asc&trans=1&JL=6_0_0#J_main"%page_num  # 拼接网址# print(url)response = urllib.request.urlopen(url)sound_code = str(response.read())      # 获取网址源码# print(sound_code)
pic_rule = re.compile("//img10.360buyimg.com/n7/.*?.jpg")   # 匹配图片链接的re规则pic_link_list = re.findall(pic_rule, sound_code)  # 获取到图片的连接# print(pic_link_list)for pic_link in pic_link_list:  # 遍历图片链接的列表pic_url = "http:" + pic_link  # 拼接好图片的网址# print(pic_url)pic_name = pic_url.split('/')[-1]  # 获取每个图片的namepic_path = '/'.join(["C:/Users/Administrator/Desktop/jd_phone", pic_name])   # 拼接图片的存放路径try:urllib.request.urlretrieve(pic_url, pic_path)  # 将图片进行存储except Exception as e:print(e)print('下载完成%s'%pic_path)pic_sum += 1print(pic_sum)  # 打印图片总数
if __name__ == '__main__':get_pic(162)

37.获取某网址中的所有link

import urllib.request
import redef get_link(url):head_info = ('user-agent', '自己的头部信息')opener = urllib.request.build_opener()   # 模拟成浏览器,添加头信息opener.addheaders = [head_info]urllib.request.install_opener(opener)   # 将opener安装为全局对象
response = urllib.request.urlopen(url)  #
result = str(response.read())   # 获取到网页源码
rule = re.compile('(https?://[^\s)";]+\.(\w|/)*)')res = re.findall(rule, result)for i in res:print(i)# print(result)if __name__ == '__main__':url = 'https://www.csdn.net/'get_link(url)

38.爬取糗事百科上的所有笑话

import urllib.request
import re# 定义一个自动爬取糗事百科网站的所有笑话
def get_joke(pages):pages = int(pages)for page in range(1, pages):url = 'https://www.qiushibaike.com/8hr/page/%s/'%page   # 拼接好网址
head_info = ('user-agent', '你的浏览器的头信息')  # 添加头信息opener = urllib.request.build_opener()opener.addheaders = [head_info]urllib.request.install_opener(opener)   # 将opener安装为全局
url_response = urllib.request.urlopen(url)url_sound_code = url_response.read()   # 获取到源码print('ok')joke_link_rule = re.compile('/article/[\d]*')   # 匹配网址中的joke_link的re规则link = re.findall(joke_link_rule, str(url_sound_code))joke_set = set()  # 创建一个集合用于去掉重复的idfor joke_link in link:joke_id = joke_link.split('/')[-1]   # 获取到每则笑话的id# print(joke_id)
            joke_set.add(joke_id)# print(joke_set)for id in joke_set:joke_url = 'https://www.qiushibaike.com/article/%s' % id   # 拼接好每则笑话的urljoke_response = urllib.request.urlopen(joke_url)joke = joke_response.read().decode('utf-8')   # 获取每则笑话网页的源码,需要编码,要不然不能显示中文
joke_rule = re.compile('<div class="content">.*?</div>', re.S)joke_content_list = re.findall(joke_rule, joke)     # 匹配到到每则笑话的文本内容for joke_content in joke_content_list:# print(type(jokes))joke_content = joke_content.replace('<br/>', '\n')    # 将源码中的<br>换行转成\n换行print(joke_content)try :with open('C:/Users/Administrator/Desktop/joke/jokes.txt', 'a') as file:file.write(joke_content)except Exception as e:print(e)# print(link)
if __name__ == '__main__':get_joke(14)   # 糗事百科一共有14个页面

39.使用代理服务器:

        import urllib.requestproxy = urllib.request.ProxyHandler({'http':'proxy_addr'})opener = urllib.request.build_opener(proxy, urllib.request.HTTPHandler)urllib.request.install_opener(opener)

40.爬取微信文章

import urllib.request
import re# 添加头信息
head_info = ('user-agent', 'head_info')  # 头部信息
opener = urllib.request.build_opener()
opener.addheaders = [head_info]
urllib.request.install_opener(opener)# 使用代理服务器
# proxy = urllib.request.ProxyHandler({'http': '17.90.3.75:9000'})
# opener = urllib.request.build_opener(proxy, urllib.request.HTTPHandler)
# urllib.request.install_opener(opener)# 定义一个函数,两个参数,第一个参数是想要搜索的关键字,第二个是你搜索的这个关键字,一共有多少个相关页面
def get_article(key_words, pages):key_words = urllib.request.quote(key_words)   #对汉字进行编码处理# print(type(key_words))for page in range(1, int(pages)+1):url = 'http://weixin.sogou.com/weixin?type=2&query=%s&ie=utf8&page=%s' % (key_words, page)   # 含有文章link的网址# print(url)# url = urllib.request.quote(url)# print(url)try:sound_code = urllib.request.urlopen(url).read().decode('utf-8')   # 将源代码转换成utf-8格式
article_link_list = re.findall('<a target="_blank" href=".*?"', sound_code)article_link_list.pop(-1)  # 删除列表中的最后一个数据(经过观察最后一个值没有用)for article_link in article_link_list:# print(article_link.split('"')[-2].replace('amp;', ''))article_link = article_link.split('"')[-2].replace('amp;', '')  # 对在sound_code中匹配到的网址进行替换和分割得到正真的article_link
article_sound_code = urllib.request.urlopen(article_link).read().decode('utf-8')   # 获取一篇文章的源代码
title_rule = re.compile('<h2 class="rich_media_title" id="activity-name">.*?</h2>', re.S)   # 匹配文章name的ruletitle_name = re.findall(title_rule, article_sound_code)for title_html in title_name:title = re.findall('[\u4e00-\u9fa5]', title_html)# print(title)title_words = ''    # 用于拼接文章namefor word in title:title_words += word   # 获取到每篇的标题,用于拼接路径
title_words = title_words + '.txt'# print(title_words)
html_rule = re.compile('<div class="rich_media_content " id="js_content">.*?</div>', re.S)   # 匹配含有文章内容的HTML源码
html_code_list = re.findall(html_rule, article_sound_code)    # 含有文章内容的源码的列表for html_code in html_code_list:article_list = re.findall('[\u4e00-\u9fa5]', html_code)   # 含有文章中所有文字的列表,但是需要对所有的文字进行拼接# print(article)article = ''for words in article_list:article += wordsarticle = article.replace('微软雅黑', '').replace('宋体', '')   # 得到文章的纯文本内容,对文本中的style设置的某些字段进行替换# print(article)download_path = '/'.join(['C:/Users/Administrator/Desktop/weixin', title_words])with open(download_path, 'w') as file:   # 将文章文本内容存入对应txt文件内
                        file.write(article)print('%s 下载完成' % download_path)  # 显示文章下载完成# print(html_code)except Exception as e:print('error is %s' % e)if __name__ == '__main__':get_article('计算机', 11)

41.窗口化随机考察单词

  注意:此程序使用了Tkinter模块

import random
from tkinter import *
import tkinter
import tkinter.font as tkFontdef output_word():soucre = ['appliance', 'purchase', 'capture', 'loyalty', 'multiple', 'original', 'merge', 'individual','subscriber','universal', 'automobile', 'embarrassment', 'transaction', 'immigrant', 'flavor', 'vanilla', 'franchise','yogurt', 'premium', 'dairy', 'combination', 'innovate', 'release', 'dense', 'content', 'substantially','grocery', 'documentary', 'label', 'philosophy', 'frequently', 'remarkable', 'revive', 'symbolic','innovation', 'shadow', 'disturbance', 'falter', 'quest', 'smash']flag = Trueoutput_words_list = []   # 存储最后将要输出的单词while flag:word_index = random.randint(0, 39)     # 产生随机下标# print(word_index)if soucre[word_index] not in output_words_list:output_words_list.append(soucre[word_index])  # 添加元素# print(output_words_list)if len(output_words_list) == 20:breakprint(output_words_list)output_words_list0 = []  # 存放前十个单词output_words_list1 = []  # 存放后十个单词for i in range(10):output_words_list0.append(output_words_list[i])   # 循环存储前十个单词for j in range(10, 20):output_words_list1.append(output_words_list[j])   # 循环存储后十个单词# for word in output_words_list:# listb.insert(0, word)    # 往Listbox中添加随机生成的单词for befor_word in output_words_list0:listb0.insert(0, befor_word)for last_word in output_words_list1:listb1.insert(0, last_word)listb0.pack()listb1.pack()def clear_screen():   # 清屏函数
listb0.delete(0, 9)   # 删除打印的单词listb1.delete(0, 9)  # 删除打印的单词
root = Tk()root.title('English exam')
root.geometry('1370x768')   # 设置窗口大小

ft1 = tkFont.Font(size=40, slant=tkFont.ROMAN)   # 字体样式对象

listb0 = Listbox(root, height=20, font=ft1)  # 设置显示单词的行数
listb1 = Listbox(root, height=20, font=ft1)  # 设置显示单词的行数
listb0.pack(padx=20, pady=100, side=LEFT)   # 设置两个列表的显示位置
listb1.pack(padx=20, pady=100, side=LEFT)Button(root, text='get words', command=output_word).pack(side=LEFT, padx=20)  # 设置button位置

Button(root, text='clear', command=clear_screen).pack(side=LEFT)root.mainloop()

the finally

转载于:https://www.cnblogs.com/kangjunhao/p/9097983.html

Python 练习小代码相关推荐

  1. python编程小代码_Python趣味小游戏编写代码

    这篇文章教大家用Python编写一些有趣的小程序,用到的都是一些简单的基础的python语句,适合刚入门的小白,可以尝试跟着一起敲一下,感受一下编程中的乐趣. 数字炸弹 相信大家在聚餐时都玩过猜数字游 ...

  2. python一些小代码集

    1.猜字 """猜测代码的含义""" import random b=int(random.randint(1,100)) print(b) ...

  3. 【python趣味小代码】为你女(男)神打造专属素描照,hhhhhh

    前言 嗨喽~大家好呀,这里是魔王呐 ! 在这寻常得今天,我逛我的文件夹得时候 发现我还有这个有趣得效果得代码,不止是何夕写的拉~ 反正现在被我挖掘出来了~被我使用了!! 然后在这里在分享给大家~可以拿 ...

  4. python手机版做小游戏代码大全-python简单小游戏代码 怎么用Python制作简单小游戏...

    1.Python猜拳小游戏代码: 2.import random #导入随机模块 3. 4.num = 1 5.yin_num = 0 6.shu_num = 0 7.while num <= ...

  5. python小游戏代码大全-python简单小游戏代码 怎么用Python制作简单小游戏

    1.Python猜拳小游戏代码: 2.import random #导入随机模块 3. 4.num = 1 5.yin_num = 0 6.shu_num = 0 7.while num <= ...

  6. pythonencoding etf-8_etf iopv python 代码30个Python常用小技巧

    1.原地交换两个数字x, y =10, 20 print(x, y) y, x = x, y print(x, y) 10 20 20 10 2.链状比较操作符n = 10 print(1 print ...

  7. python 小甲鱼 代码_Python小代码

    先自我介绍一下,本人是正在自学Python的小白,没事分享一下自己写的小代码,欢迎在评论区补充. 游戏管理系统: 代码如下: def healthe(m):if m=="Y"or ...

  8. python画图代码-Python实战小程序利用matplotlib模块画图代码分享

    Python中的数据可视化 matplotlib 是python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地进行制图.而且也可以方便地将它作为绘图控件. 实战小程序: ...

  9. 用python画图代码-Python实战小程序利用matplotlib模块画图代码分享

    Python中的数据可视化 matplotlib 是python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地进行制图.而且也可以方便地将它作为绘图控件. 实战小程序: ...

最新文章

  1. Apache Beam和BigQuery的错误处理(Java SDK)
  2. 用python做预测模型的好处_如何用Python在10分钟内建立一个预测模型
  3. 唐山师范学院计算机考试,[河北]唐山师范学院2017年3月计算机一级考试报名时间...
  4. Python基础教程和入门教程
  5. PHP压缩CSS文件
  6. python 画ks曲线_风控模型—区分度评估指标(KS)深入理解应用
  7. .net reflector 反编译失败 索引超出了数组界限问题处理方法
  8. 课程设计:89C51单片机实现六位密码锁
  9. 鞍部在哪里_观山指南!喜马拉雅9座8000米高峰在哪儿看?
  10. matlab矩阵a(8),在matlab中对矩阵A的第2行第1列赋值为8可用A(1,2)=8表示。()
  11. 出口退税率6月20日准时出通知了!客户下单了!
  12. Unity双人坦克大战竞速游戏,超简单教学
  13. apache arm 交叉编译_Apache 2移植到Arm开发板的过程整理——如何交叉编译Apache 2
  14. 适合小白入门的随机森林介绍
  15. 商场根据会员积分打折
  16. js 实现前端数据导出为excel表格
  17. 老旧计算机桌面,怎么让老旧电脑上快速度启动U盘Winpe及桌面没东西解决
  18. 正则表达式 - 常用的正则表达式级正则的捕获
  19. DCMM数据管理能力成熟度标准介绍
  20. 选择结构习题:输入0~6的数字,输出对应星期的英文。

热门文章

  1. jQuery.ajaxSetup() 详解
  2. 字节番茄小说一面+二面+三面,本以为挂了,却意外收到offer
  3. 易语言如何有效避免静态编译后误报
  4. python定义一个int_python中定义int类型的方法
  5. 《寂静的春天》内容梳理概括|思维导图清晰漂亮
  6. java 字节流和字符流
  7. uniapp 微信小程序之拉取用户电话号码
  8. python面向对象编程中方法和属性_Python面向对象编程中关于类和方法的学习笔记...
  9. Asp.Net Web Api 部署------远程连接云服务器
  10. EXCEL如何将日期格式转换成文本格式