如何从 link 中提取经度和纬度

How to extract longitude and latitude from a link

从下面的link,我正在尝试提取经度和纬度。我找到了类似的帖子,但没有找到格式相同的帖子。我是 regex/text 操作的新手,如果有任何关于如何使用 Python 执行此操作的指导,我将不胜感激。我希望从此示例中获得的输出是

latitude = 40.744221 
longitude = -73.982854

非常感谢。

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

我猜这个表达式可能 return 我们想要的输出:

center=(-?\d+\.\d+)%2C(-?\d+\.\d+)

测试 re.findall

import re

regex = r"center=(-?\d+\.\d+)%2C(-?\d+\.\d+)"

test_str = "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"

print(re.findall(regex, test_str))

测试 re.finditer

import re

regex = r"center=(-?\d+\.\d+)%2C(-?\d+\.\d+)"

test_str = "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"

matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches, start=1):

    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1

        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))

表达式在 this demo, if you wish to explore/simplify/modify it, and in this link 的右上面板进行了解释,如果您愿意,可以逐步观看它如何与一些示例输入匹配。

在元组打包的字符串上使用简单的 re.search:

lattitude, longitude = re.search(r'center=(.*?)%2C(.*?)&', s).groups()

其中 s 是您的字符串 (link)。

示例

import re

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

lattitude, longitude = re.search(r'center=(.*?)%2C(.*?)&', s).groups()

print(lattitude)  # 40.744221
print(longitude)  # -73.982854

Python在标准库

中有解析URLs的模块
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(",")

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

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