Python爬取全国或特定城市百度地图慧眼、百度迁徙迁入迁出大数据
生活随笔
收集整理的這篇文章主要介紹了
Python爬取全国或特定城市百度地图慧眼、百度迁徙迁入迁出大数据
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Python爬取全國(guó)或城市的百度地圖慧眼(百度遷徙)遷入、遷出大數(shù)據(jù)
百度地圖慧眼(百度遷移)遷入遷出大數(shù)據(jù)的官網(wǎng)顯示如圖:
?
爬取百度遷移的大數(shù)據(jù),Python代碼實(shí)現(xiàn)如下:
import requests import time import pandas as pd import datetime# 百度遷移方向 0遷入,1遷出 MOVE = ('move_in', 'move_out')""" 構(gòu)造10個(gè)城市名和代碼 代碼可以在民政部查找,連接地址 http://www.mca.gov.cn/article/sj/xzqh/2019/2019/201912251506.html 上述連接是2019年11月中華人民共和國(guó)縣以上行政區(qū)劃代碼 """ CITY = {'北京': '110000','上海': '310000','廣州': '440100','深圳': '440300','成都': '510100','天津': '120000','南京': '320100','杭州': '330100','重慶': '500000','武漢': '420100'}def get_data_from_url(url):print('請(qǐng)求http數(shù)據(jù)', url)resp = requests.get(url, timeout=5)j = resp.json()if j['errmsg'] == 'SUCCESS':data_list = j['data']['list']return data_listelse:print('請(qǐng)求服務(wù)器數(shù)據(jù)失敗!')return Nonedef get_data(city_code, direction, date):if city_code is None:# 根據(jù)日期獲取當(dāng)前全國(guó)熱門(mén)的遷入/遷出城市url_nation = f'http://huiyan.baidu.com/migration/cityrank.json?dt=country&id=0&type={direction}&date={date}'return get_data_from_url(url_nation)else:# 根據(jù)城市碼和日期獲取當(dāng)前城市的遷入/遷出數(shù)據(jù)url_city = f'http://huiyan.baidu.com/migration/cityrank.json?dt=city&id={city_code}&type={direction}&date={date}'return get_data_from_url(url_city)def get_date(year, month, day):d = datetime.date(year, month, day)return d.__format__('%Y%m%d')def main():# 拼接特定日期,年月日date = get_date(2020, 11, 11)# 獲取給定日期、給定城市碼的城市遷入/遷出數(shù)據(jù)data = get_data(city_code=CITY['成都'], direction=MOVE[0], date=date)df = pd.DataFrame(data=data, columns=['city_name', 'province_name', 'value'])df.to_excel('1.xls', encoding='utf-8') # 數(shù)據(jù)寫(xiě)入excel文件print(df)time.sleep(3)# 獲取給定日期全國(guó)熱門(mén)城市遷入/遷出數(shù)據(jù)data = get_data(city_code=None, direction=MOVE[0], date=date)df = pd.DataFrame(data=data, columns=['city_name', 'province_name', 'value'])df.to_excel('2.xls', encoding='utf-8') # 數(shù)據(jù)寫(xiě)入excel文件if __name__ == '__main__':main()?
輸出結(jié)果,數(shù)據(jù)存入excel表格,2020年11月11日全國(guó)熱門(mén)遷入城市排名數(shù)據(jù):
?
?
?
總結(jié)
以上是生活随笔為你收集整理的Python爬取全国或特定城市百度地图慧眼、百度迁徙迁入迁出大数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Linux:9个实用shell运维脚本,
- 下一篇: python 持续集成 教程_jenki