爬取百度地图poi兴趣点(可以更改参数获取你想要的数据)

学习交流,仅供参考。第一次分享自己的代码 主要是软件上面的爬取百度地图的信息的代码质量太低。

本项目基于百度api和selenium因此需要下载

pip install selenium
pip install lxml

有些朋友第一次爬数据没用浏览器的驱动可以参考这个文章

安装 selenium 及配置 edge 浏览器驱动_edge浏览器驱动-CSDN博客

这里可能会存在一个版本问题

话不多说直接上代码改参数

# coding=utf-8
import requests
import json
import pymysql
import re
from selenium import webdriver
from time import sleep
from lxml import etree

'''
获取北京市的公园数据,并用json数据格式返回
'''
def getData(url):
    #下载驱动
    edge = webdriver.Edge(r"D:\anaconda\envs\MachineLearning\msedgedriver.exe")

    edge.get(url)
    sleep(4) #在此期间,可手动操作页面,下拉加载等操作

    page = edge.page_source
    html = etree.HTML(page)
    # 获取加油站其他数据
    rate = html.xpath('//*[@id="generalheader"]/div[1]/div[2]/span[1]/text()')
    style = html.xpath('//*[@id="phoenix_dom_2_0"]/div/div[3]/ul/li[1]/div[1]/@style')
    # infos= html.xpath('//div[@class="product-tile__details"]/p[1]/span[1]/text()')
    # names=html.xpath('//div[@class="product-tile__details"]/p[1]/span[2]/text()')
    # prices=html.xpath('//div[@class="product-tile__details"]/p[2]/text()')
    print(rate)
    try:
        str = style[0]
        start = str.find('url("') + len('url("')  # 找到 URL 起始位置
        end = str.find('")', start)  # 找到 URL 结束位置
        url = str[start:end]  # 截取出 URL
        print(url)
    except:
        url='https://poi-pic.cdn.bcebos.com/swd/62889708-0dcf-39b9-a773-d2e156e4b618.jpg@h_104,w_132,q_100'
        print(url)
    edge.close()
    if(rate==[]):
        rate=["4.5"]
    return url, rate
def get_json(region):
    for i in range(0,20):
        print(i)
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16'
        }
        params = {
            'query': '加油站巴南区',                #检索关键字
            'region': region,               #检索行政区划区域
            'output': 'json',               #输出格式为json
            'scope': '2',                   #检索结果详细程度。取值为1 或空,则返回基本信息;取值为2,返回检索POI详细信息
            'page_size': 10,                #单次召回POI数量,默认为10条记录,最大返回20条。
            'page_num': i,                  #分页页码,默认为0,0代表第一页,1代表第二页,以此类推。
            'ak': '你的ak'
        }


        res = requests.get("http://api.map.baidu.com/place/v2/search", params=params, headers=headers)
        content = res.text
        decodejson = json.loads(content)  #将已编码的 JSON 字符串解码为 Python 对象,就是python解码json对象
        data=decodejson
        data=data.get('results')
        #连接你的数据库
        conn = pymysql.connect(host="localhost", port=3306, user="root", password="root", database="gasstation")
        cursor = conn.cursor()
        sql = "insert gas_stations (address, city, rate, lng, lat, picture, name, phone,status,shop_time) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
        print(data)
        for i in data:
            name=(i['name'])
            address=(i['address'])
            province=(i['province'])
            try:
                telephone=(i['telephone'])
            except:telephone=0
            lng=(i['detail_info']['navi_location']['lng'])
            lat=(i['detail_info']['navi_location']['lat'])
            type=(i['detail_info']['type'])
            shop_hours=(i['detail_info']['shop_hours'])
            url=(i['detail_info']['detail_url'])
            # print(name,address,province,lng,lat,type,shop_hours,telephone,url)
            url, rate=getData(url)
            params = [address,province,rate[0],lng,lat,url,name,telephone,type,shop_hours]
            print(params)
            cursor.execute(sql, params)
            conn.commit()

get_json("重庆市")
  • 27
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值