马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
Python 爬取公交线路信息(requests + BeautifulSoup)
以兰州公交为例,演示如何用 Python 抓取每路公交的线路、运行时间、票价与运营公司,并保存为 CSV。
一、抓取思路
- 抓取查询路径:从 https://lanzhou.8684.cn/ 中,以数字 1~9 开头的线路分类入口(如 /list1、/list2 … /list9)进入;
- 拼接每路公交线路链接:进入分类页后,提取每路公交的详情链接(如 /x_24f5dad9);
- 抓取每路公交具体信息:进入详情页提取线路名、运行时间、参考票价、公交公司等;
- 实现代码整合。
二、查询路径与链接拼接
分类页中数字开头的线路入口形如:- <a target="_blank" href="/list1">1</a>
- <a target="_blank" href="/list2">2</a>
- ...
- <a target="_blank" href="/list9">9</a>
复制代码 将 href 拼接站点域名 https://lanzhou.8684.cn/ 即为查询地址,如 https://lanzhou.8684.cn/list1。
每路公交详情链接形如:- <a target="_blank" href="/x_24f5dad9" title="兰州1路公交车路线">1路</a>
复制代码 拼接后得到 https://lanzhou.8684.cn/x_24f5dad9。
三、具体信息示例
详情页线路信息示例:- 兰州9路公交车路线[市区线路]
- 运行时间:西关什字6:00-22:30|晟地兰州汽配城6:00-21:40
- 参考票价:无人售票车,1.0元一票制,支持交联IC卡/云闪付/微信/支付宝等多种支付方式
- 公交公司:兰州公交集团第五客运公司
复制代码
四、实现代码- # author:Aurora
- # Datetime:2022-6-24 11:41:00
- """ 抓取每路公交的具体信息: title / run_time / tickets_price / bus_company """
- import requests
- from bs4 import BeautifulSoup
- import pandas as pd
- import os
- import time
- def get_every_raod_bus_info():
- url = "https://lanzhou.8684.cn/"
- headers = {
- "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"
- }
- response = requests.get(url=url, headers=headers)
- soup = BeautifulSoup(response.text, features='lxml')
- # 以数字开头的线路在第一个 pl10 标签内
- items = soup.select('.pl10')[0]
- list_tag = items.select('.list')[0]
- a = list_tag.select('a')
- hrefs = [i['href'] for i in a]
- link = "https://lanzhou.8684.cn/"
- n1 = int(input("请输入以数字开头的公车(1-9):"))
- links = [link + hrefs[n1-1]]
- # 拼接每路公交的线路链接
- road_list = []
- for u in links:
- response = requests.get(u, headers=headers)
- soup = BeautifulSoup(response.text, features='lxml')
- lists = soup.select('.list')[0]
- for i in lists.select('a'):
- road_list.append("https://lanzhou.8684.cn" + i['href'])
- # 获取每路公交的信息
- title_list, run_time_list, ticket_price_list, bus_company_list, last_updated_list = [], [], [], [], []
- n = 1
- for road_url in road_list:
- response = requests.get(road_url, headers=headers)
- soup = BeautifulSoup(response.text, features='lxml')
- info = soup.select('.info')[0]
- spans = info.select('span')
- title = spans[0].text
- a = info.select('a')
- categorys = a[0].text
- titles = title + categorys
- title_list.append(titles)
- bus_desc = info.select('.bus-desc')[0]
- lis = bus_desc.select('li')
- run_times = lis[0].text
- tickets_price = lis[1].text
- bus_companies = lis[2].text
- last_updated = lis[3].text
- run_time_list.append(run_times)
- ticket_price_list.append(tickets_price)
- bus_company_list.append(bus_companies)
- last_updated_list.append(last_updated)
- time.sleep(1)
- n += 1
- road_info_dic = {
- "title": title_list, "run_time": run_time_list,
- "ticket_price": ticket_price_list, "bus_company": bus_company_list,
- "last_updated": last_updated_list
- }
- info_df = pd.DataFrame(road_info_dic)
- info_df.to_csv(os.path.join(os.getcwd(), 'files/') + f"bus{n1}_info.csv", index=False, encoding='gbk')
- print(f"获取信息成功,共获取 {n-1} 条信息")
- get_every_raod_bus_info()
复制代码
qidao123.com企服评测-应用市场 编辑提示
|