抓取每路公交的具体信息

[复制链接]
发表于 2026-10-17 15:23:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
Python 爬取公交线路信息(requests + BeautifulSoup)

以兰州公交为例,演示如何用 Python 抓取每路公交的线路、运行时间、票价与运营公司,并保存为 CSV。

一、抓取思路

  • 抓取查询路径:从 https://lanzhou.8684.cn/ 中,以数字 1~9 开头的线路分类入口(如 /list1、/list2 … /list9)进入;
  • 拼接每路公交线路链接:进入分类页后,提取每路公交的详情链接(如 /x_24f5dad9);
  • 抓取每路公交具体信息:进入详情页提取线路名、运行时间、参考票价、公交公司等;
  • 实现代码整合。


二、查询路径与链接拼接

分类页中数字开头的线路入口形如:
  1. <a target="_blank" href="/list1">1</a>
  2. <a target="_blank" href="/list2">2</a>
  3. ...
  4. <a target="_blank" href="/list9">9</a>
复制代码
将 href 拼接站点域名 https://lanzhou.8684.cn/ 即为查询地址,如 https://lanzhou.8684.cn/list1。

每路公交详情链接形如:
  1. <a target="_blank" href="/x_24f5dad9" title="兰州1路公交车路线">1路</a>
复制代码
拼接后得到 https://lanzhou.8684.cn/x_24f5dad9。

三、具体信息示例

详情页线路信息示例:
  1. 兰州9路公交车路线[市区线路]
  2. 运行时间:西关什字6:00-22:30|晟地兰州汽配城6:00-21:40
  3. 参考票价:无人售票车,1.0元一票制,支持交联IC卡/云闪付/微信/支付宝等多种支付方式
  4. 公交公司:兰州公交集团第五客运公司
复制代码

四、实现代码
  1. # author:Aurora
  2. # Datetime:2022-6-24 11:41:00
  3. """ 抓取每路公交的具体信息: title / run_time / tickets_price / bus_company """
  4. import requests
  5. from bs4 import BeautifulSoup
  6. import pandas as pd
  7. import os
  8. import time
  9. def get_every_raod_bus_info():
  10.     url = "https://lanzhou.8684.cn/"
  11.     headers = {
  12.         "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.124 Safari/537.36 Edg/102.0.1245.44"
  13.     }
  14.     response = requests.get(url=url, headers=headers)
  15.     soup = BeautifulSoup(response.text, features='lxml')
  16.     # 以数字开头的线路在第一个 pl10 标签内
  17.     items = soup.select('.pl10')[0]
  18.     list_tag = items.select('.list')[0]
  19.     a = list_tag.select('a')
  20.     hrefs = [i['href'] for i in a]
  21.     link = "https://lanzhou.8684.cn/"
  22.     n1 = int(input("请输入以数字开头的公车(1-9):"))
  23.     links = [link + hrefs[n1-1]]
  24.     # 拼接每路公交的线路链接
  25.     road_list = []
  26.     for u in links:
  27.         response = requests.get(u, headers=headers)
  28.         soup = BeautifulSoup(response.text, features='lxml')
  29.         lists = soup.select('.list')[0]
  30.         for i in lists.select('a'):
  31.             road_list.append("https://lanzhou.8684.cn" + i['href'])
  32.     # 获取每路公交的信息
  33.     title_list, run_time_list, ticket_price_list, bus_company_list, last_updated_list = [], [], [], [], []
  34.     n = 1
  35.     for road_url in road_list:
  36.         response = requests.get(road_url, headers=headers)
  37.         soup = BeautifulSoup(response.text, features='lxml')
  38.         info = soup.select('.info')[0]
  39.         spans = info.select('span')
  40.         title = spans[0].text
  41.         a = info.select('a')
  42.         categorys = a[0].text
  43.         titles = title + categorys
  44.         title_list.append(titles)
  45.         bus_desc = info.select('.bus-desc')[0]
  46.         lis = bus_desc.select('li')
  47.         run_times = lis[0].text
  48.         tickets_price = lis[1].text
  49.         bus_companies = lis[2].text
  50.         last_updated = lis[3].text
  51.         run_time_list.append(run_times)
  52.         ticket_price_list.append(tickets_price)
  53.         bus_company_list.append(bus_companies)
  54.         last_updated_list.append(last_updated)
  55.         time.sleep(1)
  56.         n += 1
  57.     road_info_dic = {
  58.         "title": title_list, "run_time": run_time_list,
  59.         "ticket_price": ticket_price_list, "bus_company": bus_company_list,
  60.         "last_updated": last_updated_list
  61.     }
  62.     info_df = pd.DataFrame(road_info_dic)
  63.     info_df.to_csv(os.path.join(os.getcwd(), 'files/') + f"bus{n1}_info.csv", index=False, encoding='gbk')
  64.     print(f"获取信息成功,共获取 {n-1} 条信息")
  65. get_every_raod_bus_info()
复制代码

qidao123.com企服评测-应用市场 编辑提示
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表