Mr.kang之真经求取
                                        ---积跬步,至千里

Python 练习小代码

 

代码目录  

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 function
     before, after = 0, 1
     for i in range(bit):
         print(before)
         before, after = after, before + after
 fibs(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 = 30

while flag == True:
    guess =int(input('your guess age:'))
    if guess == age:
        print('you right!')
        flag = False
    else:
        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 = True
    while flag == True:

        your_choice = input('exit:')
        if your_choice == 'yes':
            flag = False
        else:
            num = int(input('>>:'))
            num_list_a.append(num)
    return num_list_a
def max_min_num(a):   #求出最大值和最小值
    max = 0
    min = a[0]
    for i in a:
        if i < min:
            min = i
        if i > max:
            max = i
    print('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+1
    else:
        max = y+1
    for i in range(1,max):
        if x % i == 0 and y % i == 0:
            timess += 1
    return 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 show
    n = 0
    print('----------------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 flag
    buy_again = input('you want to buy again:')
    if buy_again == 'yes':
        show_shopping()
        main()
    else:
        print('wlcome again!')
        flag = False
    return flag
# 主函数
def main():
    # 调用全局变量
    global your_all_money
    global flag
    while flag == True:
        exit = input('exit now:')
        if exit == 'yes':
            print('welcome again')
            flag = False
        else:
            show_shopping()   # show
            your_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 salary
    shopping_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_money
    print('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 = False
    else:
        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 sys

u = 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')
        break

    else:
        print('login fail')
else:
    print('its more time')


11、凯撒秘密逻辑代码
a ='xyz'         #凯撒密码的原理就是这样,虽然很简单
b = 3
a = a.lower()
for i in a:
    ascll = ord(i)
    ascll += b
    if ascll >= 122:
        ascll -= 26
        print(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:
        continue

    if your_choice in current_layer:
        father_list.append(current_layer) # 在进入下一级之前,将上一级的内容存储在一个列表中,用于用户的返回上一级的操作
        current_layer = current_layer[your_choice]   # 变量重新赋值
    elif your_choice == 'exit':   # 直接退出的标志位
        print('welcome again!')
        flag = False
   elif 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:
        continue

    if your_choice in current_layer:
        father_list.append(current_layer) # 在进入下一级之前,将上一级的内容存储在一个列表中,用于用户的返回上一级的操作
        current_layer = current_layer[your_choice]   # 变量重新赋值
    elif your_choice == 'exit':   # 直接退出的标志位
        print('welcome again!')
        flag = False
   elif 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 += 1
       if 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 add


def twelve(num0):
    little_list = []            # 将数字转换为十二进制并且将每个数字相加
    while num0 >= 12:
        num = num0 % 12
        num0 = num0 // 12
        little_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 += i
        elif i == 'x':
            continue
        else:
            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_time return run_time # this is source function ,need add some new functions now return inner @show_time # 对原函数名进行重新赋值 @show_time = (add = show_time(add)) def add(x, y): # 原函数 result = x + y time.sleep(3) return result print(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 inner

    return login

@login_type('jindong')
def home():  # home page source function
    print('welcome to home page!')

@login_type('weixin')
def book():   # book page source function
    print('welcome to book page!')

@login_type('jindong')
def foot():   # foot page source function
    print('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 = False
    elif len(user_choice) == 0:
        continue
    else:
        print('input error!')
        continue

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

import random

random_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 os

def 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_string


while '(' 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_sub


print('answer : %s' % test_string)   # 最后输出最终的答案

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

class bar:
    def __init__(self, page):

        try:    # 如果输入不是一个数字,则返回第一页的内容
            self.page = int(page)
        except Exception as e:
            self.page = 1

    @property
    def start(self):
        start_page = (self.page-1) * 10
        return start_page

    @property
    def end(self):
        end_page = self.page * 10
        return end_page

test_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 random

import time

class 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就退出通信
        break
    last_info = ' '.join([your_enter, str(datetime.datetime.now())])
    conn.send(bytes(last_info, 'utf8'))
    resquests = conn.recv(1024)
    if not resquests:       # 因为对端如果发送的是exit,那么就不发送消息,所以发现对端没有发送消息就退出
        break
    print(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:
        break
    print(str(requests, 'utf8'))
    your_enter = input('your enter:')
    if your_enter == 'exit':              # 如果用户输入exit就退出通信
        break
    last_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 socket

client = 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 += data
    print(str(end_info, 'gbk'))

 27、爬取英雄联盟的皮肤

import json
import re
import requests

def 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 循环得到每个英雄的id
        for i in range(20):      # 每个英雄循环20次,代表20个皮肤
            num = str(i)
            face_id = ''
            if len(num) == 1:    # 如果皮肤位的数字是个位数那么就需要在皮肤前面加上两个零,因为皮肤位固定是三位数
                face_id = '00' + num
            elif len(num) == 2:
                face_id = '0' + num
            hero_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 = 0
    for hero in hero_face_url_list:
        res = requests.get(hero)
        n += 1
        if 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 = 0
        while 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 = 0
        while send_size != file_size: # 循环读取文件,并且发送
            data = file.read(1024)
            client.send(data)
            send_size += len(data)
    print('上传成功')

 30、ftp实现

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

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

# server端

import socketserver

import subprocess



BASEDIR = 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]是需要传送文件的size
                    accept_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 os

import socket

import sys

BASEDIR = 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('服务端验证成功')
        break
    else:
        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))
        continue
    elif 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_info
        with open(download_dir, 'ab') as file:

            file.write(download_file)

            print('下载成功')
        continue
    client.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_info

    print(str(end_info, 'gbk'))

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

# 第一种
import
urllib.request url = '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.request

def 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 data

url = '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.request

def 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 data

url = "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) / length
    return 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 cv2

sendMsg = 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 flag
    message = 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 = 1
            itchat.send("消息助手已开启", 'filehelper')
        if message == 'astc':
            flag = 0
            itchat.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]  # 获取每个图片的name
            pic_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 += 1
            print(pic_sum)  # 打印图片总数
if __name__ == '__main__':
    get_pic(162)

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

 
import urllib.request
import re


def 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()  # 创建一个集合用于去掉重复的id
        for 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   # 拼接好每则笑话的url
            joke_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.request
        proxy = 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的rule
                title_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 = ''    # 用于拼接文章name
                    for 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 += words
                    article = 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 tkFont

def 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 = True
    output_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:
            break

    print(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

 

posted @ 2018-05-27 23:00  Mrs.kang  阅读(1985)  评论(0编辑  收藏  举报