利用python爬取城市公交站点

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

利用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)
    });
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值