Python有哪些神一般的蜜汁操作?(附代码)

开发 后端
有人说,“Python除了不会生孩子,Python从撩妹到装x,无所不能!什么都会!” 下载视频?我用Python;玩跳一跳?我用Python跳到4999分;撩妹子?依然用Python;抢红包抢火车票?没错还是Python;就算是整理文件,我也还是用Python……

有人说,“Python除了不会生孩子,Python从撩妹到装x,无所不能!什么都会!”

  • 下载视频?我用Python;
  • 玩跳一跳?我用Python跳到4999分;
  • 撩妹子?依然用Python;
  • 抢红包抢火车票?没错还是Python;
  • 就算是整理文件,我也还是用Python……

下面就详细跟大家分享一些Python的骚操作:

1、Python让你不再错过抢红包

刚过完年经历了抢红包大战的大家,是不是错过了好几个亿!?

用黑科技Python,开发一个微信小助手,从此再也不用担心错过巨额红包啦!

实现代码主要有两个部分:

(1)接收红包消息,直接从手机端微信获取数据比较麻烦,主流的方法都是通过微信网页版来获取。

因为网页版的消息接口可以被抓包分析,比较容易获取和使用。

(2)发通知,最简单的通知方法就是发出声音,还嫌不够的话,可以弹个窗。

  1. # 打开手机微信 
  2. poco(text='微信').click() 
  3. #获取当前页面中所有所有群聊的名称 
  4.  群聊消息的元素标识Chat_msg = poco(name='com.tencent.mm:id/d1v').offspring('com.tencent.mm:id/b6e'
  5. # 获取当前页面中所有群聊的名称 
  6. Chat_names = [] 
  7. Chat_names = list(map(lambda x: x.get_text(), Chat_msg)) 
  8. # 指定抢红包的群聊名称 
  9. chat = input('请指定群聊名称:'
  10. if chat in Chat_names: 
  11.  index = Chat_names.index(chat) 
  12.  # 点击进入指定的群聊 
  13.  Chat_msg[index].click() 
  14. 在微信聊天页面中,获取当前页面中的所有消息元素。 
  15. msg_list = poco("android.widget.ListView").children() 
  16. #   遍历消息并查找红包 
  17. for msg in msg_list: 
  18. # 微信红包的标识 
  19.  LuckyMoney = msg.offspring('com.tencent.mm:id/aql'
  20.  # 已失效红包(比如已领取、已被领完)的标识 
  21.  Invalid = msg.offspring('com.tencent.mm:id/aqk'
  22.  # 判断红包是否有效并抢起来! 
  23.  if LuckyMoney: 
  24.  pass 
  25.     #遍历消息并查找红包 
  26. if Invalid.exists() and (Invalid.get_text()=='已领取' or Invalid.get_text()=='已被领完'): 
  27. print(f'红包已无效,跳过……'
  28.  continue 
  29. else
  30.  print(f'发现一个新红包,抢起来!'
  31.  poco("com.tencent.mm:id/d1v"
  32.  msg.click() 
  33.  click_open = poco("com.tencent.mm:id/d02"
  34.  if click_open.exists(): 
  35.  click_open.click() 
  36.  keyevent('BACK'
  37. #初始化程序 
  38. from airtest.core.api import * 
  39. auto_setup(__file__) 
  40. from poco.drivers.android.uiautomation import AndroidUiautomationPoco 
  41. poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False

2、Python帮你抢火车票

Python除了抢红包,也是抢火车票的一把好手,说不定你当年买下的黄牛贩子手里的票就是靠Python抢到的。

大家过年回家抢票是不是巨艰辛,今年过年可以写个抢票软件啦!

使用 Python3 抓取12306网站的 车票信息,及时提醒,自动下单。

  1. from splinter.browser import Browser 
  2. from time import sleep 
  3. import traceback 
  4.   
  5.   
  6. class Buy_Tickets(object): 
  7.     # 定义实例属性,初始化 
  8.     def __init__(self, username, passwd, order, passengers, dtime, starts, ends): 
  9.         self.username = username 
  10.         self.passwd = passwd 
  11.         # 车次,0代表所有车次,依次从上到下,1代表所有车次,依次类推 
  12.         self.order = order 
  13.         # 乘客名 
  14.         self.passengers = passengers 
  15.         # 起始地和终点 
  16.         self.starts = starts 
  17.         self.ends = ends 
  18.         # 日期 
  19.         self.dtime = dtime 
  20.         # self.xb = xb 
  21.         # self.pz = pz 
  22.         self.login_url = 'https://kyfw.12306.cn/otn/login/init' 
  23.         self.initMy_url = 'https://kyfw.12306.cn/otn/index/initMy12306' 
  24.         self.ticket_url = 'https://kyfw.12306.cn/otn/leftTicket/init' 
  25.         self.driver_name = 'chrome' 
  26.         self.executable_path = 'C:\Python36\Scripts\chromedriver.exe' 
  27.     # 登录功能实现 
  28.     def login(self): 
  29.         self.driver.visit(self.login_url) 
  30.         self.driver.fill('loginUserDTO.user_name', self.username) 
  31.         # sleep(1) 
  32.         self.driver.fill('userDTO.password', self.passwd) 
  33.         # sleep(1) 
  34.         print('请输入验证码...'
  35.         while True
  36.             if self.driver.url != self.initMy_url: 
  37.                 sleep(1) 
  38.             else
  39.                 break 
  40.     # 买票功能实现 
  41.     def start_buy(self): 
  42.         self.driver = Browser(driver_name=self.driver_name, executable_path=self.executable_path) 
  43.         #窗口大小的操作 
  44.         self.driver.driver.set_window_size(700, 500) 
  45.         self.login() 
  46.         self.driver.visit(self.ticket_url) 
  47.         try: 
  48.             print('开始购票...'
  49.             # 加载查询信息 
  50.             self.driver.cookies.add({"_jc_save_fromStation": self.starts}) 
  51.             self.driver.cookies.add({"_jc_save_toStation": self.ends}) 
  52.             self.driver.cookies.add({"_jc_save_fromDate": self.dtime}) 
  53.             self.driver.reload() 
  54.             count = 0 
  55.             if self.order != 0: 
  56.                 while self.driver.url == self.ticket_url: 
  57.                     self.driver.find_by_text('查询').click() 
  58.                     count += 1 
  59.                     print('第%d次点击查询...' % count
  60.                     try: 
  61.                         self.driver.find_by_text('预订')[self.order-1].click() 
  62.                         sleep(1.5) 
  63.                     except Exception as e: 
  64.                         print(e) 
  65.                         print('预订失败...'
  66.                         continue 
  67.             else
  68.                 while self.driver.url == self.ticket_url: 
  69.                     self.driver.find_by_text('查询').click() 
  70.                     count += 1 
  71.                     print('第%d次点击查询...' % count
  72.                     try: 
  73.                         for i in self.driver.find_by_text('预订'): 
  74.                             i.click() 
  75.                             sleep(1) 
  76.                     except Exception as e: 
  77.                         print(e) 
  78.                         print('预订失败...'
  79.                         continue 
  80.             print('开始预订...'
  81.             sleep(1) 
  82.             print('开始选择用户...'
  83.             for p in self.passengers: 
  84.   
  85.                 self.driver.find_by_text(p).last.click() 
  86.                 sleep(0.5) 
  87.                 if p[-1] == ')'
  88.                     self.driver.find_by_id('dialog_xsertcj_ok').click() 
  89.             print('提交订单...'
  90.             # sleep(1) 
  91.             # self.driver.find_by_text(self.pz).click() 
  92.             # sleep(1) 
  93.             # self.driver.find_by_text(self.xb).click() 
  94.             # sleep(1) 
  95.             self.driver.find_by_id('submitOrder_id').click() 
  96.             sleep(2) 
  97.             print('确认选座...'
  98.             self.driver.find_by_id('qr_submit_id').click() 
  99.             print('预订成功...'
  100.         except Exception as e: 
  101.             print(e) 
  102.   
  103.   
  104.   
  105.   
  106. if __name__ == '__main__'
  107.     # 用户名 
  108.     username = 'xxxx' 
  109.     # 密码 
  110.     password = 'xxx' 
  111.     # 车次选择,0代表所有车次 
  112.     order = 2 
  113.     # 乘客名,比如passengers = ['丁小红''丁小明'
  114.     # 学生票需注明,注明方式为:passengers = ['丁小红(学生)''丁小明'
  115.     passengers = ['丁彦军'
  116.     # 日期,格式为:'2018-01-20' 
  117.     dtime = '2018-01-19' 
  118.     # 出发地(需填写cookie值) 
  119.     starts = '%u5434%u5821%2CWUY' #吴堡 
  120.     # 目的地(需填写cookie值) 
  121.     ends = '%u897F%u5B89%2CXAY' #西安 
  122.   
  123.     # xb =['硬座座']  
  124.     # pz=['成人票'
  125.   
  126.   
  127.     Buy_Tickets(username, passwordorder, passengers, dtime, starts, ends).start_buy() 

3、Python帮你选房子

过完年很多朋友要开始租房,自己一个个晒一个个查看是不是太累?

那就写个Python脚本吧,爬取某租房网站的房源信息,利用高德的 js API 在地图上标出房源地点,划出距离工作地点1小时内可到达的范围。

对比租金等,轻轻松松选出最适合的房子。

Python有哪些神一般的蜜汁操作? 《附代码》

代码:

  1. 链家的房租网站 
  2. 两个导入的包 
  3. 1.requests 用来过去网页内容 
  4. 2.BeautifulSoup 
  5. import time 
  6. import pymssql 
  7. import requests 
  8. from bs4 import BeautifulSoup 
  9. # https://wh.lianjia.com/zufang/ 
  10. #获取url中下面的内容 
  11. def get_page(url): 
  12. responce = requests.get(url) 
  13. soup = BeautifulSoup(responce.text,'lxml'
  14. return soup 
  15. #封装成函数,作用是获取列表下的所有租房页面的链接,返回一个链接列表 
  16. def get_links(url): 
  17. responce = requests.get(url) 
  18. soup = BeautifulSoup(responce.text,'lxml'
  19. link_div = soup.find_all('div',class_ = 'pic-panel'
  20. links = [div.a.get('href'for div in link_div] 
  21. return links 
  22. #收集一个房子的信息 
  23. def get_house_info(house_url): 
  24. soup = get_page(house_url) 
  25. price = soup.find('span',class_='total').text 
  26. unit = soup.find('span',class_= 'unit').text[1:-1] 
  27. area = soup.find('p', class_ = 'lf').text 
  28. house_info= soup.find_all('p',class_ = 'lf'
  29. area = house_info[0].text[3:] #字符串切片工具 
  30. layout = house_info[1].text[5:] 
  31. info={ 
  32. '价格':price, 
  33. '单位':unit, 
  34. '面积':area, 
  35. '户型':layout 
  36. return info 
  37. #链接数据库 
  38. server="192.168.xx.xx" #换成自己的服务器信息 
  39. user="liujiepeng" 
  40. password="xxxxx" #自己的数据库用户名和密码 
  41. conn=pymssql.connect(server,user,password,database="house"
  42. def insert(conn,house): 
  43. #sql_values = values.format(house['价格'],house['单位'],house['面积'], 
  44. #house['户型']) 
  45. sql = "insert into [house].dbo.lianjia(price,unit,area,layout)values('%s','%s','%s','%s')"%(house["价格"],house["单位"],house["面积"],house["户型"]) 
  46. print(sql) 
  47. cursor = conn.cursor() #游标,开拓新的窗口 
  48. #cursor1 = conn.cursor() 
  49. cursor.execute(sql) #执行sql语句 
  50. conn.commit() #提交 ,更新sql 语句 
  51. links = get_links('https://wh.lianjia.com/zufang/'
  52. count = 1 
  53. for link in links: 
  54. #time.sleep(2) 
  55. print('获取一个数据成功'
  56. house = get_house_info(link) 
  57. insert(conn,house) 
  58. print("第%s个数据,存入数据库成功!"%(count)) 
  59. count = count+1 
  60. #print(house["价格"],end='\r'

4、Python找回女神撤回的消息

除了这些日常操作,麦教授说几个你想不到的吧。

当自己一直喜欢的女神发给自己一个消息的时候,还没来得及看,就撤回了。

是不是自己在心中"YY",她是不是发了什么,然后你问她的时候,她却说没什么。

学会Python,你可以做一个程序,把私聊撤回的信息可以收集起来并发送到个人微信的文件传输助手。

Python有哪些神一般的蜜汁操作? 《附代码》

5、Python自己写小游戏

用Python写几个小游戏玩玩也不过是几十行代码的事,比如写个贪吃蛇!

还记得之前火爆一时的跳一跳,如果你早点学会Python,就可以常年占据排行榜第1了,还是全自动的效果,很有趣!

跳一跳代码:

  1. from __future__ import print_function 
  2.  
  3. import numpy as np 
  4. import matplotlib.pyplot as plt 
  5. import matplotlib.animation as animation 
  6. import math 
  7. import time 
  8. import os 
  9. import cv2 
  10. import datetime 
  11.  
  12. scale = 0.25 
  13.  
  14. template = cv2.imread('character.png'
  15. template = cv2.resize(template, (0, 0), fx=scale, fy=scale) 
  16. template_size = template.shape[:2] 
  17.  
  18.  
  19. def search(img): 
  20.  result = cv2.matchTemplate(img, template, cv2.TM_SQDIFF) 
  21.  min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result) 
  22.  
  23.  cv2.rectangle(img, (min_loc[0], min_loc[1]), (min_loc[0] + template_size[1], min_loc[1] + template_size[0]), (255, 0, 0), 4) 
  24.  
  25.  return img, min_loc[0] + template_size[1] / 2, min_loc[1] + template_size[0] 
  26.  
  27. def pull_screenshot(): 
  28.  filename = datetime.datetime.now().strftime("%H%M%S") + '.png' 
  29.  os.system('mv autojump.png {}'.format(filename)) 
  30.  os.system('adb shell screencap -p /sdcard/autojump.png'
  31.  os.system('adb pull /sdcard/autojump.png .'
  32.  
  33. def jump(distance): 
  34.  press_time = distance * 1.35 
  35.  press_time = int(press_time) 
  36.  cmd = 'adb shell input swipe 320 410 320 410 ' + str(press_time) 
  37.  print(cmd) 
  38.  os.system(cmd) 
  39.  
  40. def update_data(): 
  41.  global src_x, src_y 
  42.  
  43.  img = cv2.imread('autojump.png'
  44.  img = cv2.resize(img, (0, 0), fx=scale, fy=scale) 
  45.  
  46.  img, src_x, src_y = search(img) 
  47.  return img 
  48.  
  49.  
  50. fig = plt.figure() 
  51. index = 0 
  52.  
  53. # pull_screenshot() 
  54. img = update_data() 
  55.  
  56. update = True  
  57. im = plt.imshow(img, animated=True
  58.  
  59.  
  60. def updatefig(*args): 
  61.  global update 
  62.  
  63.  if update
  64.  time.sleep(1) 
  65.  pull_screenshot() 
  66.  im.set_array(update_data()) 
  67.  update = False 
  68.  return im, 
  69.  
  70. def onClick(event):  
  71.  global update  
  72.  global src_x, src_y 
  73.   
  74.  dst_x, dst_y = event.xdata, event.ydata 
  75.  
  76.  distance = (dst_x - src_x)**2 + (dst_y - src_y)**2  
  77.  distance = (distance ** 0.5) / scale 
  78.  print('distance = ', distance) 
  79.  jump(distance) 
  80.  update = True 
  81.  
  82.  
  83. fig.canvas.mpl_connect('button_press_event', onClick) 
  84. ani = animation.FuncAnimation(fig, updatefig, interval=5, blit=True
  85. plt.show() 

贪吃蛇代码:

  1. #!/usr/bin/env python 
  2. import pygame,sys,time,random 
  3. from pygame.locals import * 
  4. # 定义颜色变量 
  5. redColour = pygame.Color(255,0,0) 
  6. blackColour = pygame.Color(0,0,0) 
  7. whiteColour = pygame.Color(255,255,255) 
  8. greyColour = pygame.Color(150,150,150) 
  9.  
  10. # 定义gameOver函数 
  11. def gameOver(playSurface): 
  12.  gameOverFont = pygame.font.Font('arial.ttf',72) 
  13.  gameOverSurf = gameOverFont.render('Game Over'True, greyColour) 
  14.  gameOverRect = gameOverSurf.get_rect() 
  15.  gameOverRect.midtop = (320, 10) 
  16.  playSurface.blit(gameOverSurf, gameOverRect) 
  17.  pygame.display.flip() 
  18.  time.sleep(5) 
  19.  pygame.quit() 
  20.  sys.exit() 
  21.  
  22. # 定义main函数 
  23. def main(): 
  24.  # 初始化pygame 
  25.  pygame.init() 
  26.  fpsClock = pygame.time.Clock() 
  27.  # 创建pygame显示层 
  28.  playSurface = pygame.display.set_mode((640,480)) 
  29.  pygame.display.set_caption('Raspberry Snake'
  30.  
  31.  # 初始化变量 
  32.  snakePosition = [100,100] 
  33.  snakeSegments = [[100,100],[80,100],[60,100]] 
  34.  raspberryPosition = [300,300] 
  35.  raspberrySpawned = 1 
  36.  direction = 'right' 
  37.  changeDirection = direction 
  38.  while True
  39.  # 检测例如按键等pygame事件 
  40.  for event in pygame.event.get(): 
  41.  if event.type == QUIT: 
  42.  pygame.quit() 
  43.  sys.exit() 
  44.  elif event.type == KEYDOWN: 
  45.  # 判断键盘事件 
  46.  if event.key == K_RIGHT or event.key == ord('d'): 
  47.  changeDirection = 'right' 
  48.  if event.key == K_LEFT or event.key == ord('a'): 
  49.  changeDirection = 'left' 
  50.  if event.key == K_UP or event.key == ord('w'): 
  51.  changeDirection = 'up' 
  52.  if event.key == K_DOWN or event.key == ord('s'): 
  53.  changeDirection = 'down' 
  54.  if event.key == K_ESCAPE: 
  55.  pygame.event.post(pygame.event.Event(QUIT)) 
  56.  # 判断是否输入了反方向 
  57.  if changeDirection == 'right' and not direction == 'left'
  58.  direction = changeDirection 
  59.  if changeDirection == 'left' and not direction == 'right'
  60.  direction = changeDirection 
  61.  if changeDirection == 'up' and not direction == 'down'
  62.  direction = changeDirection 
  63.  if changeDirection == 'down' and not direction == 'up'
  64.  direction = changeDirection 
  65.  # 根据方向移动蛇头的坐标 
  66.  if direction == 'right'
  67.  snakePosition[0] += 20 
  68.  if direction == 'left'
  69.  snakePosition[0] -= 20 
  70.  if direction == 'up'
  71.  snakePosition[1] -= 20 
  72.  if direction == 'down'
  73.  snakePosition[1] += 20 
  74.  # 增加蛇的长度 
  75.  snakeSegments.insert(0,list(snakePosition)) 
  76.  # 判断是否吃掉了树莓 
  77.  if snakePosition[0] == raspberryPosition[0] and snakePosition[1] == raspberryPosition[1]: 
  78.  raspberrySpawned = 0 
  79.  else
  80.  snakeSegments.pop() 
  81.  # 如果吃掉树莓,则重新生成树莓 
  82.  if raspberrySpawned == 0: 
  83.  x = random.randrange(1,32) 
  84.  y = random.randrange(1,24) 
  85.  raspberryPosition = [int(x*20),int(y*20)] 
  86.  raspberrySpawned = 1 
  87.  # 绘制pygame显示层 
  88.  playSurface.fill(blackColour) 
  89.  for position in snakeSegments: 
  90.  pygame.draw.rect(playSurface,whiteColour,Rect(position[0],position[1],20,20)) 
  91.  pygame.draw.rect(playSurface,redColour,Rect(raspberryPosition[0], raspberryPosition[1],20,20)) 
  92.  
  93.  # 刷新pygame显示层 
  94.  pygame.display.flip() 
  95.  # 判断是否死亡 
  96.  if snakePosition[0] > 620 or snakePosition[0] < 0: 
  97.  gameOver(playSurface) 
  98.  if snakePosition[1] > 460 or snakePosition[1] < 0: 
  99.  for snakeBody in snakeSegments[1:]: 
  100.  if snakePosition[0] == snakeBody[0] and snakePosition[1] == snakeBody[1]: 
  101.  gameOver(playSurface) 
  102.  # 控制游戏速度 
  103.  fpsClock.tick(5) 
  104.  
  105. if __name__ == "__main__"
  106.  main() 

6、Python爬取你想要的信息

还可以用python爬取电影分析影评,用词云进行展示效果:

Python有哪些神一般的蜜汁操作? 《附代码》

写几行代码浪漫一把也是可以的,你也可以把里面的字符换成爱人的名字,做成浪漫的云图词!

Python有哪些神一般的蜜汁操作? 《附代码》

7、Python看看你朋友圈都是什么样的人

掌握python以后,你玩微信别人也玩微信。

但是你可以得到不一样的骚操作,几十行代码掌握你朋友圈都是些什么样的人!

比如?爬取微信好友男女比例并进行可视化。

Python有哪些神一般的蜜汁操作? 《附代码》

再来看看你的好友都来自哪个地方

Python有哪些神一般的蜜汁操作? 《附代码》

这还不够,来把好友的个性签名也玩出逼格,做个创意的文字图,看看大家都在说什么

Python有哪些神一般的蜜汁操作? 《附代码》

温馨小提示,如果分析的对象换成你的用户们...是不是业绩能蹭蹭上涨?

8、Python自己做小动画

还可以用字符串跳一段MV,成为B站大佬,使用 OpenCV 处理图片视频,将视频转为字符画序列,再在终端中播放字符动画。

[[323891]]

9、Python鉴黄

看看Python还能用来干嘛

《图片就不放了 怕被举报 自行想象》

哈哈 可以用来emmm 鉴黄!

使用 Python3 去识别图片是否为色情图片,利用PIL 这个图像处理库,会编写算法来划分图像的皮肤区域。

其中涉及到Python 3 基础知识,肤色像素检测与皮肤区域划分算法。

是不是万万没想到?

10、Python AI医疗

还被用进医疗的领域。

典型如腾讯觅影这样的产品,用于AI医学图像分析,在癌症的早期识别上,人工智能对拍片的识别能力,也已经超过人类医生。

[[323892]]

Python能做的好玩又实用的事情实在太多啦!

责任编辑:未丽燕 来源: 今日头条
相关推荐

2022-03-09 14:57:53

Numbapython

2022-04-02 15:08:54

API调试

2022-03-18 07:44:59

插件VSCode代码

2019-06-05 15:23:09

Redis缓存存储

2015-07-22 15:53:57

2022-04-02 10:52:33

Go开发面试

2015-01-08 09:50:54

Shen编程语言

2020-06-09 08:09:07

机器学习统计学习无监督学习

2015-10-14 13:06:59

2022-05-23 07:26:44

系统优化软件Windows操作系统

2011-09-08 11:35:18

2010-10-08 14:23:08

MySQL中INSER

2011-07-08 09:42:03

2017-08-31 14:09:26

数据库MySQLSQL优化

2021-05-07 22:11:56

宽带手机运营商

2024-03-28 18:05:41

Python编程

2024-04-03 09:55:56

代码pipeline项目

2021-08-16 08:42:31

MySQL查询数据库

2021-08-19 09:16:29

MySQL数据库优化器

2010-09-14 10:55:14

DIV CSS网页制作
点赞
收藏

51CTO技术栈公众号