直接上代码
import requests
import sys
import re
import string
import urllib.parse
from openpyxl import load_workbook
from urllib.parse import quote
requests.packages.urllib3.disable_warnings()
def FIND(string):
#定义两个变量:分别表示开始的字符串,结束的字符串
start1 = '"pointx":"'
end1 = '",'
#使用find找到开始和结束截取的位置
s3=string
s = string.find(start1)
e = string.find(end1)
#找到第一个
sub_str = string[s:e + len(end1)]
s1=sub_str.replace(start1,'')
s1=s1.replace(end1,'')
s1=s1.replace('</p>','')
count = string.count(start1)
for x in range(0,count):
s = string.find(start1,e)
e = string.find(end1,s)
sub_str = string[s:e + len(end1)]
s2=sub_str.replace(start1,'')
s2=s2.replace(end1,'')
s2=s2.replace('</p>','')
return s2
def FIND1(string):
#定义两个变量:分别表示开始的字符串,结束的字符串
start1 = '"pointy":"'
end1 = '",'
#使用find找到开始和结束截取的位置
s3=string
s = string.find(start1)
e = string.find(end1)
#找到第一个
sub_str = string[s:e + len(end1)]
s1=sub_str.replace(start1,'')
s1=s1.replace(end1,'')
s1=s1.replace('</p>','')
count = string.count(start1)
for x in range(0,count):
s = string.find(start1,e)
e = string.find(end1,s)
sub_str = string[s:e + len(end1)]
s2=sub_str.replace(start1,'')
s2=s2.replace(end1,'')
s2=s2.replace('</p>','')
return s2
def tmain(str):
try:
str = urllib.parse.quote(str)
#print(str)
response = requests.get('https://apis.map.qq.com/jsapi?qt=geoc&addr='+str+'&key=UGMBZ-CINWR-DDRW5-W52AK-D3ENK-ZEBRC&output=jsonp&pf=jsapi&ref=jsapi&cb=qq.maps._svcb3.geocoder0:',verify=False)
#print(response.text)
a=FIND(response.text)
b=FIND1(response.text)
return a,b
except:
pass
if __name__ == '__main__':
file = '0.xlsx'
wb = load_workbook(file)
ws = wb.active
for i in range(2,ws.max_row+1):
print(ws.cell(row=i, column=4).value)
c,d=tmain(ws.cell(row=i, column=4).value)
print(i,c,d)
ws.cell(row=i, column=5).value = c
ws.cell(row=i, column=6).value = d
wb.save(file)
结果: