[英]Python extract text after keywords and brackets
我是python的新手,在尝试了一些Internet搜索之后,感到有些困惑。 我要执行的操作如下:从网站中提取一些信息,该网站的页面源包含以下信息。 我想提取最后包含在方括号中的经纬度信息:19.xxxxx,-19.xxxxx。
我的想法是搜索myOptions,然后检索方括号内的坐标。 我该如何实施? 谢谢!
<script>
function initialize() {
var map, mapOptions, info, i, func, func1, borrar, capa,
marcador = [], marcadorcalle = [], locales = [], calles = [];
func = function (num, tipo) {
return function () {
if (tipo) {
info.setContent('<b>' + calles[num][0] + '</b>');
info.open(map, marcadorcalle[num]);
} else {
info.setContent('<b>' + locales[num][0] + '</b><br />' + locales[num][3]);
info.open(map, marcador[num]);
}
};
};
func1 = function (objeto, tipo) {
return function () {
if (tipo) {
if (borrar) {borrar.setMap(null); }
borrar = objeto;
objeto.setMap(map);
}
map.setZoom(18);
map.setCenter(objeto.getPosition());
google.maps.event.trigger(objeto, 'click');
};
};
mapOptions = {
zoom: 16,
scrollwheel: false,
center: new google.maps.LatLng(19.xxxxx, -19.xxxxx)
};
这是正则表达式最能发挥作用的地方:
import re
map_lat_long = re.compile(r'google\.maps\.LatLng\(([\d.-]+),\s*([\d.-]+)\)')
lat, long = map_lat_long.search(page_source).groups()
假设使用的是实际数字而不是xxxxx
。 该表达式与文字google.maps.LatLng(..)
文本匹配,并通过查找1个或多个数字,每个点和破折号从中提取两个数字。
演示(样本减少):
>>> import re
>>> sample = '''\
... mapOptions = {
... zoom: 16,
... scrollwheel: false,
... center: new google.maps.LatLng(19.12345, -19.67890)
... };
... '''
>>> map_lat_long = re.compile(r'google\.maps\.LatLng\(([\d.-]+),\s*([\d.-]+)\)')
>>> map_lat_long.search(sample).groups()
('19.12345', '-19.67890')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.