利用python爬取城市公交站点

本文介绍如何利用Python爬虫抓取城市公交站点,并通过BeautifulSoup解析数据,结合高德API获取经纬度。数据清洗部分,讨论了Excel Power Query(PQ)与Python pandas的使用,建议使用PQ进行数据清洗。最后,利用QGIS进行坐标纠偏,确保数据准确性。文章强调了面向对象编程和异常处理的重要性,并提到代码管理和上传到Gitee的实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

利用python爬取城市公交站点

页面分析

https://guiyang.8684.cn/line1

python学习资源汇总腾讯文档-在线PDFhttps://docs.qq.com/pdf/DR1doYmNBYUZ3RVNX 

爬虫

我们利用requests请求,利用BeautifulSoup来解析,获取我们的站点数据。得到我们的公交站点以后,我们利用高德api来获取站点的经纬度坐标,利用pandas解析json文件。接下来开干,我推荐使用面向对象的方法来写代码。

import requests
import json
from bs4 import BeautifulSoup
import pandas as pd
​
​
class bus_stop:
 ## 定义一个类,用来获取每趟公交的站点名称和经纬度
 def __init__(self):
 self.url = 'https://guiyang.8684.cn/line{}'
 self.starnum = []
 for start_num in range(1, 17):
 self.starnum.append(start_num)
 self.payload = {}
 self.headers = {
 'Cookie': 'JSES
### 实现Python爬取城市实时天气功能 要实现 Python 爬取城市实时天气的功能,可以通过以下方法完成: #### 1. 使用第三方 API 获取实时天气数据 许多在线服务提供开放的 API 接口来获取实时天气数据。例如 OpenWeatherMap 提供了一个简单易用的接口,开发者只需注册账号即可获得免费的 API 密钥[^3]。 以下是基于 `requests` 库调用 OpenWeatherMap 的示例代码: ```python import requests def get_weather(city_name, api_key): url = f"http://api.openweathermap.org/data/2.5/weather?q={city_name}&appid={api_key}&units=metric" response = requests.get(url) if response.status_code == 200: weather_data = response.json() temperature = weather_data['main']['temp'] description = weather_data['weather'][0]['description'] return {"temperature": temperature, "description": description} else: return None # 示例调用 api_key = 'your_api_key_here' city_name = 'Beijing' result = get_weather(city_name, api_key) if result: print(f"{city_name} 当前温度: {result['temperature']}°C, 天气描述: {result['description']}") else: print("无法获取天气信息") ``` 此代码片段展示了如何通过 HTTP 请求向 OpenWeatherMap 发送请求,并解析返回的数据以提取当前城市的气温和天气状况[^3]。 #### 2. 利用 BeautifulSoup 解析 HTML 页面中的天气数据 如果不想依赖外部 API 或者希望学习更底层的技术,则可以选择直接抓取目标网站上的公开页面内容。这种方法涉及发送 GET 请求到指定 URL 并利用库如 Beautiful Soup 来处理响应的内容[^2]。 下面是一个例子展示怎样从某特定站点读取实时气象记录: ```python from bs4 import BeautifulSoup import requests def fetch_realtime_weather(url, headers): try: html_content = requests.get(url, headers=headers).text soup = BeautifulSoup(html_content, 'html.parser') # 假设所有需要的信息都在 class='thrui' 下面 thrui_elements = soup.find_all(class_='thrui') results = [] for element in thrui_elements: date = element.find('span', {'class': 'date'}).get_text(strip=True) temp = element.find('div', {'class': 'temperature'}).get_text(strip=True) record = { "日期时间": date, "温度": temp } results.append(record) return results except Exception as e: print(e) return [] url = 'http://example.com/weatherdata' headers = {'User-Agent': 'Mozilla/5.0'} real_time_records = fetch_realtime_weather(url, headers) for entry in real_time_records[:5]: print(entry) ``` 注意这里假设了所有的关键字段都位于具有 CSS 类名为 `'thrui'` 的容器内部;实际应用时需依据具体的目标网页结构调整查找逻辑[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值