QQ登录

只需要一步,快速开始

APP扫码登录

只需要一步,快速开始

手机号码,快捷登录

泡泡马甲APP 更多内容请下载泡泡马甲手机客户端APP 立即下载 ×
查看: 1648|回复: 0

[Python] 7个Python实战项目代码,让你分分钟晋级大神!

[复制链接]

等级头衔

积分成就    金币 : 2806
   泡泡 : 1516
   精华 : 6
   在线时间 : 1244 小时
   最后登录 : 2024-5-5

丰功伟绩

优秀达人突出贡献荣誉管理论坛元老

联系方式
发表于 2021-6-25 11:25:00 | 显示全部楼层 |阅读模式
       关于Python有一句名言:不要重复造轮子。但是问题有三个:. D6 B) N2 M9 e& Q, n+ p) B# a9 I
  • 你不知道已经有哪些轮子已经造好了,哪个适合你用。有名有姓的的著名轮子就400多个,更别说没名没姓自己在制造中的轮子。
  • 确实没重复造轮子,但是在重复制造汽车。包括好多大神写的好几百行代码,为的是解决一个Excel本身就有的成熟功能。
  • 很多人是用来抓图,数据,抓点图片、视频、天气预报自娱自乐一下,然后呢?抓到大数据以后做什么用呢?比如某某啤酒卖的快,然后呢?比如某某电影票房多,然后呢?
    ! ?' j6 C) h" v; X0 {7 F
       用Python应该能分析出来,这个现实的世界属于**家,商业精英,艺术家,农民,而绝对不会属于Python程序员,纵使代码再精彩也没什么用。以下是经过Python3.6.4调试通过的代码,与大家分享:" C# Q+ `8 h2 a, y
1、抓取知乎图片,只用30行代码:; w' `2 l$ Q+ H2 G# o9 D
  1. import re
  2. from selenium import webdriver
  3. import time
  4. import urllib.request
  5. driver = webdriver.Chrome()
  6. driver.maximize_window()
  7. driver.get("https://www.zhihu.com/question/29134042")
  8. i = 0
  9. while i < 10:
  10.     driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
  11.     time.sleep(2)
  12.     try:
  13.         driver.find_element_by_css_selector('button.QuestionMainAction').click()
  14.         print("page" + str(i))
  15.         time.sleep(1)
  16.     except:
  17.         break
  18. result_raw = driver.page_source
  19. content_list = re.findall("img src="(.+?)" ", str(result_raw))
  20. n = 0
  21. while n < len(content_list):
  22.     i = time.time()
  23.     local = (r"%s.jpg" % (i))
  24.     urllib.request.urlretrieve(content_list[n], local)
  25.     print("编号:" + str(i))
  26.     n = n + 1
2、没事闲的时候,听两个聊天机器人互相聊天:  u: r" J5 P9 ^: M
  1. from time import sleep
  2. import requests
  3. s = input("请主人输入话题:")
  4. while True:
  5.     resp = requests.post("http://www.tuling123.com/openapi/api",data={"key":"4fede3c4384846b9a7d0456a5e1e2943", "info": s, })
  6.     resp = resp.json()
  7.     sleep(1)
  8.     print('小鱼:', resp['text'])
  9.     s = resp['text']
  10.     resp = requests.get("http://api.qingyunke.com/api.php", {'key': 'free', 'appid':0, 'msg': s})
  11.     resp.encoding = 'utf8'
  12.     resp = resp.json()
  13.     sleep(1)
  14.     print('菲菲:', resp['content'])
  15. #网上还有一个据说智商比较高的小i机器人,用爬虫的功能来实现一下:
  16. import urllib.request
  17. import re
  18. while True:
  19.     x = input("主人:")
  20.     x = urllib.parse.quote(x)
  21.     link = urllib.request.urlopen(
  22.         "http://nlp.xiaoi.com/robot/webrobot?&callback=__webrobot_processMsg&data=%7B%22sessionId%22%3A%22ff725c236e5245a3ac825b2dd88a7501%22%2C%22robotId%22%3A%22webbot%22%2C%22userId%22%3A%227cd29df3450745fbbdcf1a462e6c58e6%22%2C%22body%22%3A%7B%22content%22%3A%22" + x + "%22%7D%2C%22type%22%3A%22txt%22%7D")
  23.     html_doc = link.read().decode()
  24.     reply_list = re.findall(r'"content":"(.+?)\\r\\n"', html_doc)
  25.     print("小i:" + reply_list[-1])
3、分析唐诗的作者是李白还是杜甫:& O/ I  ?- Y7 Z4 t
  1. import jieba
  2. from nltk.classify import NaiveBayesClassifier
  3. # 需要提前把李白的诗收集一下,放在libai.txt文本中。
  4. text1 = open(r"libai.txt", "rb").read()
  5. list1 = jieba.cut(text1)
  6. result1 = " ".join(list1)
  7. # 需要提前把杜甫的诗收集一下,放在dufu.txt文本中。
  8. text2 = open(r"dufu.txt", "rb").read()
  9. list2 = jieba.cut(text2)
  10. result2 = " ".join(list2)
  11. # 数据准备
  12. libai = result1
  13. dufu = result2
  14. # 特征提取
  15. def word_feats(words):
  16.     return dict([(word, True) for word in words])
  17. libai_features = [(word_feats(lb), 'lb') for lb in libai]
  18. dufu_features = [(word_feats(df), 'df') for df in dufu]
  19. train_set = libai_features + dufu_features
  20. # 训练决策
  21. classifier = NaiveBayesClassifier.train(train_set)
  22. # 分析测试
  23. sentence = input("请输入一句你喜欢的诗:")
  24. print("\n")
  25. seg_list = jieba.cut(sentence)
  26. result1 = " ".join(seg_list)
  27. words = result1.split(" ")
  28. # 统计结果
  29. lb = 0
  30. df = 0
  31. for word in words:
  32.     classResult = classifier.classify(word_feats(word))
  33.     if classResult == 'lb':
  34.         lb = lb + 1
  35.     if classResult == 'df':
  36.         df = df + 1
  37. # 呈现比例
  38. x = float(str(float(lb) / len(words)))
  39. y = float(str(float(df) / len(words)))
  40. print('李白的可能性:%.2f%%' % (x * 100))
  41. print('杜甫的可能性:%.2f%%' % (y * 100))
4、彩票随机生成35选7:  f+ ^1 ]; T- y. I" L3 i! i$ R1 }1 s
  1. import random
  2. temp = [i + 1 for i in range(35)]
  3. random.shuffle(temp)
  4. i = 0
  5. list = []
  6. while i < 7:
  7.     list.append(temp[i])
  8.     i = i + 1
  9. list.sort()
  10. print('\033[0;31;;1m')
  11. print(*list[0:6], end="")
  12. print('\033[0;34;;1m', end=" ")
  13. print(list[-1])
5、自动写检讨书:) [8 \) O" M4 j% q6 N( @  T& ?
  1. import random
  2. import xlrd
  3. ExcelFile = xlrd.open_workbook(r'test.xlsx')
  4. sheet = ExcelFile.sheet_by_name('Sheet1')
  5. i = []
  6. x = input("请输入具体事件:")
  7. y = int(input("老师要求的字数:"))
  8. while len(str(i)) < y * 1.2:
  9.     s = random.randint(1, 60)
  10.     rows = sheet.row_values(s)
  11.     i.append(*rows)
  12. print(" "*8+"检讨书"+"\n"+"老师:")
  13. print("我不应该" + str(x)+",", *i)
  14. print("再次请老师原谅!")
  15. '''
  16. 以下是样稿:
  17. 请输入具体事件:抽烟
  18. 老师要求的字数:200
  19.         检讨书
  20. 老师:
  21. 我不应该抽烟, 学校一开学就三令五申,一再强调校规校纪,提醒学生不要违反校规,可我却没有把学校和老师的话放在心上,没有重视老师说的话,没有重视学校颁布的重要事项,当成了耳旁风,这些都是不应该的。同时也真诚地希望老师能继续关心和支持我,并却对我的问题酌情处理。 无论在学习还是在别的方面我都会用校规来严格要求自己,我会把握这次机会。 但事实证明,仅仅是热情投入、刻苦努力、钻研学业是不够的,还要有清醒的**头脑、大局意识和纪律观念,否则就会在学习上迷失方向,使国家和学校受损失。
  22. 再次请老师原谅!
  23. '''
6、屏幕录相机,抓屏软件:* ~% ~3 P* ?& y0 v$ n2 _
  1. from time import sleep
  2. from PIL import ImageGrab
  3. m = int(input("请输入想抓屏几分钟:"))
  4. m = m * 60
  5. n = 1
  6. while n < m:
  7.     sleep(0.02)
  8.     im = ImageGrab.grab()
  9.     local = (r"%s.jpg" % (n))
  10.     im.save(local, 'jpeg')
  11.     n = n + 1
7、制作Gif动图:
' P7 }7 E% E9 V$ g; d1 Y
  1. from PIL import Image
  2. im = Image.open("1.jpg")
  3. images = []
  4. images.append(Image.open('2.jpg'))
  5. images.append(Image.open('3.jpg'))
  6. im.save('gif.gif', save_all=True, append_images=images, loop=1, duration=1, comment=b"aaabb")
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|paopaomj.COM ( 渝ICP备18007172号 )

GMT+8, 2024-5-11 00:12

Powered by paopaomj X3.4 © 2016-2024 sitemap

快速回复 返回顶部 返回列表