在线互动地图中的Web搜集底层数据

时间:2019-04-02 12:11:05

标签: web-scraping inspect jqxhr

我正在尝试从此网站上的交互式地图中获取基础数据:https://www.sabrahealth.com/properties

我尝试使用Google Chrome上的“检查”功能来查找XHR文件,该文件将保存地图上所有点的位置,但什么都没有出现。还有另一种方法可以从这张地图中提取位置数据吗?

1 个答案:

答案 0 :(得分:0)

好,位置数据可在其站点here上下载。但是,假设您要实际的纬度,经度值进行一些分析。

我要做的第一件事就是您所做的(查找XHR)。如果在那里找不到任何内容,我经常做的第二件事就是在html中搜索<script>标签。有时数据“藏在”那里。这需要更多的侦探工作。它并不总是能产生结果,但在这种情况下确实可以。

如果您在<script>标签中查找,则会找到相关的json格式。然后,您可以使用它。只需找到它,然后处理字符串以获取有效的json格式,然后使用json.loads()即可将其输入。

import requests
import bs4
import json


url = 'https://www.sabrahealth.com/properties'

headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'}


response = requests.get(url, headers=headers)

soup = bs4.BeautifulSoup(response.text, 'html.parser')

scripts = soup.find_all('script')
for script in scripts:
    if 'jQuery.extend(Drupal.settings,' in script.text:
        jsonStr = script.text.split('jQuery.extend(Drupal.settings,')[1]
        jsonStr = jsonStr.rsplit(');',1)[0]

        jsonObj = json.loads(jsonStr)


for each in jsonObj['gmap']['auto1map']['markers']:
    name = each['markername']
    lat = each['latitude']
    lon = each['longitude']

    soup = bs4.BeautifulSoup(each['text'], 'html.parser')

    prop_type = soup.find('i', {'class':'property-type'}).text.strip()
    sub_cat = soup.find('span', {'class':'subcat'}).text.strip()

    location = soup.find('span', {'class':'subcat'}).find_next('p').text.split('\n')[0]


    print ('Type: %s\nSubCat: %s\nLat: %s\nLon: %s\nLocation: %s\n' %(prop_type, sub_cat, lat, lon, location))

输出:

Type: Senior Housing - Leased
SubCat: Assisted Living
Lat: 38.3309
Lon: -85.862521
Location: Floyds Knobs, Indiana

Type: Skilled Nursing/Transitional Care
SubCat: SNF
Lat: 29.719507
Lon: -99.06649
Location: Bandera, Texas

Type: Skilled Nursing/Transitional Care
SubCat: SNF
Lat: 37.189079
Lon: -77.376015
Location: Petersburg, Virginia

Type: Skilled Nursing/Transitional Care
SubCat: SNF
Lat: 37.759998
Lon: -122.254616
Location: Alameda, California

...
相关问题