【问题标题】:Best way to scrape a list of data from a website with python使用 python 从网站上抓取数据列表的最佳方法
【发布时间】:2018-11-30 05:22:18
【问题描述】:

我正在从网页中抓取数据以在 API 中使用,并寻找最 Pythonic/最合适的方法 - 页面源有一个名为“markerData”的字典列表,我需要获取 lat 和液化天然气值。

数据样本:

“标记数据”: [{"docEl":null,"lid":0,"clickable":true,"lat":34.0489281,"lng":-111.0937311,"title":"","iconURL":"//assets.bankofamerica .com/images/mapmarker2.png","info":"

查看所有位置 亚利桑那州

标签: python html web-scraping python-requests lxml


【解决方案1】:

这是我为我完成工作而编写的函数,以防它可能帮助处于类似情况的其他人:

def get_coordinates():

        page = requests.get('https://locators.bankofamerica.com/&check_list=4429')
        tree = html.fromstring(page.content)

        lat_lng = tree.xpath("//script[contains(., 'markerData')]/text()")
        lat_lng_string = str(lat_lng)
        latitude = re.findall('"lat":\d+\.\d+', lat_lng_string)
        longitude = re.findall('"lng":-\d+\.\d+', lat_lng_string)

        la = re.findall('\d+\.\d+', str(latitude))
        lo = re.findall('-\d+\.\d+', str(longitude))

        coords = dict(zip(la, lo))

        return coords

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    相关资源
    最近更新 更多