欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python利用百度地图爬取商家信息

程序员文章站 2022-12-04 21:08:56
import requestsimport jsonimport pandas as pddef request_hospital_data(): ak="换成自己的 AK" # 换成自己的 AK,需要申请 ct = ('衢州','宁波','台州','温州','丽水','杭州','湖州','金华') keywords=('粥') url = ["http://api.map.baidu.com/place/v2/search?query="+keywords+"&am...
import requests
import json
import pandas as pd
def request_hospital_data():
    ak="换成自己的 AK"  # 换成自己的 AK,需要申请
    ct = ('衢州','宁波','台州','温州','丽水','杭州','湖州','金华')
    keywords=('粥')
    url = ["http://api.map.baidu.com/place/v2/search?query="+keywords+"&page_size=20&scope=1&region=" + cs + "&output=json&ak=" + ak for cs in ct]
    params = {'page_num':0}  # 请求参数,页码
    for url1 in url:
        #print(url1)
        request = requests.get(url1,params=params)
        #print(request)# 请求数据
        total = json.loads(request.text)['total']     # 数据的总条数
        #print(total)
        total_page_num = (total+19) // 20  # 每个页面大小是20,计算总页码
        items = []  # 存放所有的记录,每一条记录是一个元素
        for i in range(total_page_num):
            params['page_num'] = i
            request = requests.get(url1,params=params)
            for item in json.loads(request.text)['results']:
                if "telephone" in item:
                    name = item['name']
                    telephone = item.get('telephone', '')
                    province = item['province']
                    city = item['city']
                    area = item['area']
                    address = item['address']
                    #print(name,telephone,province,city,area,address)
                    new_item = (name,telephone,province,city,area,address)
                    #print(new_item)
                    items.append(new_item)
     #使用pandas的DataFrame对象保存二维数组
            df = pd.DataFrame(items, columns=['name','telephone','province','city','area','address'])
            print(df)
            #df.drop_duplicates(inplace=True)
            df.to_csv('粥1.csv', index=False,encoding='',mode='a')

request_hospital_data()

本文地址:https://blog.csdn.net/weixin_45206990/article/details/108251724

相关标签: 百度地图 python