Traverse interface parameters, automatically calculate URLs and sign them

Source: Internet
Author: User
Tags xpath

We first choose an interface, such as path planning interface, will appear FromX, Y;tox,y. Two parameters, for all cities to facilitate (all the city coordinates latitude and longitude, can be crawled from the top of the map down), specific methods please search the blog park

The code is as follows:

#encoding: utf-8# Traverse all City path planning test scripts 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 (u RL) 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[' fr    OmX "] = 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.fromstr ing (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/f            Orecast_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 of 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 ("%[email prote  cted]%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 Routi Ng (): Oldex = Xlrd.open_workbook (R ' Oldbianli.xls ') oldsh = Oldex.sheet_by_name (' abc ') nrows = Oldsh.nrows #pri NT 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 ') ne Wsh.write (0,1, ' city ') for I in Xrange (1,nrows): Oldurl = Oldsh.cell (1,0). Value.strip () Newsh.write (1,0,ol Durl) 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 (olds H.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,FR omy) Newsh.write (I,4,tox) newsh.write (I,5,toy) #newsh. Write (I,5,test (Fromx,fromy,tox,toy)) New        Sh.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 ' Wind ' # newsh. Write (I,4,case (ec_result,html)) #else: # return U ' page failed ' Newex.save (R ' Newbianli.xls       ') if __name__== ' __main__ ': Routing ()

Code do not know whether you can understand, if you do not understand please leave a message

To facilitate the use of fromx,y,tox,y. Generate a new URL

Traverse interface parameters, automatically calculate URLs and sign them

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.