关于Python有一句名言:不要重复造轮子。但是问题有三个:
1、你不知道已经有哪些轮子已经造好了,哪个适合你用。有名有姓的的著名轮子就400多个,更别说没名没姓自己在制造中的轮子。
2、确实没重复造轮子,但是在重复制造汽车。包括好多大神写的好几百行代码,为的是解决一个Excel本身就有的成熟功能。
3、很多人是用来抓图,数据,抓点图片、视频、天气预报自娱自乐一下,然后呢?抓到大数据以后做什么用呢?比如某某啤酒卖的快,然后呢?比如某某电影票房多,然后呢?
以下是经过Python3.6.4调试通过的代码,与大家分享:
1、抓取知乎图片
2、听两个聊天机器人互相聊天
3、AI分析唐诗的作者是李白还是杜甫
4、彩票随机生成35选7
5、自动写检讨书
6、屏幕录相机
7、制作Gif动图
① 抓取知乎图片,只用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
② 没事闲的时候,听两个聊天机器人互相聊天
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\])
③ 分析唐诗的作者是李白还是杜甫
import jieba
from nltk.classify import NaiveBayesClassifier
# 需要提前把李白的诗收集一下,放在libai.txt文本中。
text1 = open(rlibai.txt, rb).read()
list1 = jieba.cut(text1)
result1 = .join(list1)
# 需要提前把杜甫的诗收集一下,放在dufu.txt文本中。
text2 = open(rdufu.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))
④ 彩票随机生成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\])
⑤ 自动写检讨书
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
检讨书
老师:
我不应该抽烟, 学校一开学就三令五申,一再强调校规校纪,提醒学生不要违反校规,可我却没有把学校和老师的话放在心上,没有重视老师说的话,没有重视学校颁布的重要事项,当成了耳旁风,这些都是不应该的。同时也真诚地希望老师能继续关心和支持我,并却对我的问题酌情处理。 无论在学习还是在别的方面我都会用校规来严格要求自己,我会把握这次机会。 但事实证明,仅仅是热情投入、刻苦努力、钻研学业是不够的,还要有清醒的政治头脑、大局意识和纪律观念,否则就会在学习上迷失方向,使国家和学校受损失。
再次请老师原谅!
'''
⑥ 屏幕录相机,抓屏软件
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
⑦ 制作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=baaabb)
由于文章篇幅有限,文档资料内容较多,需要这些文档的朋友,可以加小助手微信免费获取,【保证100%免费】,中国人不骗中国人。
**(扫码立即免费领取)**
全套Python学习资料分享:
一、Python所有方向的学习路线
Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
二、学习软件
工欲善其事必先利其器。学习Python常用的开发软件都在这里了,还有环境配置的教程,给大家节省了很多时间。
三、全套PDF电子书
书籍的好处就在于权威和体系健全,刚开始学习的时候你可以只看视频或者听某个人讲课,但等你学完之后,你觉得你掌握了,这时候建议还是得去看一下书籍,看权威技术书籍也是每个程序员必经之路。
四、入门学习视频全套
我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。
五、实战案例
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
如有侵权,请联系删除。