尝试发布表单提交请求失败

时间:2016-07-30 04:47:42

标签: python forms python-requests urllib2 urllib

我正在研究我的第一个项目,使用python提交表单并从http://www.weather.gov

检索天气数据

我是HTTP表单提交的新手,因此很有可能我认为这一切都是错误的。我已经读到机械化和/或硒对于这类工作更有效率,但我的学校服务器仅限于这些模块。

import requests

r = requests.get('http://www.weather.gov')

location = raw_input("Enter zipcode: ")
payload = {'key1' : location}

q = requests.post('http://forecast.weather.gov/', data = payload)

print q.text

我尝试搜索给定的邮政编码是不成功的,我没有达到给定邮政编码的当地天气。

注意:我也尝试使用urllib& urllib2没有成功。

import urllib
import urllib2

location = raw_input("Enter Zip Code here: ")

url = 'http://forecast.weather.gov/'
values = {'inputstring' : location}
data = urllib.urlencode(values)

req = urllib2.Request(url, data = data)
response = urllib2.urlopen(req)

html = response.read()
print html

从检查页面看到的表格:

<form name="getForecast" id="getForecast" action="http://forecast.weather.gov/zipcity.php" method="get">
                <label for="inputstring">Local forecast by <br>"City, St" or ZIP code</label>
                <input id="inputstring" name="inputstring" type="text" value="Enter location ..." onclick="this.value=''" autocomplete="off">
                <input name="btnSearch" id="btnSearch" type="submit" value="Go">
                <div id="txtError">
                    <div id="errorNoResults" style="display:none;">Sorry, the location you searched for was not found. Please try another search.</div>
                    <div id="errorMultipleResults" style="display:none">Multiple locations were found. Please select one of the following:</div>
                    <div id="errorChoices" style="display:none"></div>
                    <input id="btnCloseError" type="button" value="Close" style="display:none">
                </div>
                <div id="txtHelp"><a style="text-decoration: underline;" href="javascript:void(window.open('http://weather.gov/ForecastSearchHelp.html','locsearchhelp','status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,height=500,width=530').focus());">Location Help</a></div>
            </form>
<input id="inputstring" name="inputstring" type="text" value="Enter location    ..." onclick="this.value=''" autocomplete="off">

1 个答案:

答案 0 :(得分:0)

发布到网址http://forecast.weather.gov/zipcity.php 这就是php脚本用于从表单接收帖子数据的地方。