Python 提取关键字和括号后的文字
Python extract text after keywords and brackets
我是python的新手,在网上搜索了一番,有点迷茫。我想做的是:从一个网站中提取一些信息,该网站的页面源包含以下信息。我想提取最后括号中包含的lat/long信息: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')
我是python的新手,在网上搜索了一番,有点迷茫。我想做的是:从一个网站中提取一些信息,该网站的页面源包含以下信息。我想提取最后括号中包含的lat/long信息: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')