python必背100源代码-100行Python代码实现自动抢火车票(附源码)

本文介绍了如何使用Python和Splinter库编写代码自动抢购12306火车票。通过登录、查询和订票操作的模拟,详细解析了关键步骤,包括设置起始地的cookie值,以及处理验证码的注意事项。虽然仍需手动输入验证码,但已大大简化了抢票流程。
摘要由CSDN通过智能技术生成

前言

又要过年了,今年你不妨自己写一段代码来抢回家的火车票,是不是很Cool。下面话不多说了,来一起看看详细的介绍吧。

先准备好:

12306网站用户名和密码

chrome浏览器及下载chromedriver

下载Python代码,来自网络整理 [点击下载 | 本地下载 ]

代码用的Python+Splinter开发,Splinter是一个使用Python开发的开源Web应用测试工具,它可以帮你实现自动浏览站点和与其进行交互。

Splinter执行的时候会自动打开你指定的浏览器,访问指定的URL。然后你所开发的模拟的任何行为,都会自动完成,你只需要坐在电脑面前,像看电影一样看着屏幕上各种动作自动完成然后收集结果即可。

了解原理:

找到相应URL,找到控件模拟登录、查询、订票操作。关键是找到控件名称,难点是起始地不是直接输入的页面值,需要在cookie中查出。

12306查询URL: https://kyfw.12306.cn/otn/leftTicket/init

12306登录URL: https://kyfw.12306.cn/otn/login/init

我的12306URL: https://kyfw.12306.cn/otn/index/initMy12306

购票确认URL: https://kyfw.12306.cn/otn/confirmPassenger/initDc

Python代码打开URL,找到控件填充值:

def login(self):

self.driver.visit(self.login_url)

# 填充用户名

self.driver.fill("loginUserDTO.user_name&

很高兴为您解答这个问题。以下是Python小游戏之300代码实现俄罗斯方块的源代码: ```python import pygame import random # 初始化游戏 pygame.init() # 颜色 WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # 屏幕大小 SCREEN_WIDTH = 600 SCREEN_HEIGHT = 800 # 方块大小 BLOCK_SIZE = 30 # 方块类型及其形状 BLOCKS = { "I": [(0, 0), (1, 0), (2, 0), (3, 0)], "J": [(0, 0), (0, 1), (1, 1), (2, 1)], "L": [(2, 0), (0, 1), (1, 1), (2, 1)], "O": [(0, 0), (1, 0), (0, 1), (1, 1)], "S": [(1, 0), (2, 0), (0, 1), (1, 1)], "T": [(1, 0), (0, 1), (1, 1), (2, 1)], "Z": [(0, 0), (1, 0), (1, 1), (2, 1)] } # 方向 DIRECTIONS = { "LEFT": (-1, 0), "RIGHT": (1, 0), "DOWN": (0, 1), "UP": (0, -1) } # 游戏状态 STATE_RUNNING = 0 STATE_PAUSED = 1 STATE_GAME_OVER = 2 # 字体 FONT = pygame.font.SysFont(None, 50) class Block: def __init__(self, x, y, block_type): self.x = x self.y = y self.block_type = block_type self.shape = BLOCKS[block_type] self.color = self.get_color() def get_color(self): if self.block_type == "I": return BLUE elif self.block_type == "J": return RED elif self.block_type == "L": return GREEN elif self.block_type == "O": return BLUE elif self.block_type == "S": return RED elif self.block_type == "T": return GREEN elif self.block_type == "Z": return BLUE def move(self, direction): dx, dy = DIRECTIONS[direction] self.x += dx self.y += dy def rotate(self): new_shape = [] for x, y in self.shape: new_x = y new_y = -x new_shape.append((new_x, new_y)) self.shape = new_shape class Board: def __init__(self): self.width = SCREEN_WIDTH // BLOCK_SIZE self.height = SCREEN_HEIGHT // BLOCK_SIZE self.grid = [[BLACK for _ in range(self.width)] for _ in range(self.height)] self.current_block = self.generate_new_block() self.next_block = self.generate_new_block() self.score = 0 self.state = STATE_RUNNING def generate_new_block(self): block_type = random.choice(list(BLOCKS.keys())) x = self.width // 2 - 2 y = 0 return Block(x, y, block_type) def update(self): if self.state == STATE_RUNNING: if self.is_valid(self.current_block, 0, 1): self.current_block.move("DOWN") else: self.lock_block() self.clear_lines() self.current_block = self.next_block self.next_block = self.generate_new_block() if not self.is_valid(self.current_block, 0, 0): self.state = STATE_GAME_OVER def lock_block(self): for x, y in self.current_block.shape: self.grid[self.current_block.y + y][self.current_block.x + x] = self.current_block.color def clear_lines(self): lines_cleared = 0 for y in range(self.height): if all([self.grid[y][x] != BLACK for x in range(self.width)]): for y1 in range(y, 0, -1): for x in range(self.width): self.grid[y1][x] = self.grid[y1 - 1][x] for x in range(self.width): self.grid[0][x] = BLACK lines_cleared += 1 self.score += lines_cleared ** 2 def move_current_block(self, direction): if self.is_valid(self.current_block, *DIRECTIONS[direction]): self.current_block.move(direction) def rotate_current_block(self): if self.is_valid(self.current_block, 0, 0, True): self.current_block.rotate() def is_valid(self, block, dx, dy, rotated=False): if rotated: new_shape = [] for x, y in block.shape: new_x = y new_y = -x new_shape.append((new_x, new_y)) else: new_shape = block.shape for x, y in new_shape: new_x = block.x + x + dx new_y = block.y + y + dy if not (0 <= new_x < self.width and 0 <= new_y < self.height): return False if self.grid[new_y][new_x] != BLACK: return False return True def draw(self, screen): for y in range(self.height): for x in range(self.width): pygame.draw.rect(screen, self.grid[y][x], (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0) for x, y in self.current_block.shape: pygame.draw.rect(screen, self.current_block.color, ((self.current_block.x + x) * BLOCK_SIZE, (self.current_block.y + y) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0) text = FONT.render(f"Score: {self.score}", True, WHITE) screen.blit(text, (10, 10)) text = FONT.render("Next Block:", True, WHITE) screen.blit(text, (400, 10)) for x, y in self.next_block.shape: pygame.draw.rect(screen, self.next_block.color, ((x + 16) * BLOCK_SIZE, (y + 3) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0) def main(): # 创建屏幕 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Tetris") # 创建时钟 clock = pygame.time.Clock() # 创建棋盘 board = Board() # 游戏循环 while True: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: board.move_current_block("LEFT") elif event.key == pygame.K_RIGHT: board.move_current_block("RIGHT") elif event.key == pygame.K_DOWN: board.move_current_block("DOWN") elif event.key == pygame.K_UP: board.rotate_current_block() elif event.key == pygame.K_SPACE: board.state = STATE_PAUSED if board.state == STATE_RUNNING else STATE_RUNNING # 更新游戏状态 board.update() # 绘制屏幕 screen.fill(BLACK) board.draw(screen) # 显示屏幕 pygame.display.update() # 控制帧率 clock.tick(10) if __name__ == "__main__": main() ``` 希望能够帮到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值