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

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

最佳答案

可以读取标签的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"]

关于Python - 是否有更有效的方法来查找这些参数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10510124/

相关文章:

python - 如何使用 QSortFilterProxyModel 在 QTreeView 中重新启用手动列布局

python - 使用函数替换空值

python - 将字符串数据传递给 matplotlib API 时会绘制什么?

javascript 正则表达式与 g 标志匹配但不与 y 标志匹配

JavaScript - 防止用户在输入文本中复制粘贴特殊字符?

python - 如何使用 Beautifulsoup 访问前五个谷歌结果链接

python - 导入错误 : No module named enum on python 2. 7

c# - 使用正则表达式检查字符串中的特定字符

Python - BeautifulSoup 抓取非标准网络表

python - 使用 find_parent 结果从中获取特定项目