如何从链接中提取经度和纬度

cha*_*cha 3 python regex text-extraction extraction data-extraction

从下面的链接中,我试图提取经度和纬度。我发现了类似的帖子,但没有找到具有相同格式的帖子。我是regex /文本操作的新手,并且希望获得有关如何使用Python进行此操作的任何指导。我想从此示例获得的输出是

latitude = 40.744221 
longitude = -73.982854
Run Code Online (Sandbox Code Playgroud)

提前谢谢了。

https://maps.googleapis.com/maps/api/staticmap?scale=1¢er=40.744221%2C-73.982854&language=zh-TW&zoom=15&​​markers=scale%3A1%7Cicon%3Ahttps%3A%2F%2Fyelp-images.s3.amazonaws .com%2Fassets%2Fmap-markers%2Fannotation_32x43.png%7C40.744221%2C-73.982854&client = gme-yelp&sensor = false&size = 315x150&signature = OjixVjNCwF7yLR5tsYw2fDRZ7bw

Tim*_*Tim 5

Python在标准库中有一个用于解析URL的模块

from urllib import parse

# Split off the query
_, query_string = parse.splitquery("https://maps.googleapis.com/maps/api/staticmap?scale=1&center=40.744221%2C-73.982854&language=en&zoom=15&markers=scale%3A1%7Cicon%3Ahttps%3A%2F%2Fyelp-images.s3.amazonaws.com%2Fassets%2Fmap-markers%2Fannotation_32x43.png%7C40.744221%2C-73.982854&client=gme-yelp&sensor=false&size=315x150&signature=OjixVjNCwF7yLR5tsYw2fDRZ7bw")

# Parse the query into a dict
query = parse.parse_qs(query_string)

# You can now access the query using a dict lookup
latlng = query["center"]

# And to get the values (selecting 0 as it is valid for a query string to contain the same key multiple times).
latitude, longitude = latlng[0].split(",")
Run Code Online (Sandbox Code Playgroud)

对于这个用例,我会避免使用正则表达式。该urllib模块更加明确,将处理URL编码的所有方面,并且都经过了良好的测试。

另一个出色的用于处理URL的第三方模块是出色的YARL