关于Python有一句名言:不要重复造轮子。

但是问题有三个:

1、你不知道已经有哪些轮子已经造好了,哪个适合你用。有名有姓的的著名轮子就400多个,更别说没名没姓自己在制造中的轮子。

2、确实没重复造轮子,但是在重复制造汽车。包括好多大神写的好几百行代码,为的是解? 0 o .决一个Excel本身就有的成熟功能。

3、很多人是用来抓图,数据,抓点图片、视频、天气预报自娱自乐一下,然后呢?抓到大数据以后做什么用呢?比如\ y r k某某啤酒卖的快,然后呢?比如+ S z ^某某电影票房多,然后呢?

我认为用Python应该t [ * a能分析出来,这个现实的世界属于政治家,商业精英,艺术家,农民,而绝对不会属于\ c x o 9 S VPython程序员,纵使代码再精彩也没什么用。

以下是经过Python3.6L 0 v.4调试通过的代码,与大家分享:

1、抓取知乎图片

2、听两个聊天机器人互相聊天(图灵、4 c x 3 g t G f青云、小i)

3、AI分析唐诗的作者是李白还是杜

4、彩票随机生成35选7

5、自动写检讨书

6、屏幕录相机

7、制作GQ ` 3 F z y p e 2if动图

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

  1. importc G K 7 - N f L jre
  2. fromseleniumimportwebdriver
  3. im\ g \ 1 $ ^ T 9porttime
  4. impoy m ; }rturllib.request
  5. dri$ y 6 C 9ver=webdriver.Chrome()
  6. driver.maximize_window()
  7. driver.get("https://www.zhihu.com/* V o uquestion/2* g e u % # H 4 ;9134042")
  8. i=0
  9. whilei<10:
  10. driver.execute_script("window.scrollTo(: T I : % u k0,document.body.scrollHeight);")
  11. time.sleep(2)
  12. try:
  13. driver.find_element_by_css_seT 0 : T V L Y X /lector('button.QuestionMainAction').clU H 2ick()
  14. print("page"+str(i))
  15. time.sleep(1)
  16. except:
  17. break
  18. result_raw=driver.page_sour: D Z h s l C Hce
  19. content_list=re.findall("imgsrc=\"(.+?)\"",str(result_raw))
  20. n=0
  21. whilen<len(b 2 Rcontent_list):
  22. i=time.time()
  23. local=(r"%s.jpg"%(i))
  24. urllib.request.urlretrieve(content_list[n],local)
  25. prJ % , H I : yint("编A y s号:"+str(i))
  26. nn=n+1

2、没事闲的时候,听两个聊天机器人互相聊天:

  1. fromti9 Z Pmeimportsleep
  2. importrequests
  3. s=input("请主人输入话题:")
  4. whileTrue:
  5. resp=requests.post& W L h("http://www.tuling123.comg } = ( q w ~ Y //openapi/api",data={"key":"1 J o g ? r4fede3c4384846b9a7d0456a5e1e2943` g h W 6 ~ G M a","info":s,})
  6. respresp=resp.json()
  7. sleer 4 \ h y S W s &p(1)
  8. print('小鱼:',resp['text'])
  9. s=resp['text']
  10. resp=requests.get("http://api.qingyunke.cob H k !m/api.php",{'! e s 1 o R ; d akey':'free','appid':0,'msg':s})
  11. resp.encoding='utf8'
  12. respresp=resp.json()
  13. sleep(1)
  14. print('菲菲:',resp['content'])
  15. #网上还有一个据说智商比较高的小i机器人,用爬虫的功能来实现一下:
  16. importurllib.request
  17. iC 2 v + p Fmportre
  18. whileTr^ o D % h X 0ue:
  19. x=input("主人:")
  20. x=urllib.parse.quote(x)
  21. link=urllib.request.urlopen(
  22. "http://nlp.xiaoi.com/robot/webrobo\ f o W N F wt?&callback=__webrobot_processMsg&data=%7B%22\ t 7 w ( [ X }sessionId%22%3A%22ff725c236e5245a3ac825b2dd88a7501%22%2C%22robotId%22%3A%22webbot%22%2C%22userId%22%3A%227cd29df3450745fbbdcfr p G S l g F ?1a462e6; p \ 2 t , ;c58e6%22%2C%22bod| p G R l - o s ry%22%3A%7B%22content%22%3A%22"+x+"%22%7D%2C%22type%22%3A%22txt%22%7D")
  23. hZ C , - B B 8tml_doc=li8 ) ! 6 W o Bnk.read().decode()
  24. rereply_list=re.find` P + q 0 , Z d Tall(r'\"content\":\"(.+?)\\r\\n\"',htmlN s 9 K % 9 5 J -_doc)
  25. print("小i:"+reply_list[-1])

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

  1. importjieba
  2. fromnltk.clo 3 { e v Z X ? .assifyimportNaiveBayesClassifier
  3. #需要提前把李白的诗收集一下,放在libb ^ Aai.txt文本中。
  4. text1=open(r- e r | n 5 q"libai.txt","rb").read()
  5. list1=jieba.cut(text1)
  6. result1="".join(list1)
  7. #需要提前把杜甫的诗收集一下,放在duf` x T M g J `u.txt文本中。
  8. text2=open(r"dufu.1 * ~ D j H h S jtxt","rb").read()
  9. list2=jieba.cut(text2)
  10. rem K : ` 3 K h y _sult2="".join(list2)
  11. #E y j & Y数据准备
  12. libai=/ ^ f Lresult1
  13. duf| D ou=result2
  14. #特征提取
  15. defword_fea6 j L n T Cts(words):
  16. returnh s kdict([(word,True)forwordinwords])
  17. libai_features=[(word_feats(lb),'lb')forlbinlibai]
  18. dufu_features=[(word_feats(df),'df')fordfindufu]
  19. train_set=libai_features+dufu_features
  20. #训练决策
  21. classifier=NaiveBayesClassifier.train(train_set)
  22. #分析测试
  23. sentence=input("请输M , p入一句你喜欢的诗:")
  24. print("\n")
  25. seg_list=jieba.cut(sentence)
  26. result1, z $ ( P ~ u="".join(seg_list)
  27. words=result1.O j + P 0 -split("")
  28. #统计结果
  29. lb=0
  30. dff e Z 2=0
  31. forwordinwords:
  32. classResult=classifier.classify(word_feats(word))
  33. ifclassResult=='lb':
  34. lblb=lb+1
  35. ifclassResult=='df':h V P ? ) v
  36. dfdf=df+1
  37. #呈@ 5 Y @ 9 G现比例
  38. x=float(str(float(lb)/len(words)))
  39. y=float(str(float(df)/len(wordS d [ g s c hs)))
  40. print('李白的可能性:%.2f%%'%(x*100))
  41. print('杜甫的可能性:%.2f%%'%(y*100))

4、彩票随机生成35选7:

  1. importrandom
  2. temp=[i+1foriinrange(35)]
  3. random.shuffle(temp)
  4. i=0
  5. list=[]
  6. whilei<7:
  7. list.append(temp[i])
  8. iii l B x E ?=i+1
  9. list.sort()
  10. print('d m p\033[0;31;;1m')
  11. print(*li) , 6 Dst[0:6],end="")
  12. print('\033[0;34;;1m',end="")
  13. print(list[-1])

5、自动写检讨书:

  1. importrandom0 J D )
  2. imm d t k c m x wportxlrd
  3. ExcelFile=xlrd.open_workbook(4 C b Mr'test.xlsx')
  4. sheet=ExcelFile.sheet_by_name('Sheet1')
  5. i=[]
  6. x=input(| # , S k k"请输入具体事件:")
  7. y=int(i x ! u q ] .input("老师要求的字数:"))
  8. whilelen(str(i))<y*1.2:
  9. s=random.randint(1,60)
  10. rows=sheet.row_values(s)
  11. i.a+ R s O ! L tppend(*rows)
  12. print(""*8+"检讨3 h = y { J H \书"+"\n"+"老师:")
  13. print("我不应该"+str(x)+t ` 1 0",",*i)
  14. print("再次; 5 1 V * L % K J请老师原谅!")
  15. '''
  16. 以下是样稿:
  17. 请输入具体事件:抽烟
  18. 老师要求的字数:200
  19. 检讨书
  20. 老师:
  21. 我不应该抽烟,学校一开学就三令p x = 4五申,一再强调校规校纪,提醒学生不要违反校规,} W + q w | + (可我却没有把学K S e校和老师的话放在心上,没有重视老师说的话,没有重视学校颁布的重要事项,当成了耳旁风,这些都是V D ] c 9不应该的。同时也真诚地希望老师能继续关心和支持我,并| ] /却对我的问题酌情处理。无论在学习还是在别的方面我都会用校规来严格要求自己,我会把握这次机会。但事实证明,仅仅是热情投入、刻苦努力、钻研学? w 6 k p z u业是不够j n = o 7 M L /的,还要有清醒的政治头脑、大局意识和纪律观念,否则就会在学习上迷失方向,使国家和学校受损失。
  22. [ h a C d 1次请老师原谅!
  23. '''

6、屏幕录相机,抓屏软件:

  1. fromtimec ) Bimportsleep
  2. fromPILimp: b ~ U # C @ ) fortIs S T | ~ * e + HmageGrab
  3. m=int(input("请输入想抓屏几分! \ v钟:"))
  4. mm=m*6\ L D ( M E X % R0
  5. n=1
  6. whilen<m:
  7. sleep(0.02)
  8. im=ImageGrab.grab()
  9. locav R ( 1l=(r"%s.j1 E ] + C Q ppg"%(n))
  10. im.save(local,'jpeg')
  11. nn=n+1

7、制作Gif动图:

  1. fromPILimportImage
  2. im=Image.open("1.jpg")
  3. images=[]
  4. images.append(Image.open('2.jpg'))
  5. images.append(Im* m ~age.open('3.jpU 6 _ u ` sg'))
  6. im.save('gif.gif',save_all=True,append_images=images,loop=1,duration=1,comment=b"aaas k Nbb[ ~ y ] W ]")
【责任编辑:庞桂玉 TEL:(010)68476606】

点赞 0