分享7个 Python 实战项目练习


Posted in Python onMarch 03, 2022

但是问题有三个:

  • 1、我们不知道已经有哪些轮子已经造好了,哪个适合你用。有名有姓的的著名轮子就400多个,更别说没名没姓自己在制造中的轮子。
  • 2、确实没重复造轮子,但是在重复制造汽车。包括好多大神写的好几百行代码,为的是解决一个Excel本身就有的成熟功能。
  • 3、很多人是用来抓图,数据,抓点图片、视频、天气预报自娱自乐一下,然后呢?抓到大数据以后做什么用呢?比如某某啤酒卖的快,然后呢?比如某某电影票房多,然后呢?

以下是经过Python3.6.4调试通过的代码,与大家分享:

  • 1、抓取知乎图片
  • 2、听两个聊天机器人互相聊天
  • 3、AI分析唐诗的作者是李白还是杜甫
  • 4、彩票随机生成35选7
  • 5、自动写检讨书
  • 6、屏幕录相机
  • 7、制作Gif动图

1.抓取知乎图片,只用30行代码

from selenium import webdriver
import time
import urllib.request

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.zhihu.com/question/29134042")
i = 0
while i < 10:
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(2)
    try:
        driver.find_element_by_css_selector('button.QuestionMainAction').click()
        print("page" + str(i))
        time.sleep(1)
    except:
        break
result_raw = driver.page_source
content_list = re.findall("img src=\"(.+?)\" ", str(result_raw))
n = 0
while n < len(content_list):
    i = time.time()
    local = (r"%s.jpg" % (i))
    urllib.request.urlretrieve(content_list[n], local)
    print("编号:" + str(i))
    n = n + 1

2.听两个聊天机器人互相聊天

from time import sleep
import requests
s = input("请主人输入话题:")
while True:
    resp = requests.post("http://www.tuling123.com/openapi/api",data={"key":"4fede3c4384846b9a7d0456a5e1e2943", "info": s, })
    resp = resp.json()
    sleep(1)
    print('小鱼:', resp['text'])
    s = resp['text']
    resp = requests.get("http://api.qingyunke.com/api.php", {'key': 'free', 'appid':0, 'msg': s})
    resp.encoding = 'utf8'
    resp = resp.json()
    sleep(1)
    print('菲菲:', resp['content'])
#网上还有一个据说智商比较高的小i机器人,用爬虫的功能来实现一下:

import urllib.request
import re

while True:
    x = input("主人:")
    x = urllib.parse.quote(x)
    link = urllib.request.urlopen(
        "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")
    html_doc = link.read().decode()
    reply_list = re.findall(r'\"content\":\"(.+?)\\r\\n\"', html_doc)
    print("小i:" + reply_list[-1])

3.分析唐诗的作者是李白还是杜甫

import jieba
from nltk.classify import NaiveBayesClassifier

# 需要提前把李白的诗收集一下,放在libai.txt文本中。
text1 = open(r"libai.txt", "rb").read()
list1 = jieba.cut(text1)
result1 = " ".join(list1)
# 需要提前把杜甫的诗收集一下,放在dufu.txt文本中。
text2 = open(r"dufu.txt", "rb").read()
list2 = jieba.cut(text2)
result2 = " ".join(list2)

# 数据准备
libai = result1
dufu = result2


# 特征提取
def word_feats(words):
    return dict([(word, True) for word in words])


libai_features = [(word_feats(lb), 'lb') for lb in libai]
dufu_features = [(word_feats(df), 'df') for df in dufu]
train_set = libai_features + dufu_features
# 训练决策
classifier = NaiveBayesClassifier.train(train_set)

# 分析测试
sentence = input("请输入一句你喜欢的诗:")
print("\n")
seg_list = jieba.cut(sentence)
result1 = " ".join(seg_list)
words = result1.split(" ")

# 统计结果


lb = 0
df = 0
for word in words:
    classResult = classifier.classify(word_feats(word))
    if classResult == 'lb':
        lb = lb + 1
    if classResult == 'df':
        df = df + 1

# 呈现比例
x = float(str(float(lb) / len(words)))
y = float(str(float(df) / len(words)))
print('李白的可能性:%.2f%%' % (x * 100))
print('杜甫的可能性:%.2f%%' % (y * 100))

4.彩票随机生成35选7

import random

temp = [i + 1 for i in range(35)]
random.shuffle(temp)
i = 0
list = []
while i < 7:
    list.append(temp[i])
    i = i + 1
list.sort()
print('\033[0;31;;1m')
print(*list[0:6], end="")
print('\033[0;34;;1m', end=" ")
print(list[-1])

5.自动写检讨书

import random
import xlrd

ExcelFile = xlrd.open_workbook(r'test.xlsx')
sheet = ExcelFile.sheet_by_name('Sheet1')
i = []
x = input("请输入具体事件:")
y = int(input("老师要求的字数:"))
while len(str(i)) < y * 1.2:
    s = random.randint(1, 60)
    rows = sheet.row_values(s)
    i.append(*rows)
print(" "*8+"检讨书"+"\n"+"老师:")
print("我不应该" + str(x)+",", *i)
print("再次请老师原谅!")
'''

以下是样稿:

请输入具体事件:抽烟
老师要求的字数:200
        检讨书

老师:

我不应该抽烟, 学校一开学就三令五申,一再强调校规校纪,提醒学生不要违反校规,可我却没有把学校和老师的话放在心上,没有重视老师说的话,没有重视学校颁布的重要事项,当成了耳旁风,这些都是不应该的。同时也真诚地希望老师能继续关心和支持我,并却对我的问题酌情处理。 无论在学习还是在别的方面我都会用校规来严格要求自己,我会把握这次机会。 但事实证明,仅仅是热情投入、刻苦努力、钻研学业是不够的,还要有清醒的政治头脑、大局意识和纪律观念,否则就会在学习上迷失方向,使国家和学校受损失。
再次请老师原谅!
'''

6.屏幕录相机,抓屏软件

from time import sleep
from PIL import ImageGrab

m = int(input("请输入想抓屏几分钟:"))
m = m * 60
n = 1
while n < m:
    sleep(0.02)
    im = ImageGrab.grab()
    local = (r"%s.jpg" % (n))
    im.save(local, 'jpeg')
    n = n + 1

7.制作Gif动图

from PIL import Image

im = Image.open("1.jpg")
images = []
images.append(Image.open('2.jpg'))
images.append(Image.open('3.jpg'))
im.save('gif.gif', save_all=True, append_images=images, loop=1, duration=1, comment=b"aaab

到此这篇关于分享7个 Python 实战项目代码的文章就介绍到这了,更多相关 Python 实战项目内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
py2exe 编译ico图标的代码
Mar 08 Python
python多线程抓取天涯帖子内容示例
Apr 03 Python
Python遍历zip文件输出名称时出现乱码问题的解决方法
Apr 08 Python
Python遍历指定文件及文件夹的方法
May 09 Python
python实现决策树ID3算法的示例代码
May 30 Python
python中plot实现即时数据动态显示方法
Jun 22 Python
Python简单过滤字母和数字的方法小结
Jan 09 Python
Python基础之循环语句用法示例【for、while循环】
Mar 23 Python
python实现多进程通信实例分析
Sep 01 Python
python如何代码集体右移
Jul 20 Python
Python 高效编程技巧分享
Sep 10 Python
用Python远程登陆服务器的步骤
Apr 16 Python
python工具dtreeviz决策树可视化和模型可解释性
Python GUI编程之tkinter 关于 ttkbootstrap 的使用详解
python机器学习实现oneR算法(以鸢尾data为例)
Mar 03 #Python
详解python的异常捕获
Mar 03 #Python
分享提高 Python 代码的可读性的技巧
Mar 03 #Python
使用python创建股票的时间序列可视化分析
Python Pandas读取Excel日期数据的异常处理方法
You might like
vBulletin HACK----关于排版的两个HACK
2006/10/09 PHP
php中用date函数获取当前时间有误的解决办法
2013/08/02 PHP
PHP实现转盘抽奖算法分享
2020/04/15 PHP
基于PHP的微信公众号的开发流程详解
2020/08/07 PHP
CL vs ForZe BO5 第三场 2.13
2021/03/10 DOTA
优化JavaScript脚本的性能的几个注意事项
2006/12/22 Javascript
Javascript - HTML的request类
2007/01/09 Javascript
JavaScript学习笔记之获取当前目录的实现代码
2010/12/14 Javascript
jQuery学习笔记 操作jQuery对象 CSS处理
2012/09/19 Javascript
js 调用本地exe的例子(支持IE内核的浏览器)
2012/12/26 Javascript
JavaScript简介
2015/02/15 Javascript
浅析JSONP技术原理及实现
2016/06/08 Javascript
js+div+css下拉导航菜单完整代码分享
2016/12/28 Javascript
ExtJs异步无法向外传值和赋值的完美解决办法
2017/06/14 Javascript
微信小程序实现animation动画
2018/01/26 Javascript
JS图片懒加载的优点及实现原理
2020/01/10 Javascript
python清除字符串里非字母字符的方法
2015/07/02 Python
详谈python http长连接客户端
2017/06/12 Python
python识别文字(基于tesseract)代码实例
2019/08/24 Python
python opencv将表格图片按照表格框线分割和识别
2019/10/30 Python
PyTorch 解决Dataset和Dataloader遇到的问题
2020/01/08 Python
Django 删除upload_to文件的步骤
2020/03/30 Python
基于PyTorch的permute和reshape/view的区别介绍
2020/06/18 Python
Python爬虫破解登陆哔哩哔哩的方法
2020/11/17 Python
全球度假村:Club Med
2017/11/27 全球购物
Geekbuying波兰:购买中国电子产品
2019/10/20 全球购物
美体小铺法国官方网站:The Body Shop法国
2020/06/04 全球购物
英国婴儿产品专家:Samuel Johnston
2020/04/20 全球购物
自我鉴定三原则
2014/01/13 职场文书
公司经理任命书
2014/06/05 职场文书
团组织推优材料
2014/12/29 职场文书
行政助理岗位职责
2015/02/10 职场文书
大学生个人学习总结
2015/02/15 职场文书
法院个人总结
2015/03/03 职场文书
毕业实习证明范本
2015/06/16 职场文书
JavaScript实现音乐播放器
2022/08/14 Javascript