博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
遍历接口参数,自动计算url并进行签名
阅读量:5877 次
发布时间:2019-06-19

本文共 3919 字,大约阅读时间需要 13 分钟。

我们先选个接口,如路径规划接口,会出现Fromx ,y;Tox,y。两种参数,分别要所有城市进行便利(所有城市坐标经纬度,可以通过爬虫从高德地图上爬下来),具体方法请搜博客园

代码如下:

#encoding:utf-8#遍历所有城市路径规划的测试脚本from xlutils.copy import copyimport urlparse, copy,urllib,xlrd,xlwt,urllib2from lxml import etreefrom hashlib import md5def url_values_xy(url,fromX,fromY,toX,toY,sign):    ret = []    u = urlparse.urlparse(url)    qs = u.query    pure_url = url.replace('?'+qs, '')    #print pure_url    qs_dict = dict(urlparse.parse_qsl(qs))    tmp_dict = copy.deepcopy(qs_dict)    tmp_dict['fromX'] = fromX    tmp_dict['fromY'] = fromY    tmp_dict['toX'] = toX    tmp_dict['toY'] = toY    tmp_dict['sign'] = sign    tmp_qs = urllib.unquote(urllib.urlencode(tmp_dict))    ret.append(pure_url + "?" + tmp_qs)    return retdef openurl(url):    try:        response=urllib2.urlopen(url)        html = response.read()        return ('pass',html)    except urllib2.URLError,e:        ex=e.code+e.reason        return ("except",ex)        def case(EC_result,html):    root = etree.fromstring(html)    Message=root.xpath('/ats/message')[0].text    valuelist=[]    if Message ==  "Successful.":        #__VALUE=root.xpath('/ats/weather/livingindex/day/item/name') #PSA        #__VALUE=root.xpath('/ats/weatherlive/forecast_date') #public        __VALUE=root.xpath('/ats/weatherday/hour/wind_direction') #volvo        for val in __VALUE:            value=val.text            valuelist.append(value)            strvalue =";".join(valuelist)        if EC_result in strvalue:            return "PASS"        else:            return "FALSE"    else:        return Message#autonavi的key_license_="12345"#_buffer ='uidA0001fromnbsofttime2015-03-02T12:24:53'def sign(_buffer):    if (_buffer is None) or (_buffer==""):        return None    if isinstance(_buffer, unicode):        _buffer = _buffer.encode("utf-8")    try:        return md5("%s@%s" % (_buffer, _license_)).hexdigest().upper()        print md5    except:        return None#print sign(_buffer)def test(_fromX,_fromY,_toX,_toY):    _buffer = "".join([_fromX,_fromY,_toX,_toY])    return sign(_buffer)'''if __name__=='__main__':     print test("116.418757","39.917544","116.235906","40.218085")'''       def routing():    oldex = xlrd.open_workbook(r'oldbianli.xls')    oldsh = oldex.sheet_by_name('abc')    nrows = oldsh.nrows    #print nrows    newex = xlwt.Workbook()    newsh = newex.add_sheet('abc' , cell_overwrite_ok = True )    newsh.write(0,0,'oldurl')    newsh.write(0,2,'fromX')    newsh.write(0,3,'fromY')    newsh.write(0,4,'toX')    newsh.write(0,5,'toY')    newsh.write(0,6,'newurl')    newsh.write(0,1,'city')    for i in xrange(1,nrows):        oldurl = oldsh.cell(1,0).value.strip()        newsh.write(1,0,oldurl)        fromX = str(oldsh.cell(i,2).value)        fromY = str(oldsh.cell(i,3).value)        try:            toX = str(oldsh.cell(i+1,2).value)        except:            toX = str(oldsh.cell(0,2).value)        try:            toY = str(oldsh.cell(i+1,3).value)        except:            toY = str(oldsh.cell(0,3).value)        print fromX,fromY,toX,toY        print test(fromX,fromY,toX,toY)        city = oldsh.cell(i,0).value        newsh.write(i,2,fromX)        newsh.write(i,3,fromY)        newsh.write(i,4,toX)        newsh.write(i,5,toY)        #newsh.write(i,5,test(fromX,fromY,toX,toY))        newsh.write(i,1,city)                        urls = url_values_xy(oldurl,fromX,fromY,toX,toY,test(fromX,fromY,toX,toY))        for new_url in urls:            status,html=openurl(new_url)            newsh.write(i,6,new_url)            print "#%d."%i,u"newurl\n",new_url                        #if status == 'pass':            #    EC_result =u'风'            #    newsh.write(i,4,case(EC_result,html))            #else:            #    return u'网页失败'                newex.save(r'newbianli.xls')if __name__=='__main__':       routing()

 代码不知道大家能否看懂,如有不懂请留言

便利需要用到的 fromX,Y,toX,Y。产生新的url

 

转载于:https://www.cnblogs.com/BUGU/p/4739285.html

你可能感兴趣的文章
我的友情链接
查看>>
使用Nginx搭建WEB服务器
查看>>
【oracle唯一主键SYS_GUID()】
查看>>
PyQt5 ImportError: DLL load failed: 找不到指定的模块
查看>>
我的友情链接
查看>>
作业2
查看>>
raid技术-研究感受
查看>>
远程主机探测技术FAQ集 - 扫描篇
查看>>
C++中调用python函数
查看>>
Nomad添加acl认证
查看>>
“TI门外汉”网路知识笔记一 OSI参考模型
查看>>
你不需要jQuery(五)
查看>>
DatanodeDescriptor说明
查看>>
ServlertContext
查看>>
eclipse编辑器生命周期事件监听
查看>>
Python WOL/WakeOnLan/网络唤醒数据包发送工具
查看>>
sizeof(long)
查看>>
pxe网络启动和GHOST网克
查看>>
ftp 虚拟用户的使用(安装)
查看>>
2.5-saltstack配置apache
查看>>