使用re进行正则匹配,requests发送网络请求,pymssql连接数据库,json解析结果
import pymssql
import requests
import json
import re
import time
serverName = '127.0.0.1'
userName = 'sa'
passWord = '123456'
database='uniSchool'
conn = pymssql.connect(server=serverName,user=userName,password=passWord,database=database)
cursor = conn.cursor()
creTab='CREATE TABLE building (id varchar(100), title varchar(100),category varchar(100),lat FLOAT,lng FLOAT)'
cursor.execute(creTab)
key="你的key"
keyword="西南石油大学"
boundary="nearby(30.826486607890168,104.18436350631168,1000,0)"
page_size=20
for i in range(1,11):
print(f'第{i}次开始')
time.sleep(1)
baseUrl = f'https://apis.map.qq.com/ws/place/v1/search?boundary={boundary}&keyword={keyword}&page_size={page_size}&page_index={i}&key={key}'
r=requests.get(baseUrl)
r.encoding='utf-8'
ObjJson=json.loads(r.text)
datas=ObjJson["data"]
for data in datas:
# 这里用下正则,只存入名称开头包含西南石油大学的
txt=data["title"]
if re.match("^西南石油大学", txt):
insert = "insert into building (id,title,category,lat,lng) values (%s, %s, %s, %d, %d)"
da = (data["id"],txt, data["category"], data["location"]["lat"], data["location"]["lng"])
cursor.execute(insert, da)
conn.commit()
print(data)
cursor.close()
conn.close()
结果如图