码迷,mamicode.com
首页 > Web开发 > 详细

遍历接口参数,自动计算url并进行签名

时间:2015-08-18 15:46:24      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

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

代码如下:

#encoding:utf-8
#遍历所有城市路径规划的测试脚本

from xlutils.copy import copy
import urlparse, copy,urllib,xlrd,xlwt,urllib2
from lxml import etree
from hashlib import md5

def 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 ret

def 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_="429b811c781e400b83120ae301a9ad2e"

#_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

 

遍历接口参数,自动计算url并进行签名

标签:

原文地址:http://www.cnblogs.com/BUGU/p/4739285.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!