PHP前端开发

python爬虫火车票实例源码

百变鹏仔 3天前 #Python
文章标签 爬虫
Python 爬虫可用于自动获取火车票信息,包括火车号、出发/到达时间、票价等。代码结构包括导入库、设置请求头、获取车站代码、构建查询 URL、发送请求、解析响应和提取信息。步骤依次是:导入库、设置请求头、获取车站代码、构建查询 URL、发送请求、解析响应、提取信息。

Python 爬虫火车票实例源码

入门

爬取火车票信息对于自动抢票或旅行规划非常有用。Python 提供了丰富的库和工具来实现这一目的。

代码结构

立即学习“Python免费学习笔记(深入)”;

一个基本的 Python 爬虫火车票实例源码可能包含以下部分:

步骤详解

1. 导入库

import requestsfrom bs4 import BeautifulSoupimport re

2. 设置请求头

headers = {    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36"}

3. 获取车站代码

response = requests.get("https://www.12306.cn/index/", headers=headers)soup = BeautifulSoup(response.text, "html.parser")stations = soup.find_all("a", {"href": re.compile("/otn/resources/js/framework/station_name.js")})station_codes = {}for station in stations:    station_codes[station.text] = station["href"].split("/")[-1][:-3]

4. 构建查询 URL

from_station = "上海"to_station = "北京"date = "2023-05-01"url = "https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date={}&leftTicketDTO.from_station={}&leftTicketDTO.to_station={}&purpose_codes=ADULT".format(date, station_codes[from_station], station_codes[to_station])

5. 发送请求

response = requests.get(url, headers=headers)

6. 解析响应

soup = BeautifulSoup(response.text, "html.parser")

7. 提取信息

trains = soup.find_all("tr", {"class": "result-item"})for train in trains:    train_number = train.find("a", {"class": "number"}).text    start_time = train.find("td", {"class": "start-time"}).text    arrive_time = train.find("td", {"class": "arrive-time"}).text    duration = train.find("td", {"class": "duration"}).text    prices = train.find("td", {"class": "price"}).text.split("|")    print(train_number, start_time, arrive_time, duration, prices)