当前位置:   article > 正文

python实战项目_driver.find_element_by_css_selector('button.questi

driver.find_element_by_css_selector('button.questionmainaction').click()

1.爬取知乎图片

  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.没事的时候听两个机器人聊天

  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.分析唐诗的作者是李白还是杜甫

  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

  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.自动写检讨书

  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.屏幕录像机,抓屏软件

  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动图

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

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读