从 url 获取纬度和经度

get the latitude and longitude from url

使用 xpath 我能够获得包含纬度和经度的 url,但我需要通过以下方式单独显示这些值:

纬度 = -34.552654847695510 经度= -58.457549057672110

<div class="article-map" id="article-map">
<img id="static-map" src="//maps.google.com/maps/api/staticmap?center=-34.552654847695510,-58.457549057672110&amp;zoom=16&amp;markers=-34.552654847695510,-58.457549057672110&amp;channel=ZP&amp;size=780x456&amp;sensor=true&amp;scale=2&amp;key=AIzaSyDuxqN04nAj6aHygffqUpehsbMFbxEZX90&amp;signature=W-cOkT98ssMPpXbZbU3jil5xNes=" class="static-map">
</div>    


response.xpath ('// div [@ id = "article-map"] / img'). extract ()

['<img id = "static-map" src = "// maps.google.com/maps/api/staticmap?center=-34.552654847695510,-58.457549057672110&amp;zoom=16&amp;markers=-34.552654847695510,-58.457549057672110&amp; channel = ZP & amp; size = 780x456 & amp; sensor = true & amp; scale = 2 & amp; key = AIzaSyDuxqN04nAj6aHygffqUpehsbMFbxEZX90 & signature = W-cOkT98ssMPpXbZbU3jil5xNes = "class =" static-map "> ']

试试这个,例如:response.css('#article-map img::attr(src)').re(r'markers=([-\d\.]+),([-\d\.]+)')

或者 一种。获得 url 赞 response.css('#article-map img::attr(src)').get() b.通过 from w3lib.url import url_query_parameter 提取 markerscenter 参数,然后应用正则表达式。

但第一个变体看起来更短更容易。

使用url解析模块方便且准确:

from urllib.parse import urlparse, parse_qs

img_url_string = Selector(text=body).xpath('//img[@id="static-map"]/@src').extract_first()

url_data = urlparse(img_url_string, scheme='https')

qs = url_data.query
parse_qs(qs)['center']
# output ['-34.552654847695510,-58.457549057672110']