相关代码:
# @Time: 2024/1/22 22:10
# @Author: 马龙强
# @File: 爬取站点信息.py
# @software: PyCharm
import re
import json
import requests
# 定义URL
url = 'https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.9028'
# 发送GET请求
response = requests.get(url, verify=False)
# 编写正则表达式
station = re.findall(r'([\u4e00-\u9fa5]+)\|([A-Z]+)', response.text)
# 将正则匹配结果转换为字典形式
station_dict = {match[0]: match[1] for match in station}
# 将字典保存为JSON文件
with open('station.json', 'w', encoding='utf-8') as file:
json.dump(station_dict, file, ensure_ascii=False, indent=4)
有关链接:火车票车票查询-Python-CSDN博客