1

currently using beautifulsoup to find this a tag which y value return:

and

Longitude = re.search("Longitude\=[\d]+\.[\d]+", str(y)).group(0)

returns "Longitude=27.2115500"

is there a better way to return the values of BusinessName, Address, Latitude, Longitude, and AdvertiserID, where the value of y is:

<a href="http://maps.website.com/default.aspx?AdvertiserID=ZZ_13130030_5182_000761&amp;Latitude=-18.1320800&amp;Longitude=17.2115500&amp;BusinessName=ACME Ltd&amp;Address=55 Nowhere Rd In This Town&amp;telno=&amp;advertiser=location&amp;routeType=to&amp;origincoords=-18.1320800|17.2115500#tabs-2;" 
onclick="dcsMultiTrack('DCSext._mainreq','','DCSext.linktype','get directions','DCSext.linkplace','','DCSext.linkvalue','','DCSext.show_listingId','ZZ_13130030_5182_000761_8378972_IYMX','DCSext.show_zoningUsed','0','DCSext.show_resultNumber','2')" target="_blank">Get directions</a>'''
4

3 回答 3

2

利用urlparse

>>> import urlparse
>>> the_url = """
http://maps.website.com/default.aspx?AdvertiserID=ZZ_13130030_5182_000761&amp;Latitude=-18.1320800&amp;Longitude=17.2115500&amp;BusinessName=ACME Ltd&amp;Address=55 Nowhere Rd In This Town&amp;telno=&amp;advertiser=location&amp;routeType=to&amp;origincoords=-18.1320800|17.2115500#tabs-2;
"""
>>> query_string = urlparse.parse_qs(urlparse.urlparse(the_url)[4])
>>> query_string['AdvertiserID'][0]
于 2012-05-09T05:18:58.460 回答
2

您可以使用:读取href标签的属性href = tag["href"],然后获取参数:

   from urlparse import urlparse
   o = urlparse(href)
   params = dict(query.split("=") for query in o.query.split("&"))

那么你可以通过以下方式获得价值:

params["Longitude"], params["BusinessName"], ETC

于 2012-05-09T05:17:26.077 回答
0

在 Python 3.5 中:

import urllib
from urllib.parse import urlparse

url = 'http://tupagina.com/?var1=6e5d8&var2=40'
#url = str(request.get_full_path())
parsed = urlparse(url)
qs = urllib.parse.parse_qs(parsed.query)
var1 = qs['var1'][0]
var2 = qs['var2'][0]
于 2016-05-30T16:30:39.657 回答