Python - 有没有更有效的方法来查找这些参数值?

时间:2012-05-09 05:03:41

标签: python regex beautifulsoup lxml

目前正在使用beautifulsoup来找到y值返回的标签:

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

返回“经度= 27.2115500”

是否有更好的方法可以返回BusinessName,Address,Latitude,Longitude和AdvertiserID的值,其中y的值为:

<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>'''

3 个答案:

答案 0 :(得分: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"]

答案 1 :(得分: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]

答案 2 :(得分: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]