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

Python 爬虫火车票实例源码
入门
爬取火车票信息对于自动抢票或旅行规划非常有用。Python 提供了丰富的库和工具来实现这一目的。
代码结构
立即学习“Python免费学习笔记(深入)”;
一个基本的 Python 爬虫火车票实例源码可能包含以下部分:
步骤详解
1. 导入库
<code class="python">import requests from bs4 import BeautifulSoup import re</code>
2. 设置请求头
<code class="python">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"
}</code>3. 获取车站代码
<code class="python">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]</code>4. 构建查询 URL
<code class="python">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])</code>5. 发送请求
<code class="python">response = requests.get(url, headers=headers)</code>
6. 解析响应
<code class="python">soup = BeautifulSoup(response.text, "html.parser")</code>
7. 提取信息
<code class="python">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)</code>以上就是python爬虫火车票实例源码的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号