Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何在Python中从URL中提取坐标(lat、lan)?_Regex_Python 2.7_Coordinates - Fatal编程技术网

Regex 如何在Python中从URL中提取坐标(lat、lan)?

Regex 如何在Python中从URL中提取坐标(lat、lan)?,regex,python-2.7,coordinates,Regex,Python 2.7,Coordinates,我对如何在Python中从URL提取坐标(Lat,Long)有点迷茫 我总是会收到这样的url: https://www.testweb.com/cordi?ll=41.403781,2.1896&z=17&pll=41.403781,2.1896 这里我需要提取这个URL的第二组(在本例中:41.403781,2.1896),也就是说,第一组和第二组坐标并不总是相同的 我知道,一些正则表达式可以做到这一点,但我做得不够好。下面是如何使用正则表达式: import re m =

我对如何在Python中从URL提取坐标(Lat,Long)有点迷茫

我总是会收到这样的url:

https://www.testweb.com/cordi?ll=41.403781,2.1896&z=17&pll=41.403781,2.1896
这里我需要提取这个URL的第二组(在本例中:41.403781,2.1896),也就是说,第一组和第二组坐标并不总是相同的


我知道,一些正则表达式可以做到这一点,但我做得不够好。

下面是如何使用正则表达式:

import re
m = re.search(r'pll=(\d+\.\d+),(\d+\.\d+)', 'https://www.testweb.com/cordi?ll=41.403781,2.1896&z=17&pll=41.403781,2.1896')
print m.groups()
结果:
('41.403781','2.1896')


您可能需要查看模块
urlparse
,以获得更健壮的解决方案。

以下是如何使用正则表达式:

import re
m = re.search(r'pll=(\d+\.\d+),(\d+\.\d+)', 'https://www.testweb.com/cordi?ll=41.403781,2.1896&z=17&pll=41.403781,2.1896')
print m.groups()
结果:
('41.403781','2.1896')

您可能需要查看模块
urlparse
,以获得更健壮的解决方案。

urlparse有一个函数“urlparse”和“parse_qs”,用于可靠地访问此数据,如下所示

$ python
Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> u="""https://www.testweb.com/cordi?ll=41.403781,2.1896&z=17&pll=41.403781,2.1896"""
>>> import urlparse
>>> x=urlparse.urlparse(u)
>>> x
ParseResult(scheme='https', netloc='www.testweb.com', path='/cordi', params='', query='ll=41.403781,2.1896&z=17&pll=41.403781,2.1896', fragment='')
>>> x.query
'll=41.403781,2.1896&z=17&pll=41.403781,2.1896'
>>> urlparse.parse_qs(x.query)
{'ll': ['41.403781,2.1896'], 'z': ['17'], 'pll': ['41.403781,2.1896']}
>>> 
urlparse有一个函数“urlparse”和“parseqs”,用于可靠地访问此数据,如下所示

$ python
Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> u="""https://www.testweb.com/cordi?ll=41.403781,2.1896&z=17&pll=41.403781,2.1896"""
>>> import urlparse
>>> x=urlparse.urlparse(u)
>>> x
ParseResult(scheme='https', netloc='www.testweb.com', path='/cordi', params='', query='ll=41.403781,2.1896&z=17&pll=41.403781,2.1896', fragment='')
>>> x.query
'll=41.403781,2.1896&z=17&pll=41.403781,2.1896'
>>> urlparse.parse_qs(x.query)
{'ll': ['41.403781,2.1896'], 'z': ['17'], 'pll': ['41.403781,2.1896']}
>>> 

肯定有一个不错的Python URL解析器吗?它在URL中总是排在最后吗?因为如果是这样的话,
[\d,]+$
将完成这项工作。谢谢你,比芬,如果有什么我会看一看。是的@noob这就是所有的模式。更新了演示以只提取坐标。肯定有一个适合Python的URL解析器吗?它总是在URL中最后一个吗?因为如果是这样的话,
[\d,]+$
将完成这项工作。谢谢你,比芬,如果有什么我会看一看。是@noob这是所有的模式。更新了演示,只提取坐标。谢谢!我去看看谢谢!我来看看