2017-05-12 29 views
0

我想从HTML的这部分中提取纬度和经度(有两对经度/纬度,我需要它可以用于任意数量的坐标):使用BeautifulSoup提取经度和纬度(脚本标记)

<script type="text/javascript"> 
[...] 
truvo.data['map']= [{"lat":50.469585,"lon":4.487113,"id":"fr_BE_YP_PAID_16758523_0000_2840991_8600_20139917392","number":"1","display":"1","customerid":"16758523","addressid":"2840991","part":"base","type":"paid"},{"lat":50.721645,"lon":4.6253505,"id":"fr_BE_YP_PAID_12075596_0000_2315340_8600_20139200640","number":"2","display":"2","customerid":"12075596","addressid":"2315340","part":"base","type":"paid"}] 
; 
</script> 

我试了几种方法:

how to access latitude and longtitude in a script with beautifulsoup?

How to scrape latitude longitude in beautiful soup

和所有其他类型的Ø f stackoverflow提案,但没有任何工作。

如果我使用一种模式,那一个是正确的吗?

'("lat"|"lon"):(-?\d{1,3}\.\d+)' 

有人有想法吗?

非常感谢,

玛丽

回答

1

你是几乎没有,您需要从regex

>>> re.findall(r'("lat"|"lon"):(\d{1,3}\.\d+)', data) 
[('"lat":', '50.469585'), 
('"lon":', '4.487113'), 
('"lat":', '50.721645'), 
('"lon":', '4.6253505')] 

删除-或者你也可以尝试(这已经为你工作)

>>> re.findall(r'(?is)("lat":|"lon":)([0-9.]+)',data) 
+0

非常感谢,它使用脚本完成了一个字符串,但是ho我是否可以用字符串格式的方式从HTML中提取代码?我通常做soup.find_all('script'),所以目前的格式是bs4.element.Tag – MarieC

+0

使用'str(soup.select('script'))' –

+0

非常感谢,效果很好! – MarieC