是否可以触发 Google 地图 api 的 JavaScript 点击侦听器,然后使用 Python 抓取数据?

Is it possible to trigger Google Maps api's JavaScript click listener then scrape the data via using Python?

http://ihe.istanbul/satis-noktalari

我想在使用 Google 地图 api.

的地图上抓取目标公司经销商的点(latLng)数据

我试图通过使用 requests_html 在网站页面上呈现 JavaScript 来抓取数据,然后我曾经使用 BeautifulSoup.

到达该元素
from bs4 import BeautifulSoup
from requests_html import HTMLSession

# create an HTML Session object
session = HTMLSession()

# Use the object above to connect to needed webpage
resp = session.get("http://ihe.istanbul/satis-noktalari")

# Run JavaScript code on webpage
resp.html.render()

soup = BeautifulSoup(resp.html.html, "lxml")

html_content = soup.contents[1]

_script = html_content.find_all("script")[23]
print(_script)

因此,印刷品让我找到了一种方法,如果点击事件被触发,我可以看到所需的区域,在那里我可以找到 latLng 点值。

但是,该网站的 url 无法自行更新并为选定的城市区域放置标签。

为了清楚地解释我自己,我创建了两张图片来准确展示我想做的事情:

此输出显示没有选择城市的结果:

这是显示所需结果的触发点击事件:

如果 url 可以在触发 JavaScript 事件后通过 Google 地图 api 更新,我可以使用 url。 如何使用 Python 触发它,或者如何使用 Python 抓取触发的数据?我提供的 Python 代码显示未触发事件。

假设页面使用选项 value 属性值动态发出 POST XHR 请求,在 select 下拉列表中。您可以提取这些值,模仿 POST 请求页面,然后使用正则表达式从响应中提取经纬度。下面是抓取中心指定坐标的逻辑。

import requests, re

headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'User-Agent': 'Mozilla/5.0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'Referer': 'http://ihe.istanbul/satis-noktalari',
    'Accept-Language': 'en-GB,en-US;q=0.9,en;q=0.8',
}

results = {}

with requests.Session() as s:
    
    s.headers = headers
    r = s.get('http://ihe.istanbul/satis-noktalari')
    soup = bs(r.content, 'lxml')
    options = {i.text:i['value'] for i in soup.select('[name=ilceID] option:nth-child(n+2)')} 
    
    for k, v in options.items():

        data = {'ilceID': v, 'SatisBufe': '1'}

        r = s.post('http://ihe.istanbul/satis-noktalari', data=data)
        
        lat, lon = re.search(r'google.maps.LatLng\(([\d.]+),\s?([\d.]+)\)', r.text).groups()
        
        print(k, f'lat = {lat}', f'lon = {lon}')
        
        results[k] = [lat, lon]