当前位置:网站首页>太原市公交路线爬取
太原市公交路线爬取
2022-07-29 05:23:00 【赵颂@】
人生苦短,我学Python
# -*- codeing = utf-8 -*-
import requests
from lxml import etree
from fake_useragent import UserAgent
#**列表用来保存所有信息**
items = []
headers ={
"User-Agent":UserAgent().chrome
}
def parse_navigation():
start_url = "https://taiyuan.8684.cn/"
r =requests.get(url=start_url,headers=headers)
**#解析内容获取所有导航链接**
tree =etree.HTML(r.text)
**#获取链接
#以数字开头的**
number_href_list =tree.xpath('//div[@class="bus-layer depth w120"]/div[1]/div/a/@href')
**#字母开头的**
char_href_list =tree.xpath('//div[@class="bus-layer depth w120"]/div[2]/div/a/@href')
**#将返回所有链接**
return number_href_list + char_href_list
def parse_erji_route(content):
tree = etree.HTML(content)
#获取每一个线路链接
route_list = tree.xpath('//div[@class="list clearfix"]/a/@href')
route_name = tree.xpath('//div[@class="list clearfix"]/a/text()')
#遍历,
i = 0
for route in route_list:
print("开始爬取%s的信息" % route_name[i])
#便利每一个路线
route ="https://taiyuan.8684.cn" + route
#发送请求, #获取响应
r =requests.get(url=route,headers=headers)
parse_sanji_route(r.text)
print("结束爬取%s的信息" % route_name[i])
i += 1
def parse_sanji_route(content):
tree =etree.HTML(content)
#抓取想要的内容
#公交信息
bus_num =tree.xpath('//div[@class="info"]/h1/text()')[0]
#获取运行时间:
run_time = tree.xpath('//ul[@class="bus-desc"]/li[1]/text()')[0]
#票价信息
ticket_price_info = tree.xpath('//ul[@class="bus-desc"]/li[2]/text()')[0]
#更新时间
updata_time =tree.xpath('//ul[@class="bus-desc"]/li[4]/text()')[0]
#获取上行总站数
bus_station_list = tree.xpath('//div[@class="bus-excerpt mb15"]/div/div[@class="total"]/text()')
up_total =bus_station_list[0]
#获取上行站名
up_station_name =tree.xpath('//div[@class="bus-lzlist mb15"][1]')
try:
#获取下行总站数
down_total = bus_station_list[1]
# 获取下行站名
down_station_name_list = tree.xpath('//div[@class="bus-lzlist mb15"][2]')
except Exception as error:
down_total = ""
down_station_name_list =[]
#保存每一条公交信息,存放到字典
item ={
"公交名称":bus_num,
"运行时间":run_time,
"票价信息":ticket_price_info,
"更新时间":updata_time,
"上行站数":up_total,
"上行站名":up_station_name,
"下行站数":down_total,
"下行站名":down_station_name_list,
}
items.append(item)
def parse_erji(navi_list):
#遍历列表一次发送请求,获取响应每个页面所有的公交路线
for first_url in navi_list:
print("开始爬取%s所有的公交信息"%first_url)
#拼接完整的链接
first_url = "https://taiyuan.8684.cn" +first_url
#发送请求,获取响应
print(first_url)
r =requests.get(first_url,headers=headers)
content =r.text
#解析内容每一路公交的完整url
parse_erji_route(content)
print("结束爬取%s所有的公交信息" % first_url)
边栏推荐
- Hal library learning notes-13 application of I2C and SPI
- TLE5012b+STM32F103C8T6(bluepill)读取角度数据
- Tf.get in tensorflow_ Detailed explanation of variable() function
- 【软件工程之美 - 专栏笔记】27 | 软件工程师的核心竞争力是什么?(上)
- Reading papers on false news detection (5): a semi supervised learning method for fake news detection in social media
- 【软件工程之美 - 专栏笔记】26 | 持续交付:如何做到随时发布新版本到生产环境?
- 2022春招——芯动科技FPGA开发岗笔试题(原题以及心得)
- 数学建模心得
- DP1332E 多协议高度集成非接触式读写芯片
- Reading papers on fake news detection (2): semi supervised learning and graph neural networks for fake news detection
猜你喜欢

防爆倾角传感器应用于LNG液化天然气安全作业

2022 spring recruit - Hesai technology FPGA technology post (one or two sides, collected from: Digital IC workers and FPGA Explorers)

华为云14天鸿蒙设备开发-Day1源码获取

TLE5012b+STM32F103C8T6(bluepill)读取角度数据

HAL库学习笔记- 8 串口通信之概念

Am model in NLP field

传统模型预测控制轨迹跟踪——圆形轨迹(功能包已经更新)

FPGA based: multi-target motion detection (hand-in-hand teaching ①)

Hal library learning notes - 8 use of serial communication

【软件工程之美 - 专栏笔记】29 | 自动化测试:如何把Bug杀死在摇篮里?
随机推荐
Low rank transfer subspace learning
HAL库学习笔记- 8 串口通信之使用
ML7 self study notes
基于51单片机的直流电机调速系统(L298的使用)
八大排序-----------快速排序
传统模型预测控制轨迹跟踪——波浪形轨迹(功能包已经更新)
Huawei cloud 14 day Hongmeng device development -day2 compilation framework
Huawei cloud 14 day Hongmeng device development -day1 source code acquisition
【软件工程之美 - 专栏笔记】“一问一答”第3期 | 18个软件开发常见问题解决策略
倾角传感器精度校准检测
Si12T和Si14T低功耗电容触摸芯片
华为云14天鸿蒙设备开发-Day1环境搭建
Ml6 self study notes
Ml8 self study notes
【RoboMaster】从零开始控制RM电机(2)-CAN通信原理及电调通信协议
基于F407ZGT6的WS2812B彩灯驱动
QT学习笔记-数据管理系统设计实现
Jingwei Qili: development of heart rate and blood oxygen module based on hmep060 (1: FPGA sends multi bit instructions)
QT学习笔记-Qt Model/View
FT232替代GP232RL USB-RS232转换器芯片国产化应用