利用python爬取城市公交站点
页面分析
https://guiyang.8684.cn/line1


爬虫
我们利用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%7B%7D'
self.starnum = []
for start_num in range(1, 17):
self.starnum.append(start_num)
self.payload = {}
self.headers = {
'Cookie': 'JSESSIONID=48304F9E8D55A9F2F8ACC14B7EC5A02D'}
## 调用高德api获取公交线路的经纬度
### 这个key大家可以自己去申请
def get_location(self, line):
url_api = 'https://restapi.amap.com/v3/bus/linename?s=rsv3&exten%3C/pre%3E%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3C/div%3E%20%20%20%20%20%20%20%20%3C/div%3E%20%20%20%20%3C/article%3E%20%20%3Cscript%3E%20%20%20%20$(function()%20{%20%20%20%20%20%20setTimeout(function%20()%20{%20%20%20%20%20%20%20%20var%20mathcodeList%20=%20document.querySelectorAll('.htmledit_views img.mathcode');
if (mathcodeList.length > 0) {
for (let i = 0; i < mathcodeList.length; i++) {
if (mathcodeList[i].complete) {
if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
var alt = mathcodeList[i].alt;
alt = '\\(' + alt + '\\)';
var curSpan = $('');
curSpan.text(alt);
$(mathcodeList[i]).before(curSpan);
$(mathcodeList[i]).remove();
}
} else {
mathcodeList[i].onerror = function() {
var alt = mathcodeList[i].alt;
alt = '\\(' + alt + '\\)';
var curSpan = $('');
curSpan.text(alt);
$(mathcodeList[i]).before(curSpan);
$(mathcodeList[i]).remove();
};
}
}
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
}
}, 500)
});
05-06
1966
1966
12-06
840
840
01-04
1678
1678

本文介绍如何利用Python爬虫抓取城市公交站点信息,通过高德API获取经纬度,再进行数据清洗,包括Excel PQ和Python方法。在QGIS中进行坐标纠偏,推荐使用面向对象编程和PQ进行数据清洗,因为其速度和便利性更优。

被折叠的 条评论
为什么被折叠?



