javascript - Python3请求库提交不允许post请求的表单

标签 javascript forms python-3.x web-scraping python-requests

我正在尝试从 Philly Police webpage 的给定位置获取警察区。我的位置太多,无法手动执行此操作,因此我尝试使用 Python 的请求库来自动化该过程。保存位置值的网页形式如下:

<form id="search-form" method="post" action="districts/searchAddress">
<fieldset>
    <div class="clearfix">
        <label for="search-address-box"><span>Enter Your Street Address</span></label>
        <div class="input">
            <input tabindex="1" class="district-street-address-input" id="search-address-box" name="name" type="text" value="">
        </div>
    </div>
    <div class="actions" style="float: left;">
        <button tabindex="3" type="submit" class="btn btn-success">Search</button>
    </div>
    <a id="use-location" href="https://www.phillypolice.com/districts/index.html?_ID=7&_ClassName=DistrictsHomePage#" style="float: left; margin: 7px 0 0 12px;"><i class="icon-location-arrow"></i>Use Current Location</a>
    <div id="current-location-display" style="display: none;"><p>Where I am right now.</p></div>
</fieldset>
</form>

但是,当我尝试使用以下内容发布或放入网页时:

r = requests.post('http://www.phillypolice.com/districts',data={'search-address-box':'425 E. Roosevelt Blvd'})

我收到错误 405,不允许 POST。然后我关闭了 Javascript 并尝试在网页上查找该分区,当我点击“提交”时,我收到了相同的 405 错误消息。因此,肯定不会提交表单,而是使用 JavaScript 找到分区。

有没有办法使用请求库模拟“单击”提交按钮来触发 JavaScript?

最佳答案

首先查询谷歌地图到坐标后检索数据,最终请求是如下所示的 get:

enter image description here

您可以使用 bing maps api 设置免费帐户并获取发出获取请求所需的坐标:

import requests

key = "my_key"
coord_params = {"output": "json",
                "key": key}

# This provides the coordinates.
coords_url = "https://dev.virtualearth.net/REST/v1/Locations"

# Template to pass each address to in your actual loop.
template = "{add},US"
url = "https://api.phillypolice.com/jsonservice/Map/searchAddress.json"
with requests.Session() as s:
    # Add the query param passing in each zipcode
    coord_params["query"] = template.format(add="425 E. Roosevelt Blvd")
    js = s.get(coords_url, params=coord_params).json()
    # Parse latitude and longitude from the returned json.
    # Call str to make make it into `(lat, lon)`
    latitude_longitude = str((js[u'resourceSets'][0][u'resources'][0]["point"][u'coordinates']))
    data = s.get(url, params={"latlng": latitude_longitude})

    print(data.json())

如果我们运行它减去我的 key :

In [2]: import requests
   ...: 
   ...: key = "my_key..."
   ...: 
   ...: coord_params = {"output": "json",
   ...:                 "key": key}
   ...: coords_url = "https://dev.virtualearth.net/REST/v1/Locations"
   ...: template = "{add},US"
   ...: url = "https://api.phillypolice.com/jsonservice/Map/searchAddress.json"
   ...: with requests.Session() as s:
   ...:     coord_params["query"] = template.format(add="425 E. Roosevelt Blvd")
   ...: 
   ...:     js = s.get(coords_url, params=coord_params).json()
   ...:     latitude_longitude = str(js[u'resourceSets'][0][u'resources'][0]["po
   ...: int"][u'coordinates'])
   ...:     print(latitude_longitude)
   ...:     data = s.get(url, params={"latlng": latitude_longitude})
   ...:     print(data.json())
   ...:     
[40.02735900878906, -75.1153564453125]
{'response': ['35', '2', 'Marques Newsome', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2a7a7a6e04191f757a796b186a5a4243464b044d455c" rel="noreferrer noopener nofollow">[email protected]</a> ', '267-357-1436']}

如果您在浏览器中查看请求,您会发现它与您看到的响应相匹配。

关于javascript - Python3请求库提交不允许post请求的表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39927760/

相关文章:

php - 将 PHP 转换为 JavaScript。将键/值存储到数组中

java - 如何将表单输入值传递给 Action (struts 1)

python-3.x - 带有 Voila 的 Ipywidgets 未显示 : ERROR tornado Uncaught exception GET

python - 如何获取用户从顶层组合框中选择的选项

python-3.x - 在 python 3.3.3 中安装 pywin32

javascript - vue-cli项目修改端口号的方法

javascript - Angular ng-view : show after hidden

javascript - 对 NodeJS+Express 进行基本 GET 请求的 Angular 调用

ruby - Rails 根据 Action 隐藏表单字段

django - 更新表单 - 使用表单集 - 直到运行时才知道初始值