Python 練習小代碼

 

代碼目錄  

renturn tophtml

renturn downweb

一、斐波那契數列的實現ajax

二、猜年齡(code)正則表達式

三、求最大值和最小值shell

四、打印出一個九九乘法表:編程

五、求最大公約數的個數json

六、一個購物車程序:api

七、購物車1.0瀏覽器

八、 輸出一個矩形服務器

九、超級low的一個登錄接口

十、for ... else 的實例

十一、凱撒祕密邏輯代碼

十二、給定兩個正整數a和b,將該小數點後第x位到第y位的數字當作密碼

1三、三級菜單

1四、在文件中的某一行後面加上一段字符串

1五、對一個文件中的內容進行修改

1六、若爲py數,輸出‘yes’,不然輸出‘no’

1七、將日誌寫入文件的函數

1八、斐波那契簡單的實現列表的實現方式

1九、裝飾器函數實例

20、用裝飾器寫的一個登陸頁面程序

2一、隨機數模塊生成隨機驗證碼

2二、實現日誌寫入的函數

2二、計算機的最low實現(求改)

2三、面向對象編程,實現頁面分頁(屬性)

2四、利用字符串調用模塊中的函數

2五、b站主播間狂刷彈幕

2五、socket實現不間斷通訊

2六、socket執行cmd命令

2七、爬取英雄聯盟的皮膚

2八、文件上傳

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)

 

二、猜年齡(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!')

 

三、求最大值和最小值(支持用戶輸入數字)

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

四、打印出一個九九乘法表:
for i in range(1,10):
    for j in range(1,i+1):     # 對於九九乘法表要注意他的每行,是第一個數在遞增
        print('%d * %d = %d' % (j, i, i*j), end='    ')     # end的方法是將在本次循環中的輸出的全部內容在一行輸出
    print()   # 至關於一個換行符的做用
 

五、求最大公約數的個數

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))

 

六、一個購物車程序:

這是一個簡單的購物程序

 

實現的功能有:

 

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()
 
七、購物車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

八、 輸出一個矩形
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)

 


九、超級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)

十、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')


十一、凱撒祕密邏輯代碼
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='')
 

 

十二、給定兩個正整數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)

1三、三級菜單
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!')

 

1四、在文件中的某一行後面加上一段字符串
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!')

 


1五、對一個文件中的內容進行修改
方法:
  內存的機制只能支持從一個文件的最後進行寫入的操做,可是不支持修改,和插入式的添加
  因此若是咱們須要修改文件中的內容的話,只能從以前的文件中讀出全部的內容,而後在將更改以後的內容,寫入另外一個文件(這種方法存在很大的弊端)
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)

 

1六、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

 


1七、將日誌寫入文件的函數
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))

 


1八、斐波那契簡單的實現列表的實現方式
def fibs(num):
    result = [0, 1]   # 將斐波那契的前兩個數先存在列表中
    for times in range(num - 2):      # 控制循環的次數
        result.append(result[-1] + result[-2])  # 將斐波那契數列的最後的兩位數字相加,而後把結果存在列表中
    return result
print(fibs(10))

1九、裝飾器函數實例(保證封閉原則的狀況下,實現功能的擴展)
 
 
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

 2一、隨機數模塊生成隨機的驗證碼

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

2二、實現寫入日誌的函數

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)
2二、計算機最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)   # 最後輸出最終的答案

 2三、面向對象編程,實現頁面分頁(屬性)

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')

 2五、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()

 2六、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'))

 2七、爬取英雄聯盟的皮膚

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

 2八、文件上傳(代碼實現圖片的上傳,能夠上傳任何類型的文件)

#  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
相關文章
相關標籤/搜索