【问题标题】:Extracting latitude and longitude with BeautifulSoup (script tag)用 BeautifulSoup(脚本标签)提取经纬度
【发布时间】:2017-05-12 08:55:55
【问题描述】:

我正在尝试从这部分 HTML 中提取纬度和经度(有两对纬度/经度,我需要它来处理任意数量的坐标):

<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

以及所有其他类型的 stackoverflow 提案,但没有任何效果。

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

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

有人有想法吗?

非常感谢,

玛丽

【问题讨论】:

    标签: python web-scraping geolocation beautifulsoup


    【解决方案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)
    

    【讨论】:

    • 非常感谢,它适用于使用脚本完成的字符串,但是如何以字符串格式的方式从 HTML 中提取代码?我一般都是做soup.find_all('script'),所以现在的格式是bs4.element.Tag
    • 使用str(soup.select('script'))
    猜你喜欢
    • 2015-09-05
    • 1970-01-01
    • 2022-06-27
    • 1970-01-01
    • 2020-11-14
    • 2023-03-03
    • 2014-06-16
    • 1970-01-01
    • 2020-06-28
    相关资源
    最近更新 更多